基于fluttertoast實現(xiàn)封裝彈框提示工具類
提示
已將代碼上傳至gitee,后續(xù)會繼續(xù)更新學(xué)習(xí)封裝的一些組件:
實現(xiàn)效果

實現(xiàn)
1.先在pubspec.yaml文件匯總引入fluttertoast的包:
fluttertoast: ^8.0.8 # 彈窗
2.封裝彈框工具類DialogUtils:
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
/// @author longzipeng
/// @創(chuàng)建時間:2022/2/24
/// 封裝自定義彈框
class DialogUtils {
/// 基礎(chǔ)彈框
static alert(
BuildContext context, {
String title = "提示",
String content = "",
GestureTapCallback? confirm,
GestureTapCallback? cancle,
List<Widget>? actions, // 自定義按鈕
}) {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text(
'提示',
style: TextStyle(color: Theme.of(context).primaryColor),
),
content: Text(content),
actions: actions ??
<Widget>[
InkWell(
onTap: () {
if (cancle != null) {
cancle();
}
Navigator.of(context).pop();
},
child: const Padding(
padding: EdgeInsets.only(right: 20),
child: Text(
"取消",
style: TextStyle(color: Colors.grey),
),
),
),
InkWell(
onTap: () {
if (confirm != null) {
confirm();
}
Navigator.of(context).pop();
},
child: Padding(
padding: const EdgeInsets.only(right: 10),
child: Text(
"確定",
style: TextStyle(color: Theme.of(context).primaryColor),
),
),
)
],
);
});
}
/// 彈出關(guān)于界面
static alertAboutDialog(BuildContext context) {
showAboutDialog(
context: context,
applicationIcon: FlutterLogo(),
applicationName: 'flutterdemo',
applicationVersion: '1.0.0',
applicationLegalese: 'copyright 編程小龍',
children: <Widget>[
Container(
height: 70,
child: const Text(
"總而言之,言而總之,時而不知,終究自知",
maxLines: 2,
style: TextStyle(),
),
),
],
);
}
/// 顯示普通消息
static showMessage(String msg,
{toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.grey,
fontSize: 16.0}) {
// 先關(guān)閉彈框再顯示對應(yīng)彈框
Fluttertoast.cancel();
Fluttertoast.showToast(
msg: msg,
toastLength: toastLength,
gravity: gravity,
timeInSecForIosWeb: timeInSecForIosWeb,
backgroundColor: backgroundColor,
fontSize: fontSize);
}
/// 顯示錯誤消息
static showErrorMessage(String msg,
{toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.red,
fontSize: 16.0}) {
showMessage(msg,
toastLength: toastLength,
gravity: gravity,
timeInSecForIosWeb: timeInSecForIosWeb,
backgroundColor: backgroundColor,
fontSize: fontSize);
}
/// 顯示警告信息
static showWaringMessage(String msg,
{toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.orangeAccent,
fontSize: 16.0}) {
showMessage(msg,
toastLength: toastLength,
gravity: gravity,
timeInSecForIosWeb: timeInSecForIosWeb,
backgroundColor: backgroundColor,
fontSize: fontSize);
}
/// 顯示成功消息
static showSuccessMessage(String msg,
{toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.greenAccent,
fontSize: 16.0}) {
showMessage(msg,
toastLength: toastLength,
gravity: gravity,
timeInSecForIosWeb: timeInSecForIosWeb,
backgroundColor: backgroundColor,
fontSize: fontSize);
}
}
測試
注意:這里L(fēng)istTitleWidget是自己封裝的組件,直接改為ListTitle就不會報錯了
import 'package:csdn_flutter_demo/pages/common/common_appbar.dart';
import 'package:csdn_flutter_demo/utils/dialog_utils.dart';
import 'package:csdn_flutter_demo/widgets/list_title_widgets.dart';
import 'package:flutter/material.dart';
/// @author longzipeng
/// @創(chuàng)建時間:2022/3/31
/// 彈框演示頁面
class DialogUtilsDemoPage extends StatefulWidget {
const DialogUtilsDemoPage({Key? key}) : super(key: key);
@override
State<DialogUtilsDemoPage> createState() => _DialogUtilsDemoPageState();
}
class _DialogUtilsDemoPageState extends State<DialogUtilsDemoPage> {
/// 查詢數(shù)據(jù)
search(value) {
print("搜索的值為:$value");
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: const CommonAppbar(
title: "彈窗、提示演示",
),
body: ListView(
children: [
ListTitleWidget(
title: const Text("彈框,帶確認(rèn)和取消"),
onTap: () {
DialogUtils.alert(context, content: "靚仔、靚女們,一起學(xué)習(xí)flutter!",
confirm: () {
print("點擊了確認(rèn)");
}, cancle: () {
print("點擊了取消");
});
},
),
ListTitleWidget(
title: const Text("默認(rèn)提示"),
onTap: () {
DialogUtils.showMessage("默認(rèn)提示");
},
),
ListTitleWidget(
title: const Text("成功提示"),
onTap: () {
DialogUtils.showSuccessMessage("成功提示");
},
),
ListTitleWidget(
title: const Text("警告提示"),
onTap: () {
DialogUtils.showWaringMessage("警告提示");
},
),
ListTitleWidget(
title: const Text("錯誤提示"),
onTap: () {
DialogUtils.showErrorMessage("錯誤提示");
},
),
],
),
);
}
}
以上就是基于fluttertoast實現(xiàn)封裝彈框提示工具類的詳細(xì)內(nèi)容,更多關(guān)于fluttertoast封裝彈框提示工具類的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android開發(fā)ListView中下拉刷新上拉加載及帶列的橫向滾動實現(xiàn)方法
這篇文章主要介紹了Android開發(fā)ListView中下拉刷新上拉加載及帶列的橫向滾動實現(xiàn)方法的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07
Android 中RecycleView實現(xiàn)item的點擊事件
這篇文章主要介紹了Android 中RecycleView實現(xiàn)item的點擊事件的相關(guān)資料,需要的朋友可以參考下2017-03-03
Android游戲開發(fā)學(xué)習(xí)②焰火綻放效果實現(xiàn)方法
這篇文章主要介紹了Android游戲開發(fā)學(xué)習(xí)②焰火綻放效果實現(xiàn)方法,以實例形式詳細(xì)分析了Android中粒子對象類Particle類和粒子集合類ParticleSet類及物理引擎ParticleThread類 的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10
Android如何實現(xiàn)一個DocumentProvider示例詳解
這篇文章主要為大家介紹了Android如何實現(xiàn)一個DocumentProvider示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12
Android?Jetpack組件Navigation導(dǎo)航組件的基本使用
本篇主要簡單介紹了一下?Navigation?是什么?以及使用它的流程是什么,并且結(jié)合實際案例?操作了一番,Navigation?還有很多其他用法,如條件導(dǎo)航、嵌套圖、過度動畫?等等功能?有機會再操作,需要的朋友可以參考下2022-06-06

