-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.dart
105 lines (96 loc) · 3.91 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import 'package:flutter/material.dart';
import 'package:nested_scroll_controller/nested_scroll_controller.dart';
/// A simple example app demonstrating basic usage of [NestedScrollController].
///
/// The code below is copied-and-modified from [https://api.flutter.dev/flutter/widgets/NestedScrollView-class.html].
/// The only modifications made are for the [NestedScrollController] to be clearly used.
void main() {
runApp(TestApp());
}
final double itemExtent = 48.0;
class TestApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "NestedScrollController Example",
home: ExamplePage(),
);
}
}
class ExamplePage extends StatelessWidget {
List<String> get _tabs => ["One", "Two"];
/// 1. Create the [NestedScrollController].
final NestedScrollController nestedScrollController = NestedScrollController();
@override
Widget build(BuildContext context) {
return Scaffold(
body: DefaultTabController(
length: _tabs.length,
child: NestedScrollView(
/// 2. Give the controller to the [NestedScrollView].
controller: nestedScrollController,
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
SliverOverlapAbsorber(
handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context),
sliver: SliverAppBar(
title: const Text('Books'),
pinned: true,
expandedHeight: 200.0,
forceElevated: innerBoxIsScrolled,
bottom: TabBar(
tabs: _tabs.map((String name) => Tab(text: name)).toList(),
),
),
),
];
},
/// 3. Wrap the body in a [Builder] to provide the [NestedScrollView.body] [BuildContext].
body: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
/// 4. Provide the [NestedScrollController] with the [NestedScrollView] body context
/// and, since [NestedScrollController.centerScroll] is true, also provide the controller
/// with the [NestedScrollView] body constraints.
nestedScrollController.enableScroll(context);
nestedScrollController.enableCenterScroll(constraints);
return TabBarView(
children: _tabs.map((String name) {
return SafeArea(
top: false,
bottom: false,
child: CustomScrollView(
key: PageStorageKey<String>(name),
slivers: <Widget>[
SliverOverlapInjector(
handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context),
),
SliverPadding(
padding: const EdgeInsets.all(8.0),
sliver: SliverFixedExtentList(
itemExtent: itemExtent,
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return ListTile(
title: Text('Item $index'),
onTap: () {
/// 5. Use the [NestedScrollController]!
nestedScrollController.nestedAnimateTo(index * itemExtent);
},
);
},
childCount: 30,
),
),
),
],
),
);
}).toList(),
);
},
),
),
),
);
}
}