Flutter Widget之Buton

Widget:https://flutter.io/docs/development/ui/widgets
RaisedButton:https://docs.flutter.io/flutter/material/RaisedButton-class.html
FlatButton:https://docs.flutter.io/flutter/material/FlatButton-class.html
MaterialButton:https://docs.flutter.io/flutter/material/MaterialButton-class.html
OutlineButton:https://docs.flutter.io/flutter/material/OutlineButton-class.html
CupertinoButton:https://docs.flutter.io/flutter/cupertino/CupertinoButton-class.html

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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class ButtonDemoPage extends StatefulWidget {
@override
State<StatefulWidget> createState() => new _ButtonDemoPageState();
}

class _ButtonDemoPageState extends State<ButtonDemoPage> {
void onRaisedButtonClicked() {
print('onRaisedButtonClicked');
}

void onHighLightChanged(bool highLight) {
print('onHighLightChanged: $highLight');
}

@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(title: new Text('Button Page Demo'),),
body: Center(
child: ButtonBuilder().getButtons(context),
),
);
}
}

class ButtonBuilder {
VoidCallback onButtonPressed(BuildContext context) {
return () {
showToast(context, 'onButtonPressed');
};
}

ValueChanged<bool> onHighlightChanged(BuildContext context) {
return (bool b) {
//showToast(context, "onHighlightChanged:" + b.toString());
};
}

Widget getButtons(BuildContext context) {
return new Column(children: [
SizedBox(height: 10),
new Expanded(flex: 0, child: getMaterialButton(context)),
SizedBox(height: 10),
new Expanded(flex: 0, child: getRaisedButtonRow(context)),
SizedBox(height: 10),
new Expanded(flex: 0, child: getFlatButtonRow(context)),
SizedBox(height: 10),
new Expanded(flex: 0, child: getOutlineButtonRow(context)),
SizedBox(height: 10),
new Expanded(flex: 0, child: getCupertinoButton(context)),
SizedBox(height: 10),
]);
}

Widget getMaterialButton(BuildContext context) {
return MaterialButton(
key: ValueKey("text"),
child: Text("MaterialButton"),
onPressed: onButtonPressed(context),
onHighlightChanged: onHighlightChanged(context),
textTheme: ButtonTextTheme.normal,
textColor: Colors.blue,
disabledTextColor: Colors.red,
color: Color(0xFF82B1FF),
disabledColor: Colors.grey,
highlightColor: Colors.grey,
// 按下的背景色
splashColor: Colors.green,
// 水波纹颜色
colorBrightness: Brightness.light,
// 主题
elevation: 10,
highlightElevation: 10,
disabledElevation: 10,
padding: EdgeInsets.all(10),
// MaterialButton shape 子类才起效
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20)),
side: BorderSide(
color: Color(0xFFFFFFFF), style: BorderStyle.solid, width: 2)),
clipBehavior: Clip.antiAlias,
materialTapTargetSize: MaterialTapTargetSize.padded,
animationDuration: Duration(seconds: 1),
minWidth: 200,
height: 50,
);
}

Widget getRaisedButtonRow(BuildContext context) {
return new Row(children: [
SizedBox(width: 10),
new Expanded(flex: 1, child: getRaisedButton(context)),
SizedBox(width: 10),
new Expanded(flex: 1, child: getRaisedButtonIcon(context)),
SizedBox(width: 10),
]);
}

Widget getRaisedButton(BuildContext context) {
return RaisedButton(
child: Text("RaisedButton"),
onPressed: onButtonPressed(context),
onHighlightChanged: onHighlightChanged(context),
textTheme: ButtonTextTheme.normal,
textColor: Colors.blue,
disabledTextColor: Colors.red,
color: Color(0xFF82B1FF),
disabledColor: Colors.grey,
highlightColor: Colors.grey,
// 按下的背景色
splashColor: Colors.green,
// 水波纹颜色
colorBrightness: Brightness.light,
// 主题
elevation: 10,
highlightElevation: 10,
disabledElevation: 10,
padding: EdgeInsets.all(10),
// RaisedButton 才起效
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20)),
side: BorderSide(
color: Color(0xFFFFF00F), style: BorderStyle.solid, width: 2)),
clipBehavior: Clip.antiAlias,
materialTapTargetSize: MaterialTapTargetSize.padded,
animationDuration: Duration(seconds: 1),
// minWidth: 200,
// height: 50,
);
}

