flutterToast中怎么實(shí)現(xiàn)一個消息提示框,相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
在武強(qiáng)等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作按需網(wǎng)站開發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),成都全網(wǎng)營銷推廣,外貿(mào)營銷網(wǎng)站建設(shè),武強(qiáng)網(wǎng)站建設(shè)費(fèi)用合理。使用方法
//默認(rèn)是顯示在中間的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的當(dāng)前時間,用于對比是否已經(jīng)展示了足夠時間 static DateTime _startedTime; // 提示內(nèi)容 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, //文字水平方向的內(nèi)邊距 double pdHorizontal = 20.0, //文字垂直方向的內(nèi)邊距 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負(fù)責(zé)構(gòu)建布局 //通過OverlayEntry將構(gòu)建的布局插入到整個布局的最上層 _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, //目標(biāo)透明度 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, ), ), ), ), ); }// 設(shè)置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; }}
看完上述內(nèi)容,你們掌握flutterToast中怎么實(shí)現(xiàn)一個消息提示框的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
分享名稱:flutterToast中怎么實(shí)現(xiàn)一個消息提示框-創(chuàng)新互聯(lián)
轉(zhuǎn)載源于:http://aaarwkj.com/article20/jccjo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、品牌網(wǎng)站建設(shè)、電子商務(wù)、移動網(wǎng)站建設(shè)、Google、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容