I am trying to have a ListView inside NestedScrollView to have SliverAppBar collapsed. However if I add a controller to the ListView, it stops working (AppBar does not collapse). Here's an example where left ListView does not affect SliverAppBar, but right ListView does.
import 'package:flutter/material.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
ScrollController scrollController = new ScrollController();
List<String> entries = ["a", "b", "c", "d", "e", "a", "b", "c", "d", "e"];
@override
Widget build(BuildContext context) {
return new DefaultTabController(
length: 2,
child: new Scaffold(
body: new NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
new SliverAppBar(
title: new Text("My app"),
pinned: true,
expandedHeight: 150.0,
floating: true,
forceElevated: innerBoxIsScrolled,
bottom: new TabBar(
tabs: <Tab>[
new Tab(text: "FIRST"),
new Tab(text: "SECOND"),
],
)),
];
},
body: new TabBarView(
children: <Widget>[
new ListView.builder(
itemCount: entries.length,
controller: scrollController,
itemExtent: 60.0,
itemBuilder: (buildContext, index) {
return new Text(entries[index]);
},
),
new ListView.builder(
itemCount: entries.length,
itemExtent: 60.0,
itemBuilder: (buildContext, index) {
return new Text(entries[index]);
},
),
],
),
),
),
);
}
}
Do you have any idea if it is possible to have a controller attached to a ListView and still notify NestedScrollView?
If you're using NestedScrollView, you're opting in to having it manage the scroll positions of each child Scrollable as if it were all one unified scrollable. There isn't a way to drive the positions of individual Scrollable children with controllers; doing so would be challenging because it could put the NestedScrollView into a confused state. However, you're not totally out of luck:
NestedScrollView.Scrollable in a NotificationListener to listen for ScrollNotification.Scrollable "reset" to a zero initial scroll position, you could change its key to blow away its state.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With