带控制器的SliverAppBar和Listview [英] SliverAppBar and Listview with controller

查看:197
本文介绍了带控制器的SliverAppBar和Listview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在NestedScrollView中放入一个ListView来折叠SliverAppBar.但是,如果我将控制器添加到ListView,它将停止工作(AppBar不会折叠).这是一个示例,其中左ListView不会影响SliverAppBar,而右ListView会影响.

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]);
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}

您是否知道是否可以将控制器连接到ListView并仍通知NestedScrollView?

Do you have any idea if it is possible to have a controller attached to a ListView and still notify NestedScrollView?

推荐答案

如果使用的是NestedScrollView,则选择让它管理每个子级Scrollable的滚动位置,就好像它们都是一个统一滚动.没有一种方法可以用控制器来驱动单个Scrollable儿童的位置.这样做将具有挑战性,因为它可能会使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包装在key使其状态消失.
  • 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.

这篇关于带控制器的SliverAppBar和Listview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