flutter fluttertoast弹窗工具的使用和封装

效果:

 

1.pubspec.yaml中引入依赖:

fluttertoast: 8.0.9

2.直接在需要弹窗提示的地方使用:

 Fluttertoast.showToast(
    msg: "我是弹窗",
    toastLength: Toast.LENGTH_SHORT,
    gravity: ToastGravity.CENTER,
    timeInSecForIosWeb: 1,
    backgroundColor: Colors.blue,
    textColor: Colors.white,
    fontSize: 16.0);

3.在项目中我们会大量使用到弹窗工具,一般都是单独提出作为一个工具类使用,以便于我们后期的改动,直接改动工具类就达到全局修改的作用:

创建一个文件FlutterToast.dart: 提供操作成功和失败的两个方法:

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

void okToast(String text) {
   Fluttertoast.showToast(
    msg: text,
    toastLength: Toast.LENGTH_SHORT,
    gravity: ToastGravity.CENTER,
    timeInSecForIosWeb: 1,
    backgroundColor: Colors.blue,
    textColor: Colors.white,
    fontSize: 16.0);
}

void errorToast(String text) {
   Fluttertoast.showToast(
    msg:text,
    toastLength: Toast.LENGTH_SHORT,
    gravity: ToastGravity.CENTER,
    timeInSecForIosWeb: 1,
    backgroundColor: Colors.red,
    textColor: Colors.white,
    fontSize: 16.0);
}

最后在需要使用弹窗的地方使用就可以了:

okToast("操作成功的弹窗提示信息!"); //减少了大量的基础代码以及维护成本
errorToast("操作失败的弹窗提示信息!");

猜你喜欢

转载自blog.csdn.net/f234344435/article/details/127082397