Flutter Widget之SnackBar

SnackBar是用户操作后,显示提示信息的一个控件,类似Toast,会自动隐藏。SnackBar还可以添加操作按钮。SnackBar是通过Scaffold的showSnackBar方法来显示的。

Widget:https://flutter.io/docs/development/ui/widgets
SnackBar:https://docs.flutter.io/flutter/material/SnackBar-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
import 'package:flutter/material.dart';

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

class _SnackBarDemoPageState extends State<SnackBarDemoPage> {

@override
void initState() {
super.initState();
}

Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
title: new Text('SnackBar Demo'),
),
floatingActionButton: new Builder(builder: (BuildContext context) {
return new FloatingActionButton(
onPressed: () {
// SnackBarBuilder.showSnackBar(context);
SnackBarBuilder.showAlertSnackBar(context);
},
child: new Icon(Icons.add),
);
}),
// body: Center(
// child: Text('SnackBar'),
// )
body: new Builder(builder: (BuildContext context) {
return new Center(
child: new GestureDetector(
onTap: () {
final snackBar = new SnackBar(content: new Text('这是一个SnackBar'));
Scaffold.of(context).showSnackBar(snackBar);
},
child: new Text('显示SnackBar'),
),
);
})
);
}
}

class SnackBarBuilder {
static void showSnackBar(BuildContext context) {
final snackBar = new SnackBar(content: new Text('这是一个SnackBar!'));
Scaffold.of(context).showSnackBar(snackBar);
}

static void showAlertSnackBar(BuildContext context) {
final snackBar = new SnackBar(
content: new Text('删除信息'),
action: new SnackBarAction(
label: '撤消',
onPressed: () {
// do something to undo
}
),
);
Scaffold.of(context).showSnackBar(snackBar);
}
}

Flutter Widget SnackBar

Powered by AppBlog.CN     浙ICP备14037229号

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

访客数 : | 访问量 :