Reputation: 11
I have a simple question: how to initialize the scroll position of a ListView widget, child of a TabView inside a NestedScrollView?
My application has different parts:
I'm trying to initialize the scroll position of the ListView of the 2nd tab to display the last items. But:
Help Me !! Please !
Here is my code:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: MyPage(),
);
}
}
class MyPage extends StatefulWidget {
@override
_MyPageState createState() => _MyPageState();
}
class _MyPageState extends State<MyPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: DefaultTabController(
length: 2,
child: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) => [
SliverAppBar(
pinned: true,
title: Text("Test Application"),
expandedHeight: 200,
bottom: TabBar(
tabs: [
Tab(icon: Icon(Icons.info), text: 'Tab 1'),
Tab(icon: Icon(Icons.chat), text: 'Tab 2'),
],
),
),
],
body: TabBarView(
children: [
ListView.builder(
itemCount: 100,
itemBuilder: (context, index) => Text("ITEM $index FROM TAB 1"),
),
Tab2(),
],
),
),
)
);
}
}
class Tab2 extends StatefulWidget {
@override
_Tab2State createState() => _Tab2State();
}
class _Tab2State extends State<Tab2> {
@override
Widget build(BuildContext context) {
return Column(
children: [
Expanded(
child: ListView.builder(
itemCount: 100,
itemBuilder: (context, index) => Text("ITEM $index FROM TAB 2"),
)
),
TextField()
],
);
}
@override
void initState() {
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
ScrollController controller = PrimaryScrollController.of(context);
controller.jumpTo(controller.position.maxScrollExtent);
});
}
}
Upvotes: 0
Views: 1091
Reputation: 11
I think I have found the solution: I just have to replace this string:
controller.jumpTo(controller.position.maxScrollExtent)
by this string:
controller.jumpTo(controller.positions.last.maxScrollExtent)
Upvotes: 1