flutter Toast实现消息提示框

 更新时间:2021年08月23日 14:01:39   作者:早起的年轻人  
这篇文章主要为大家详细介绍了flutter Toast实现消息提示框,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了flutter Toast实现消息提示框的具体代码,供大家参考,具体内容如下

使用方法

//默认是显示在中间的
Toast.toast(context,msg: "中间显示的 ");     
 Toast.toast(context,msg: "中间显示的 ",position: ToastPostion.center);
Toast.toast(context,msg: "顶部显示的 Toast $_count",position: ToastPostion.top);
Toast.toast(context,msg: "底部显示的 Toast $_count",position: ToastPostion.bottom);

Toast 源码

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

//Toast 显示位置控制 
enum ToastPostion {
 top,
 center,
 bottom,
}

class Toast {
 // toast靠它加到屏幕上
 static OverlayEntry _overlayEntry;
 // toast是否正在showing
 static bool _showing = false;
 // 开启一个新toast的当前时间,用于对比是否已经展示了足够时间
 static DateTime _startedTime;
 // 提示内容
 static String _msg;
 // toast显示时间
 static int _showTime;
 // 背景颜色
 static Color _bgColor;
 // 文本颜色
 static Color _textColor;
 // 文字大小
 static double _textSize;
 // 显示位置
 static ToastPostion _toastPosition;
 // 左右边距
 static double _pdHorizontal;
 // 上下边距
 static double _pdVertical;
 static void toast(
  BuildContext context, {
  //显示的文本
  String msg,
  //显示的时间 单位毫秒
  int showTime = 1000,
  //显示的背景
  Color bgColor = Colors.black,
  //显示的文本颜色
  Color textColor = Colors.white,
  //显示的文字大小
  double textSize = 14.0,
  //显示的位置
  ToastPostion position = ToastPostion.center,
  //文字水平方向的内边距
  double pdHorizontal = 20.0,
  //文字垂直方向的内边距
  double pdVertical = 10.0,
 }) async {
  assert(msg != null);
  _msg = msg;
  _startedTime = DateTime.now();
  _showTime = showTime;
  _bgColor = bgColor;
  _textColor = textColor;
  _textSize = textSize;
  _toastPosition = position;
  _pdHorizontal = pdHorizontal;
  _pdVertical = pdVertical;
  //获取OverlayState
  OverlayState overlayState = Overlay.of(context);
  _showing = true;
  if (_overlayEntry == null) {
   //OverlayEntry负责构建布局
   //通过OverlayEntry将构建的布局插入到整个布局的最上层
   _overlayEntry = OverlayEntry(
     builder: (BuildContext context) => Positioned(
        //top值,可以改变这个值来改变toast在屏幕中的位置
        top: buildToastPosition(context),
        child: Container(
          alignment: Alignment.center,
          width: MediaQuery.of(context).size.width,
          child: Padding(
           padding: EdgeInsets.symmetric(horizontal: 40.0),
           child: AnimatedOpacity(
            opacity: _showing ? 1.0 : 0.0, //目标透明度
            duration: _showing
              ? Duration(milliseconds: 100)
              : Duration(milliseconds: 400),
            child: _buildToastWidget(),
           ),
          )),
       ));
   //插入到整个布局的最上层
   overlayState.insert(_overlayEntry);
  } else {
   //重新绘制UI,类似setState
   _overlayEntry.markNeedsBuild();
  }
  // 等待时间
  await Future.delayed(Duration(milliseconds: _showTime));
  //2秒后 到底消失不消失
  if (DateTime.now().difference(_startedTime).inMilliseconds >= _showTime) {
   _showing = false;
   _overlayEntry.markNeedsBuild();
   await Future.delayed(Duration(milliseconds: 400));
   _overlayEntry.remove();
   _overlayEntry = null;
  }
 }

 //toast绘制
 static _buildToastWidget() {
  return Center(
   child: Card(
    color: _bgColor,
    child: Padding(
     padding: EdgeInsets.symmetric(
       horizontal: _pdHorizontal, vertical: _pdVertical),
     child: Text(
      _msg,
      style: TextStyle(
       fontSize: _textSize,
       color: _textColor,
      ),
     ),
    ),
   ),
  );
 }

// 设置toast位置
 static buildToastPosition(context) {
  var backResult;
  if (_toastPosition == ToastPostion.top) {
   backResult = MediaQuery.of(context).size.height * 1 / 4;
  } else if (_toastPosition == ToastPostion.center) {
   backResult = MediaQuery.of(context).size.height * 2 / 5;
  } else {
   backResult = MediaQuery.of(context).size.height * 3 / 4;
  }
  return backResult;
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • android 使用OkHttp上传多张图片的实现代码

    android 使用OkHttp上传多张图片的实现代码

    这篇文章主要介绍了android 使用OkHttp上传多张图片的相关资料,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-07-07
  • Android模仿Toast实现提示框效果

    Android模仿Toast实现提示框效果

    这篇文章主要为大家详细介绍了Android模仿Toast实现提示框效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08
  • Android形状图形与状态列表图形及九宫格图片超详细讲解

    Android形状图形与状态列表图形及九宫格图片超详细讲解

    这篇文章主要介绍了Android形状图形与状态列表图形及九宫格图片的应用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2022-09-09
  • Anroid四大组件service之本地服务的示例代码

    Anroid四大组件service之本地服务的示例代码

    本篇文章主要介绍了Anroid四大组件service之本地服务的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-10-10
  • Android事件与手势操作详解

    Android事件与手势操作详解

    现在程序都是通过事件实现人机交互的。事件:用于对图形界面的操作,有物理按键事件和触摸屏事件两大类。在Android组件中提供了事件处理的相关方法,如在View类中提供onTouchEvent()方法重写触摸屏事件,setOnTouchListener()方法为组件设置监听器来处理触摸屏事件
    2022-12-12
  • android SDk中常用的java包介绍

    android SDk中常用的java包介绍

    在android的应用程序开发中,通常使用的是java语言,除了需要熟悉java语言的基础知识之外,还需要了解android提供的扩展的java功能。android SDK中API提供一些扩展的java 类库,类库分为若干个包,每个包中包含若干个类
    2014-05-05
  • Android判断现在所处界面是否为home主桌面的方法

    Android判断现在所处界面是否为home主桌面的方法

    这篇文章主要介绍了Android判断现在所处界面是否为home主桌面的方法,涉及Android界面判断的相关技巧,需要的朋友可以参考下
    2015-05-05
  • Input系统分发策略及其应用示例详解

    Input系统分发策略及其应用示例详解

    这篇文章主要为大家介绍了Input系统分发策略及其应用示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-02-02
  • Android实现读取NFC卡的编号

    Android实现读取NFC卡的编号

    这篇文章主要为大家详细介绍了Android实现读取NFC卡的编号,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • Android截屏截图的几种方法总结

    Android截屏截图的几种方法总结

    这篇文章主要介绍了 Android截屏截图方法汇总(Activity、View、ScrollView、ListView、RecycleView、WebView截屏截图)的相关资料,需要的朋友可以参考下
    2017-05-05

最新评论