Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SliverAppBar and Listview with controller

Tags:

flutter

dart

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?

like image 750
Marcin Szałek Avatar asked Oct 18 '17 18:10

Marcin Szałek


1 Answers

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:

  • You can give a controller to the NestedScrollView.
  • If you just want to be notified about the current scroll position or update when there are scroll events in the nested scroll views, you can wrap the Scrollable in a NotificationListener to listen for ScrollNotification.
  • If you want to make a child Scrollable "reset" to a zero initial scroll position, you could change its key to blow away its state.
like image 127
Collin Jackson Avatar answered Nov 07 '22 16:11

Collin Jackson