基于fluttertoast怎么实现封装弹框提示工具类
发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,本文小编为大家详细介绍"基于fluttertoast怎么实现封装弹框提示工具类",内容详细,步骤清晰,细节处理妥当,希望这篇"基于fluttertoast怎么实现封装弹框提示工具类"文章能帮助大家解决
千家信息网最后更新 2026年10月31日基于fluttertoast怎么实现封装弹框提示工具类
本文小编为大家详细介绍"基于fluttertoast怎么实现封装弹框提示工具类",内容详细,步骤清晰,细节处理妥当,希望这篇"基于fluttertoast怎么实现封装弹框提示工具类"文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
实现效果

实现
1.先在pubspec.yaml文件汇总引入fluttertoast的包:
fluttertoast: ^8.0.8 # 弹窗
2.封装弹框工具类DialogUtils:
import 'package:flutter/material.dart';import 'package:fluttertoast/fluttertoast.dart';/// @author longzipeng/// @创建时间:2022/2/24/// 封装自定义弹框class DialogUtils { /// 基础弹框 static alert( BuildContext context, { String title = "提示", String content = "", GestureTapCallback? confirm, GestureTapCallback? cancle, List? actions, // 自定义按钮 }) { showDialog( context: context, builder: (context) { return AlertDialog( title: Text( '提示', style: TextStyle(color: Theme.of(context).primaryColor), ), content: Text(content), actions: actions ?? [ 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), ), ), ) ], ); }); } /// 弹出关于界面 static alertAboutDialog(BuildContext context) { showAboutDialog( context: context, applicationIcon: FlutterLogo(), applicationName: 'flutterdemo', applicationVersion: '1.0.0', applicationLegalese: 'copyright 编程小龙', children: [ 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}) { // 先关闭弹框再显示对应弹框 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); }} 测试
注意:这里ListTitleWidget是自己封装的组件,直接改为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/// @创建时间:2022/3/31/// 弹框演示页面class DialogUtilsDemoPage extends StatefulWidget { const DialogUtilsDemoPage({Key? key}) : super(key: key); @override State createState() => _DialogUtilsDemoPageState();}class _DialogUtilsDemoPageState extends State { /// 查询数据 search(value) { print("搜索的值为:$value"); } @override Widget build(BuildContext context) { return Scaffold( appBar: const CommonAppbar( title: "弹窗、提示演示", ), body: ListView( children: [ ListTitleWidget( title: const Text("弹框,带确认和取消"), onTap: () { DialogUtils.alert(context, content: "靓仔、靓女们,一起学习flutter!", confirm: () { print("点击了确认"); }, cancle: () { print("点击了取消"); }); }, ), ListTitleWidget( title: const Text("默认提示"), onTap: () { DialogUtils.showMessage("默认提示"); }, ), ListTitleWidget( title: const Text("成功提示"), onTap: () { DialogUtils.showSuccessMessage("成功提示"); }, ), ListTitleWidget( title: const Text("警告提示"), onTap: () { DialogUtils.showWaringMessage("警告提示"); }, ), ListTitleWidget( title: const Text("错误提示"), onTap: () { DialogUtils.showErrorMessage("错误提示"); }, ), ], ), ); }} 读到这里,这篇"基于fluttertoast怎么实现封装弹框提示工具类"文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注行业资讯频道。
提示
封装
工具
成功
文章
消息
错误
内容
时间
学习
演示
妥当
普通
信息
基础
小龙
思路
按钮
效果
数据
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
广州大一互联网络科技有限公司
高科技互联网
互联网科技公司税收优惠
菲比寻常互联网科技
融贸通互联网科技
上海万想互联网科技
科技互联网创业计划书
成都狮子座互联网科技
佰商互联网科技有限公司
华中科技有哪些互联网大佬
欧洲十大科技互联网公司
互联网科技公司实习生
知域互联网科技有限公司大骗子
互联网与数字科技关系
半导体行业属于互联网高科技类吗
江苏互联网科技小贷
宁波跨境商互联网科技有限公司
兰石爱特工业互联网科技分公司
北京军民互联网科技公司
众联互联网络科技
湖北九亚互联网络科技有限公司
华服互联网科技怎么样
今日互联网科技
合肥互联网科技公司
青岛互联网科技公司
互联网科技类展会
中国互联网有什么科技含量
互联网科技如何创业
深圳互联网科技的发展
互联网高科技行业有哪些企业