|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | + |
| 3 | +class Index extends StatefulWidget { |
| 4 | + @override |
| 5 | + State<StatefulWidget> createState() => _IndexState(); |
| 6 | +} |
| 7 | + |
| 8 | +class _IndexState extends State<Index> { |
| 9 | + List<String> _tabs; |
| 10 | + |
| 11 | + @override |
| 12 | + void initState() { |
| 13 | + super.initState(); |
| 14 | + _tabs = ['tab_1', 'tab_2']; |
| 15 | + } |
| 16 | + |
| 17 | + @override |
| 18 | + Widget build(BuildContext context) { |
| 19 | + return DefaultTabController( |
| 20 | + length: _tabs.length, // This is the number of tabs. |
| 21 | + child: NestedScrollView( |
| 22 | + headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) { |
| 23 | + // These are the slivers that show up in the "outer" scroll view. |
| 24 | + return <Widget>[ |
| 25 | + SliverOverlapAbsorber( |
| 26 | + // This widget takes the overlapping behavior of the SliverAppBar, |
| 27 | + // and redirects it to the SliverOverlapInjector below. If it is |
| 28 | + // missing, then it is possible for the nested "inner" scroll view |
| 29 | + // below to end up under the SliverAppBar even when the inner |
| 30 | + // scroll view thinks it has not been scrolled. |
| 31 | + // This is not necessary if the "headerSliverBuilder" only builds |
| 32 | + // widgets that do not overlap the next sliver. |
| 33 | + // 交叠减震器,当组件滚动造成交叠、覆盖时,可以增加SliverOverlapAbsorber |
| 34 | + handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context), |
| 35 | + child: SliverAppBar( |
| 36 | + title: const Text( |
| 37 | + 'NestedScrollView'), // This is the title in the app bar. |
| 38 | + pinned: true, // 固定顶部appbar |
| 39 | + expandedHeight: 200.0, // 展开显示面板的最大高度 |
| 40 | + // The "forceElevated" property causes the SliverAppBar to show |
| 41 | + // a shadow. The "innerBoxIsScrolled" parameter is true when the |
| 42 | + // inner scroll view is scrolled beyond its "zero" point, i.e. |
| 43 | + // when it appears to be scrolled below the SliverAppBar. |
| 44 | + // Without this, there are cases where the shadow would appear |
| 45 | + // or not appear inappropriately, because the SliverAppBar is |
| 46 | + // not actually aware of the precise position of the inner |
| 47 | + // scroll views. |
| 48 | + forceElevated: innerBoxIsScrolled, // appbar底部阴影 |
| 49 | + bottom: TabBar( |
| 50 | + // These are the widgets to put in each tab in the tab bar. |
| 51 | + tabs: _tabs.map((String name) => Tab(text: name)).toList(), |
| 52 | + ), |
| 53 | + // appbar导航左侧按钮 |
| 54 | + leading: Container( |
| 55 | + child: Icon(Icons.access_alarm), |
| 56 | + ), |
| 57 | + flexibleSpace: FlexibleSpaceBar( |
| 58 | + background: Image.network( |
| 59 | + 'https://static.moschat.com/efoxfile/Moschat/ojbk.png', |
| 60 | + filterQuality: FilterQuality.high, |
| 61 | + fit: BoxFit.none, |
| 62 | + ), |
| 63 | + ), |
| 64 | + ), |
| 65 | + ), |
| 66 | + ]; |
| 67 | + }, |
| 68 | + body: TabBarView( |
| 69 | + // These are the contents of the tab views, below the tabs. |
| 70 | + children: _tabs.map((String name) { |
| 71 | + return SafeArea( |
| 72 | + top: true, |
| 73 | + bottom: true, |
| 74 | + child: Builder( |
| 75 | + // This Builder is needed to provide a BuildContext that is "inside" |
| 76 | + // the NestedScrollView, so that sliverOverlapAbsorberHandleFor() can |
| 77 | + // find the NestedScrollView. |
| 78 | + builder: (BuildContext context) { |
| 79 | + return CustomScrollView( |
| 80 | + // The "controller" and "primary" members should be left |
| 81 | + // unset, so that the NestedScrollView can control this |
| 82 | + // inner scroll view. |
| 83 | + // If the "controller" property is set, then this scroll |
| 84 | + // view will not be associated with the NestedScrollView. |
| 85 | + // The PageStorageKey should be unique to this ScrollView; |
| 86 | + // it allows the list to remember its scroll position when |
| 87 | + // the tab view is not on the screen. |
| 88 | + // controller跟primary属性不需要在此设置,否则会独立与NestedScrollView的控制。 |
| 89 | + key: PageStorageKey<String>(name), |
| 90 | + slivers: <Widget>[ |
| 91 | + // SliverOverlapInjector与SliverOverlapAbsorber是相对成立的, |
| 92 | + // 若不增加SliverOverlapInjector,则下方的list顶部会被上方headerSliverBuilder所创建的组件遮住, |
| 93 | + // 增加后,类似clear:both效果,使得布局能顺畅衔接 |
| 94 | + SliverOverlapInjector( |
| 95 | + // This is the flip side of the SliverOverlapAbsorber above. |
| 96 | + handle: NestedScrollView.sliverOverlapAbsorberHandleFor( |
| 97 | + context), |
| 98 | + ), |
| 99 | + SliverPadding( |
| 100 | + padding: const EdgeInsets.all(8.0), |
| 101 | + // In this example, the inner scroll view has |
| 102 | + // fixed-height list items, hence the use of |
| 103 | + // SliverFixedExtentList. However, one could use any |
| 104 | + // sliver widget here, e.g. SliverList or SliverGrid. |
| 105 | + sliver: SliverFixedExtentList( |
| 106 | + // The items in this example are fixed to 48 pixels |
| 107 | + // high. This matches the Material Design spec for |
| 108 | + // ListTile widgets. |
| 109 | + itemExtent: 48.0, |
| 110 | + delegate: SliverChildBuilderDelegate( |
| 111 | + (BuildContext context, int index) { |
| 112 | + // This builder is called for each child. |
| 113 | + // In this example, we just number each list item. |
| 114 | + return ListTile( |
| 115 | + title: Text('Item $index'), |
| 116 | + ); |
| 117 | + }, |
| 118 | + // The childCount of the SliverChildBuilderDelegate |
| 119 | + // specifies how many children this inner list |
| 120 | + // has. In this example, each tab has a list of |
| 121 | + // exactly 30 items, but this is arbitrary. |
| 122 | + childCount: 30, |
| 123 | + ), |
| 124 | + ), |
| 125 | + ), |
| 126 | + ], |
| 127 | + ); |
| 128 | + }, |
| 129 | + ), |
| 130 | + ); |
| 131 | + }).toList(), |
| 132 | + ), |
| 133 | + ), |
| 134 | + ); |
| 135 | + } |
| 136 | +} |
0 commit comments