logo头像
Snippet 博客主题

Flutter app 3.添加 loading toast

Flutter app 3.添加 loading toast

1.添加loading

创建 Loading Widget

LoginPage.dart 中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Widget _buildBody(){
if (_isLoading) {
return _buildLoading();
} else {
return new ListView(
children: <Widget>[
_buildCustomBar(),
_buildAccountEdit(),
_buildPWDEdit(),
_buildLogin(),
_buildTips(),
],
);
}
}

loading

1
2
3
4
5
6
7
8
9
10
Widget _buildLoading() {

return new Container(
padding: const EdgeInsets.all(32.0),
child: new Center(
child: new CircularProgressIndicator(),
),
);

}

1
var  _isLoading  = false ;

更新 状态

1
2
3
4
5
6
void showLoading(bool isshowLoading) {
setState( () {
_isLoading = isshowLoading;
},
);
}}

显示

1
showLoading(true);

2. Toast

参考
https://pub.dartlang.org/packages/fluttertoast#-installing-tab-

1. Depend it

Add this to your package’s pubspec.yaml file:

1
2
dependencies:
fluttertoast: ^2.0.7

2. Install it

You can install packages from the command line:

with Flutter:

1
$ flutter packages get

Alternatively, your editor might support flutter packages get. Check the docs for your editor to learn more.

  1. 使用

util/ToastUtils.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
import 'package:fluttertoast/fluttertoast.dart';

class ToastUtils {

static void showLongToast(String msg) {
Fluttertoast.showToast(
msg: msg,
toastLength: Toast.LENGTH_LONG,
);
}

static void showColoredToast(String msg,String bgcolor,String textcolor) {
Fluttertoast.showToast(
msg: msg,
toastLength: Toast.LENGTH_SHORT,
bgcolor: bgcolor,
textcolor: textcolor
);
}

static void showShortToast(String msg) {
Fluttertoast.showToast(
msg: msg,
toastLength: Toast.LENGTH_SHORT,
timeInSecForIos: 1
);
}
static void showTopShortToast(String msg) {
Fluttertoast.showToast(
msg: msg,
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.TOP,
timeInSecForIos: 1
);
}
static void showCenterShortToast(String msg) {
Fluttertoast.showToast(
msg: msg,
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIos: 1
);
}
}

LoginPage.dart

1
ToastUtils. showShortToast("登陆成功") ;
支付宝打赏 微信打赏

打赏