Widget getRaisedButtonIcon(BuildContext context) {
return RaisedButton.icon(
icon: Icon(Icons.menu),
label: Text("RaisedButton.icon"),
onPressed: onButtonPressed(context),
onHighlightChanged: onHighlightChanged(context),
textTheme: ButtonTextTheme.normal,
textColor: Colors.blue,
disabledTextColor: Colors.red,
color: Color(0xFF82B1FF),
disabledColor: Colors.grey,
highlightColor: Colors.red,
// 按下的背景色
// splashColor: Colors.green,// 水波纹颜色
colorBrightness: Brightness.light,
// 主题
elevation: 10,
highlightElevation: 10,
disabledElevation: 10,
// padding: EdgeInsets.all(10),
// RaisedButton 才起效
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(10)),
side: BorderSide(
color: Color(0xFFF0F00), style: BorderStyle.solid, width: 2)),
clipBehavior: Clip.antiAlias,
materialTapTargetSize: MaterialTapTargetSize.padded,
animationDuration: Duration(seconds: 1),
// minWidth: 200,
// height: 50,
);
}

Widget getFlatButtonRow(BuildContext context) {
return new Row(children: [
SizedBox(width: 10),
new Expanded(flex: 1, child: getFlatButton(context)),
SizedBox(width: 10),
new Expanded(flex: 1, child: getFlatButtonIcon(context)),
SizedBox(width: 10),
]);
}

Widget getFlatButton(BuildContext context) {
return FlatButton(
child: Text("FlatButton"),
onPressed: onButtonPressed(context),
onHighlightChanged: onHighlightChanged(context),
textTheme: ButtonTextTheme.normal,
textColor: Colors.yellow,
disabledTextColor: Colors.red,
color: Color(0xFF82B1FF),
disabledColor: Colors.grey,
highlightColor: Colors.grey,
// 按下的背景色
splashColor: Colors.transparent,
// 水波纹颜色
colorBrightness: Brightness.light,
// 主题
// elevation: 10,
// highlightElevation: 10,
// disabledElevation: 10,
padding: EdgeInsets.all(10),
// RaisedButton 才起效
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20)),
side: BorderSide(
color: Color(0xFFF9F3FF), style: BorderStyle.solid, width: 2)),
clipBehavior: Clip.antiAlias,
materialTapTargetSize: MaterialTapTargetSize.padded,
// animationDuration: Duration(seconds:1),
// minWidth: 200,
// height: 50,
);
}

Widget getFlatButtonIcon(BuildContext context) {
return FlatButton.icon(
icon: Icon(
Icons.menu,
color: Colors.green,
),
label: Text("FlatButton.icon"),
onPressed: onButtonPressed(context),
onHighlightChanged: onHighlightChanged(context),
textTheme: ButtonTextTheme.normal,
textColor: Colors.yellow,
disabledTextColor: Colors.red,
color: Color(0xFF82B1FF),
disabledColor: Colors.grey,
highlightColor: Colors.red,
// 按下的背景色
splashColor: Colors.green,
// 水波纹颜色
colorBrightness: Brightness.light,
// 主题
// elevation: 10,
// highlightElevation: 10,
// disabledElevation: 10,
// padding: EdgeInsets.all(10),
// RaisedButton 才起效
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(10)),
side: BorderSide(
color: Color(0xFF6FFF00), style: BorderStyle.solid, width: 2)),
clipBehavior: Clip.antiAlias,
materialTapTargetSize: MaterialTapTargetSize.padded,
// animationDuration: Duration(seconds:1),
// minWidth: 200,
// height: 50,
);
}

