带控制器的SliverAppBar和Listview

时间:2017-10-18 18:49:24

标签: dart flutter

我想在ListViewNestedScrollViewSliverAppBar崩溃。但是,如果我向ListView添加控制器,它将停止工作(AppBar不会崩溃)。这是一个示例,其中左ListView不会影响SliverAppBar,但右ListView会影响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并仍然通知<SOAP-ENV:Body> <log-entry serial="abcde" domain="abc"> <date>Tue Oct 17 2017</date> <time utc="abcde">14:14:30</time> <type>all</type> <class>ccccc</class> <object>Web_Token</object> <level num="5">notice</level> <transaction>xxxxx</transaction> <global-transaction-id>xxxxx</global-transaction-id> <client>X.X.X.X</client> <message> <base64>**encodeddata**</base64> </message> </log-entry> </SOAP-ENV:Body> i need output <SOAP-ENV:Body> <log-entry serial="abcde" domain="abc"> <date>Tue Oct 17 2017</date> <time utc="abcde">14:14:30</time> <type>all</type> <class>ccccc</class> <object>Web_Token</object> <level num="5">notice</level> <transaction>xxxxx</transaction> <global-transaction-id>xxxxx</global-transaction-id> <client>X.X.X.X</client> <message> <base64>**decodeddata**</base64> </message> </log-entry> </SOAP-ENV:Body>

1 个答案:

答案 0 :(得分:7)

如果您正在使用NestedScrollView,则您选择让它管理每个孩子Scrollable的滚动位置,就好像它们都是统一的可滚动一样。没有办法驱动个体Scrollable孩子与控制者的位置;这样做会很有挑战性,因为它可能会使NestedScrollView处于混乱状态。但是,你并非完全没有运气:

  • 您可以将控制器提供给NestedScrollView
  • 如果您只想在嵌套滚动视图中有滚动事件时收到有关当前滚动位置或更新的通知,您可以将Scrollable包裹在NotificationListener中以监听{{3 }}。
  • 如果您想让孩子Scrollable&#34;重置&#34;在初始滚动位置为零时,您可以更改其key以消除其状态。