基于fluttertoast实现封装弹框提示工具类

 更新时间:2022年05月16日 15:19:38   作者:编程小龙  
这篇文章主要为大家介绍了基于fluttertoast实现封装弹框提示工具类的实现代码,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

提示

已将代码上传至gitee,后续会继续更新学习封装的一些组件:

flutter练习

实现效果

实现

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<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),
                      ),
                    ),
                  )
                ],
          );
        });
  }
  /// 弹出关于界面
  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}) {
    // 先关闭弹框再显示对应弹框
    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<DialogUtilsDemoPage> createState() => _DialogUtilsDemoPageState();
}
class _DialogUtilsDemoPageState extends State<DialogUtilsDemoPage> {
  /// 查询数据
  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实现封装弹框提示工具类的详细内容,更多关于fluttertoast封装弹框提示工具类的资料请关注脚本之家其它相关文章!

相关文章

  • 完美解决安卓jni项目会删除其他so文件的问题

    完美解决安卓jni项目会删除其他so文件的问题

    下面小编就为大家带来一篇完美解决安卓jni项目会删除其他so文件的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • Android实现图片毛玻璃背景效果

    Android实现图片毛玻璃背景效果

    这篇文章主要为大家详细介绍了Android实现图片毛玻璃背景效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • Android中TelephonyManager用法实例

    Android中TelephonyManager用法实例

    这篇文章主要介绍了Android中TelephonyManager用法,结合实例形式分析了TelephonyManager类的功能,使用技巧与相关注意事项,需要的朋友可以参考下
    2016-03-03
  • Android弹幕框架 黑暗火焰使基本使用方法

    Android弹幕框架 黑暗火焰使基本使用方法

    这篇文章主要介绍了Android弹幕框架黑暗火焰使基本使用方法,需要的朋友可以参考下。今天我将分享由BiliBili开源的Android弹幕框架(DanmakuFlameMaster)的学习经验,感兴趣的朋友一起看看吧
    2016-10-10
  • Android变形(Transform)之Matrix用法

    Android变形(Transform)之Matrix用法

    Android的2D变形(包括缩放,扭曲,平移,旋转等)可以通过Matrix来实现,本文研究了一下;接下来就将我这俩天研究的东西和大家分享下,先来看看Matrix的用法感兴趣的你可不要错过了哈
    2013-02-02
  • Android studio升级4.1时遇到的问题记录

    Android studio升级4.1时遇到的问题记录

    这篇文章主要介绍了Android studio升级4.1时遇到的问题记录,本文给大家介绍的非常详细,在大家的平时开发过程都是经常遇到的问题,需要的朋友可以参考下
    2020-10-10
  • 关于AndroidStudio R文件莫名其妙缺失的快速解决方法

    关于AndroidStudio R文件莫名其妙缺失的快速解决方法

    下面小编就为大家带来一篇关于AndroidStudio R文件莫名其妙缺失的快速解决方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-03-03
  • Android基于OpenGL在GLSurfaceView上绘制三角形及使用投影和相机视图方法示例

    Android基于OpenGL在GLSurfaceView上绘制三角形及使用投影和相机视图方法示例

    这篇文章主要介绍了Android基于OpenGL在GLSurfaceView上绘制三角形及使用投影和相机视图方法,结合实例形式分析了Android基于OpenGL的图形绘制技巧,需要的朋友可以参考下
    2016-10-10
  • Android使用Jetpack Compose开发零基础起步教程

    Android使用Jetpack Compose开发零基础起步教程

    Jetpack Compose是用于构建原生Android UI的现代工具包。Jetpack Compose使用更少的代码,强大的工具和直观的Kotlin API,简化并加速了Android上的UI开发
    2023-04-04
  • Android中Item实现点击水波纹效果

    Android中Item实现点击水波纹效果

    这篇文章主要给大家介绍了关于Android中Item实现点击水波纹效果的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-11-11

最新评论