Widget getOutlineButtonRow(BuildContext context) {
return new Row(children: [
SizedBox(width: 10),
new Expanded(flex: 1, child: getOutlineButton(context)),
SizedBox(width: 10),
new Expanded(flex: 1, child: getOutlineButtonIcon(context)),
SizedBox(width: 10),
]);
}

Widget getOutlineButton(BuildContext context) {
return OutlineButton(
child: Text("OutlineButton"),
onPressed: onButtonPressed(context),
// onHighlightChanged: onHighlightChanged,
textTheme: ButtonTextTheme.accent,
textColor: Colors.blueAccent,
disabledTextColor: Colors.red,
color: Color(0xFF82B1FF),
// disabledColor: Colors.grey,
highlightColor: Color(0xFF2962FF),
// 按下的背景色
splashColor: Colors.red,
// 水波纹颜色
// colorBrightness: Brightness.light, // 主题
// elevation: 10,
highlightElevation: 10,
// disabledElevation: 10,
padding: EdgeInsets.all(10),
// RaisedButton 才起效
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20)),
side: BorderSide(
color: Color(0xFFFFFF00), style: BorderStyle.solid, width: 2)),
clipBehavior: Clip.antiAlias,
// materialTapTargetSize: MaterialTapTargetSize.padded,
// animationDuration: Duration(seconds:1),
// minWidth: 200,
// height: 50,
);
}

Widget getOutlineButtonIcon(BuildContext context) {
return OutlineButton.icon(
icon: Icon(
Icons.menu,
color: Colors.green,
),
label: Text("OutlineButton.icon"),
onPressed: onButtonPressed(context),
// onHighlightChanged: onHighlightChanged,
textTheme: ButtonTextTheme.normal,
textColor: Colors.yellow,
disabledTextColor: Colors.red,
color: Color(0xFF82B1FF),
// disabledColor: Colors.grey,
highlightColor: Colors.red,
// 按下的背景色
splashColor: Colors.green,
// 水波纹颜色
// colorBrightness: Brightness.light, // 主题
// elevation: 10,
highlightElevation: 10,
// disabledElevation: 10,
padding: EdgeInsets.all(10),
// RaisedButton 才起效
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(12)),
),
clipBehavior: Clip.antiAlias,
// materialTapTargetSize: MaterialTapTargetSize.padded,
// animationDuration: Duration(seconds:1),
// minWidth: 200,
// height: 50,
);
}

Widget getCupertinoButton(BuildContext context) {
return CupertinoButton(
child: Text("CupertinoButton"),
onPressed: onButtonPressed(context),
color: Colors.blue,
disabledColor: Colors.grey,
padding: EdgeInsets.all(10),
minSize: 50,
pressedOpacity: 0.8,
borderRadius: BorderRadius.all(Radius.circular(8.0)),
);
}

void showToast(BuildContext context, var msg) async {
Widget _buildToastWidget() {
return Center(
child: Card(
color: Colors.black26,
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 5.0),
child: Text(
msg,
style: TextStyle(
fontSize: 14.0,
color: Colors.white,
),
),
),
),
);
}

//获取OverlayState
OverlayState overlayState = Overlay.of(context);
//创建OverlayEntry
OverlayEntry _overlayEntry = OverlayEntry(
//toast靠它加到屏幕上
builder: (BuildContext context) => Positioned(
//top值,可以改变这个值来改变toast在屏幕中的位置
top: MediaQuery.of(context).size.height * 4 / 7,
child: Container(
alignment: Alignment.center,
width: MediaQuery.of(context).size.width,
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 80.0),
child: _buildToastWidget(),
)),
));
//显示到屏幕上
overlayState.insert(_overlayEntry);
//等待2秒
await Future.delayed(Duration(seconds: 2));
//移除
_overlayEntry.remove();
}
}

Flutter Widget Button

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :