flutter插件 - oktoast(toast)

简介

官方文档

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');

创作不易,若本文对你有帮助,欢迎打赏支持作者!

 分享给好友: