-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestIssue147Sample.cs
More file actions
153 lines (145 loc) · 7.07 KB
/
Copy pathTestIssue147Sample.cs
File metadata and controls
153 lines (145 loc) · 7.07 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.Collections.Generic;
using com.unity.uiwidgets.Runtime.rendering;
using UIWidgetsSample;
using Unity.UIWidgets.animation;
using Unity.UIWidgets.material;
using Unity.UIWidgets.painting;
using Unity.UIWidgets.widgets;
using Icons = UIWidgetsSample.Icons;
namespace DefaultNamespace
{
public class TestScrollController : UIWidgetsSamplePanel
{
protected override Widget createWidget()
{
return new MaterialApp(
home: new CustomScrollViewTestRoute());
}
private class CustomScrollViewTestRoute : StatelessWidget
{
public override Widget build(BuildContext context)
{
return new Material(
child: new CustomScrollView(
slivers: new List<Widget>
{
new SliverAppBar(
pinned: true,
expandedHeight: 250.0f,
flexibleSpace: new FlexibleSpaceBar(
title: new Text("Demo"),
background: Image.asset(
//The image path is different from https://book.flutterchina.club/chapter6/custom_scrollview.html
"india_thanjavur_market", fit: BoxFit.cover)
)
),
new SliverPadding(
padding: EdgeInsets.all(8.0f),
sliver: new SliverGrid( //Grid
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2, //Grid按两列显示
mainAxisSpacing: 10.0f,
crossAxisSpacing: 10.0f,
childAspectRatio: 4.0f
),
layoutDelegate: new SliverChildBuilderDelegate(
(BuildContext subContext, int subIndex) =>
{
//创建子widget
return new Container(
alignment: Alignment.center,
//There is a minor problem in https://book.flutterchina.club/chapter6/custom_scrollview.html,
//where the index could be 0, which leads to error since Colors.cyan (A dictionary) doesn't
//have such a key.
color: Colors.cyan[100 * (1 + subIndex % 8)],
child: new Text($"grid item {subIndex}")
);
},
childCount: 20
)
)
),
//List
new SliverFixedExtentList(
itemExtent: 50.0f,
del: new SliverChildBuilderDelegate(
(BuildContext subContext, int subIndex) =>
{
//创建列表项
return new Container(
alignment: Alignment.center,
//There is a minor problem in https://book.flutterchina.club/chapter6/custom_scrollview.html,
//where the index could be 0, which leads to error since Colors.lightBlue (A dictionary) doesn't
//have such a key.
color: Colors.lightBlue[100 * (1 + subIndex % 8)],
child: new Text($"list item {subIndex}")
);
},
childCount: 50 //50个列表项
)
)
}
)
);
}
}
private class ScrollControllerTestRoute : StatefulWidget
{
public override State createState()
{
return new ScrollControllerTestRouteState();
}
}
private class ScrollControllerTestRouteState : State<ScrollControllerTestRoute>
{
private ScrollController _controller = new ScrollController();
private bool showToTopBtn = false; //是否显示“返回到顶部”按钮
public override void initState()
{
//监听滚动事件,打印滚动位置
_controller.addListener(() =>
{
print(_controller.offset); //打印滚动位置
if (_controller.offset < 1000 && showToTopBtn)
setState(() => { showToTopBtn = false; });
else if (_controller.offset >= 1000 && showToTopBtn == false)
setState(() => { showToTopBtn = true; });
});
}
public override void dispose()
{
//为了避免内存泄露,需要调用_controller.dispose
_controller.dispose();
base.dispose();
}
public override Widget build(BuildContext context)
{
return new Scaffold(
appBar: new AppBar(title: new Text("滚动控制")),
body: new Scrollbar(
child: ListView.builder(
itemCount: 100,
itemExtent: 50.0f, //列表项高度固定时,显式指定高度是一个好习惯(性能消耗小)
controller: _controller,
itemBuilder: (SubContext, index) => { return new ListTile(title: new Text($"{index}")); }
)
),
floatingActionButton: !showToTopBtn
? null
: new FloatingActionButton(
child: new Icon(Icons.account_circle),
onPressed: () =>
{
//返回到顶部时执行动画
_controller.animateTo(0f,
duration: new TimeSpan(0, 0, 0, 0, 200),
curve: Curves.ease
);
}
)
);
}
}
}
}