項目中須要用到相似安卓的Toast提示框,由於flutter中又沒有相關組件,而後在網上看到個不錯的,地址https://www.jianshu.com/p/cf7877c9bdeb,而後拿過來修改了了一下封裝。javascript
先看一下效果圖java
廢話很少說~~上代碼git
首先你須要下載toast.dart文件,而後引用他~app
下載地址:https://gitee.com/daydayfull/flutter_toastasync
import 'package:test_app/toast.dart';
最簡單的調用,若是不須要更改樣式啥的直接這樣子就能夠用啦~ui
Toast.toast( context, msg: '大哥~你不是點到了我嗎~', );
效果以下:blog
若是你要求多一點,那你能夠根據下面的參數作修改ip
Toast.toast( context, msg: '大哥~你不是點到了我嗎~', // String 提示的文本 showTime: 2000, // int 顯示的時間,單位milliseconds,也就是毫秒 bgColor: Color.fromRGBO(130, 0, 0, 1), // Color 提示框背景顏色 textColor: Color.fromRGBO(250, 100, 100, 1), // Color 提示框文字顏色 textSize: 18.0, // double 提示框文字大小 position: 'bottom', // String 提示框顯示位置,默認是center,可設置top和bottom pdHorizontal: 50.0, // double 左右邊距 pdVertical: 30.0, // double 上下邊距 );
效果以下:ci
下面附上toast.dart的源碼rem
import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; class Toast { static OverlayEntry _overlayEntry; // toast靠它加到屏幕上 static bool _showing = false; // toast是否正在showing static DateTime _startedTime; // 開啓一個新toast的當前時間,用於對比是否已經展現了足夠時間 static String _msg; // 提示內容 static int _showTime; // toast顯示時間 static Color _bgColor; // 背景顏色 static Color _textColor; // 文本顏色 static double _textSize; // 文字大小 static String _toastPosition; // 顯示位置 static double _pdHorizontal; // 左右邊距 static double _pdVertical; // 上下邊距 static void toast( BuildContext context, { String msg, int showTime = 2000, Color bgColor = Colors.black, Color textColor = Colors.white, double textSize = 14.0, String position = '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( builder: (BuildContext context) => Positioned( //top值,能夠改變這個值來改變toast在屏幕中的位置 top: _calToastPosition(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 _calToastPosition(context) { var backResult; if(_toastPosition == 'top'){ backResult = MediaQuery.of(context).size.height * 1 / 4; }else if(_toastPosition == 'center'){ backResult = MediaQuery.of(context).size.height * 2 / 5; }else{ backResult = MediaQuery.of(context).size.height * 3 / 4; } return backResult; } }
若是你覺位置不合適可到源碼_calToastPosition()修改。