简介
fluttertoast
是调用了Android
原生的Toast
,首先在各个系统上的样式就不统一,同时部分系统机型上受限通知权限,会导致Toast
无法弹出。
使用
- 修改
pubspec.yaml
文件,添加依赖oktoast
# ...
dev_dependencies:
# ...
oktoast: ^2.3.2
# ...
- 修改
lib/main.dart
文件
import 'package:oktoast/oktoast.dart';
// ...
@override
Widget build(BuildContext context) {
return OKToast(
backgroundColor: Colors.black54,
textPadding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 10.0),
radius: 20.0,
position: ToastPosition.bottom,
child: MaterialApp(
// ...
)
);
}
- 在
lib/utils/
目录下新增toast.dart
文件
import 'package:oktoast/oktoast.dart';
class Toast {
static void show(String msg, { int duration = 2000 }) {
if (msg == null) {
return;
}
showToast(msg, duration: Duration(milliseconds: duration), dismissOtherToast: true);
}
static void cancelToast() {
dismissAllToast();
}
}
- 代码中使用
Toast.show('xxxx');
发表评论