forked from Tencent/tdesign-flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtd_backtop_page.dart
More file actions
173 lines (160 loc) · 4.79 KB
/
td_backtop_page.dart
File metadata and controls
173 lines (160 loc) · 4.79 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import 'package:flutter/material.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import '../annotation/demo.dart';
import '../base/example_widget.dart';
class TDBackTopPage extends StatefulWidget {
const TDBackTopPage({Key? key}) : super(key: key);
@override
State<TDBackTopPage> createState() => _TDBackTopPageState();
}
class _TDBackTopPageState extends State<TDBackTopPage> {
ScrollController controller = ScrollController();
bool showBackTop = false;
TDBackTopStyle style = TDBackTopStyle.circle;
TDBackTopTheme theme = TDBackTopTheme.light;
@override
void initState() {
super.initState();
controller.addListener(listenCallback);
WidgetsBinding.instance.addPostFrameCallback((_) {
setState(() {
theme = Theme.of(context).brightness == Brightness.dark
? TDBackTopTheme.light
: TDBackTopTheme.dark;
});
});
}
@override
void dispose() {
super.dispose();
controller.removeListener(listenCallback);
}
void listenCallback() {
final shouldShow = controller.offset >= 100;
if (shouldShow != showBackTop) {
setState(() {
showBackTop = shouldShow;
});
}
}
@override
Widget build(BuildContext context) {
return ExamplePage(
scrollController: controller,
title: tdTitle(),
desc: '用于当页面过长往下滑动时,帮助用户快速回到页面顶部。',
exampleCodeGroup: 'backtop',
floatingActionButton: Stack(
clipBehavior: Clip.none,
children: [
Visibility(
visible: showBackTop,
child: style == TDBackTopStyle.halfCircle
? Positioned(
right: -16,
bottom: 10,
child: TDBackTop(
controller: controller,
theme: theme,
showText: true,
style: style,
))
: TDBackTop(
controller: controller,
theme: theme,
showText: true,
style: style,
)),
],
),
children: [
ExampleModule(title: '组件类型', children: [
ExampleItem(desc: '圆形返回顶部', builder: _buildCircleBackTop),
ExampleItem(desc: '半圆形返回顶部', builder: _buildHalfCircleBackTop),
])
]);
}
@Demo(group: 'backtop')
Widget _buildCircleBackTop(BuildContext context) {
return getCustomButton(context, '圆形返回顶部', () {
setState(() {
showBackTop = true;
if (controller.hasClients) {
controller.jumpTo(500);
}
style = TDBackTopStyle.circle;
});
});
}
@Demo(group: 'backtop')
Widget _buildHalfCircleBackTop(BuildContext context) {
return Column(
children: [
getCustomButton(context, '半圆形返回顶部', () {
setState(() {
showBackTop = true;
if (controller.hasClients) {
controller.jumpTo(500);
}
style = TDBackTopStyle.halfCircle;
});
}),
Padding(
padding: const EdgeInsets.only(left: 16, right: 16, top: 24),
child: Wrap(
spacing: 16,
runSpacing: 24,
children: List.generate(6, (_) => getDemoBox(context)),
),
)
],
);
}
Widget getCustomButton(
BuildContext context, String text, void Function() onTap) {
return TDButton(
text: text,
isBlock: true,
size: TDButtonSize.large,
type: TDButtonType.outline,
shape: TDButtonShape.rectangle,
theme: TDButtonTheme.primary,
onTap: onTap,
);
}
Widget getDemoBox(BuildContext context) {
final theme = TDTheme.of(context);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
// spacing: 10,
children: [
Container(
width: 163,
height: 163,
decoration: BoxDecoration(
color: theme.bgColorContainer,
borderRadius: BorderRadius.circular(theme.radiusExtraLarge),
),
),
const SizedBox(height: 10),
Container(
width: 163,
height: 16,
decoration: BoxDecoration(
color: theme.bgColorContainer,
borderRadius: BorderRadius.circular(theme.radiusSmall),
),
),
const SizedBox(height: 10),
Container(
width: 100,
height: 16,
decoration: BoxDecoration(
color: theme.bgColorContainer,
borderRadius: BorderRadius.circular(theme.radiusSmall),
),
),
],
);
}
}