1+ import 'package:flutter/material.dart' ;
2+ class TestNestedScrollView extends StatefulWidget {
3+ const TestNestedScrollView ({Key ? key}) : super (key: key);
4+
5+ @override
6+ State <TestNestedScrollView > createState () => _TestNestedScrollViewState ();
7+ }
8+
9+ class _TestNestedScrollViewState extends State <TestNestedScrollView >
10+ with SingleTickerProviderStateMixin {
11+ final List <String > tabTitles = < String > ["我的发布" , "我的关注" ];
12+ late TabController tabController;
13+
14+ @override
15+ void initState () {
16+ super .initState ();
17+ tabController = TabController (length: tabTitles.length, vsync: this );
18+ }
19+
20+ @override
21+ Widget build (BuildContext context) {
22+ return Scaffold (
23+ body: CustomScrollView (
24+ slivers: [
25+ _buildAppbar (),
26+ _buildTabView (),
27+ ],
28+ ),
29+ );
30+ }
31+
32+ Widget _buildAppbar () {
33+ return SliverAppBar (
34+ expandedHeight: 140 ,
35+ title: const Text ('CustomScrollView 测试' ),
36+ flexibleSpace: FlexibleSpaceBar (
37+ collapseMode: CollapseMode .pin,
38+ background:
39+ Image .asset ('assets/image/pic.png' , fit: BoxFit .cover)),
40+ pinned: true ,
41+ bottom: TabBar (
42+ controller: tabController,
43+ tabs: tabTitles.map ((String name) => Tab (text: name)).toList (),
44+ ),
45+ );
46+ }
47+
48+ Widget _buildTabView () {
49+ return SliverFillRemaining (
50+ child: TabBarView (
51+ controller: tabController,
52+ children: [
53+ buildScrollPage (Colors .red),
54+ buildScrollPage (Colors .amber),
55+ ],
56+ ),
57+ );
58+ }
59+
60+ Widget buildScrollPage (Color color) {
61+ return CustomScrollView (
62+ slivers: [
63+ _buildBox (color),
64+ SliverPadding (
65+ padding: EdgeInsets .all (8 ),
66+ sliver: _buildSliverGrid (),
67+ ),
68+ // _buildSliverGrid(),
69+ _buildSliverList (),
70+ ],
71+ );
72+ }
73+
74+ Widget _buildBox (Color color) {
75+ return SliverToBoxAdapter (
76+ child: Container (
77+ height: 60 ,
78+ color: color,
79+ ),
80+ );
81+ }
82+
83+ final List <int > data = List .generate (60 , (index) => index + 1 );
84+
85+ Widget _buildSliverList () {
86+ return SliverList (
87+ delegate: SliverChildBuilderDelegate (
88+ _buildItemByIndex,
89+ childCount: data.length,
90+ ));
91+ }
92+
93+ Widget _buildSliverGrid () {
94+ return SliverGrid (
95+ delegate: SliverChildBuilderDelegate (
96+ _buildItemByIndex,
97+ childCount: 8 ,
98+ ),
99+ gridDelegate: SliverGridDelegateWithFixedCrossAxisCount (
100+ crossAxisCount: 4 ,
101+ crossAxisSpacing: 8 ,
102+ childAspectRatio: 1 ,
103+ mainAxisSpacing: 8 ,
104+ ),
105+ );
106+ }
107+
108+ Widget _buildItemByIndex (BuildContext context, int index) {
109+ return ItemBox (
110+ index: data[index],
111+ );
112+ }
113+
114+ @override
115+ void dispose () {
116+ tabController.dispose ();
117+ super .dispose ();
118+ }
119+ }
120+
121+
122+ class ItemBox extends StatelessWidget {
123+ final int index;
124+
125+ ItemBox ({
126+ Key ? key,
127+ required this .index,
128+ }) : super (key: key) {
129+ // print('----构建ItemBox-----$index--------');
130+ }
131+
132+ Color get color => Colors .blue.withOpacity ((index % 10 ) * 0.1 );
133+
134+ @override
135+ Widget build (BuildContext context) {
136+ return Container (
137+ alignment: Alignment .center,
138+ color: color,
139+ height: 56 ,
140+ child: Text (
141+ '第 $index 个' ,
142+ style: const TextStyle (fontSize: 20 ),
143+ ),
144+ );
145+ }
146+ }
0 commit comments