Flutter 彈框6種

1、Flutter 項(xiàng)目默認(rèn)升級彈框和自定義升級彈框

注:在pubspec.yaml中添加 插件

#版本更新對話框

flutter_update_dialog: ^1.0.0

代碼如下:

import 'dart:async';

import 'package:flutter/material.dart';

import 'package:flutter_update_dialog/flutter_update_dialog.dart';

///

/// 版本更新對話框

///

class MyUpdateDialog {

static UpdateDialogdialog;

static doubleprogress =0.0;

? ///默認(rèn)樣式

? static defaultStyle(BuildContext context) {

????if (dialog !=null &&dialog.isShowing()) {

????????return;

? ? }

? dialog = UpdateDialog.showUpdate(context,

? ? ? ? title:"是否升級到4.1.4版本?",

? ? ? ? updateContent:"新版本大小:2.0M\n1.更新UI\n2.更新Bug\n3.增加新功能",

? ? ? ? onUpdate:onUpdate);

? }

///自定義樣式

? static customStyle(BuildContext context) {

????if (dialog !=null &&dialog.isShowing()) {

????????return;

? ? }

????dialog = UpdateDialog.showUpdate(context,

? ? ? ? width:250,

? ? ? ? title:"是否升級到4.1.4版本昧港?",

? ? ? ? updateContent:"新版本大小:2.0M\n1.更新UI\n2.更新Bug\n3.增加新功能",

? ? ? ? titleTextSize:14,

? ? ? ? contentTextSize:12,

? ? ? ? buttonTextSize:12,

? ? ? ? topImage:Image.asset('assets/images/image_top.png'),

? ? ? ? extraHeight:5,

? ? ? ? radius:8,

? ? ? ? themeColor:Color(0xFFFFAC5D),

? ? ? ? progressBackgroundColor:Color(0x5AFFAC5D),

? ? ? ? isForce:true,

? ? ? ? updateButtonText:'升級',

? ? ? ? ignoreButtonText:'忽略此版本',

? ? ? ? enableIgnore:true, onIgnore: () {

????????// ToastUtils.waring("忽略");

? ? ? ????dialog.dismiss();

? ? }, onUpdate:onUpdate);

? }

///更新方法

? static onUpdate() {

????// ToastUtils.success("開始升級...");

? ? Timer.periodic(Duration(milliseconds:50), (timer) {

? ? ? progress =progress +0.02;

? ? ? if (progress >1.0001) {

????????timer.cancel();

? ? ? ? dialog.dismiss();

? ? ? ? progress =0;

? ? ? ? // ToastUtils.success("升級成功召川!");

? ? ? }else {

????????dialog.update(progress);

? ? ? }

});

? }

}


調(diào)用:

///默認(rèn)版本更新對話彈框

MyUpdateDialog.defaultStyle(context);

///自定義 版本更新對話彈框

MyUpdateDialog.customStyle(context);


2.從底部彈窗

代碼如下:

AppTool? ?工具類

import 'package:flutter/cupertino.dart';

import 'package:flutter/material.dart';

import 'package:flutter/widgets.dart';

import 'package:flutter_app1/module/dialogs/widget/bottom_widget.dart';

import 'package:flutter_app1/module/dialogs/widget/center_dialog.dart';

import 'package:flutter_app1/module/dialogs/widget/define_widget.dart';

import 'package:flutter_app1/module/dialogs/widget/input_dialog.dart';

import 'package:flutter_app1/module/dialogs/widget/style_dialog.dart';

///

/// 自定義彈窗

///

///

class AppTool {

/// 底部彈出2個選項(xiàng)框

? showBottomAlert(BuildContext context, confirmCallback, String title,

? ? ? String option1, String option2) {

showCupertinoModalPopup(

context: context,

? ? ? ? builder: (context) {

return BottomCustomAlterWidget(

confirmCallback, title, option1, option2);

? ? ? ? });

? }

/// 中間彈出提示框

? showCenterTipsAlter(

BuildContext context, confirmCallback, String title, String desText) {

showDialog(

context: context,

? ? ? ? builder: (BuildContext context) {

return CenterTipsAlterWidget(confirmCallback, title, desText);

? ? ? ? });

? }

/// 中間彈出輸入框

? showCenterInputAlert(

BuildContext context, confirmCallback, String title, String placeholder) {

showDialog(

context: context,

? ? ? ? builder: (BuildContext context) {

return ShowInputAlertWidget(confirmCallback, title, placeholder);

? ? ? ? });

? }

///自定義彈框

? showStyleAlert(BuildContext context, confirmCallback, String title,

? ? ? String contentTitle) {

showDialog(

context: context,

? ? ? ? builder: (BuildContext context) {

return StyleDialog(confirmCallback, title, contentTitle);

? ? ? ? });

? }

///只有一個確定按鈕的彈窗

? showDefineAlert(

BuildContext context, confirmCallback, String title, String hintText) {

showDialog(

context: context,

? ? ? ? builder: (BuildContext context) {

return ShowDefineAlertWidget(confirmCallback, title, hintText);

? ? ? ? });

? }

}




底部彈窗Widget?

import 'package:flutter/cupertino.dart';

import 'package:flutter/widgets.dart';

///底部彈框

class BottomCustomAlterWidget extends StatefulWidget {

final confirmCallback;

? final title;

? final option1;

? final option2;

? const BottomCustomAlterWidget(this.confirmCallback, this.title, this.option1, this.option2);

? @override

? ? ?_BottomCustomAlterWidgetStatecreateState() =>

????_BottomCustomAlterWidgetState();

}

class _BottomCustomAlterWidgetStateextends State {

? final controller =TextEditingController();

? StringinputValuue ="";

? @override

? Widgetbuild(BuildContext context) {

return CupertinoActionSheet(

///底部彈出的提示框

? ? ? title:Text(

????????widget.title,

? ? ? ? style:TextStyle(fontSize:22),

? ? ? ),

? ? ? actions: [

????CupertinoActionSheetAction(

? ? onPressed: () {

? ? ? ? ? ? ? Navigator.pop(context);

? ? ? ? ? ? ? widget.confirmCallback(widget.option1);

? ? ? ? ? ? },

? ? ? ? ? ? child:Text(widget.option1)),

? ? ? ? CupertinoActionSheetAction(

????????????????onPressed: () {

????????????????Navigator.pop(context);

? ? ? ? ? ? ? ? widget.confirmCallback(widget.option2);

? ? ? ? ? ? },

? ? ? ? ? ? child:Text(widget.option2)),

? ? ? ],

? ? ? cancelButton:CupertinoActionSheetAction(

????????onPressed: () {

????????????Navigator.pop(context);

? ? ? ? },

? ? ? ? child:Text('取消'),

? ? ? ),

? ? );

? }

}

調(diào)用:

///從底部彈出彈框

AppTool().showBottomAlert(context, sexConfirmCallback, "請選擇性別", '男', '女');


3眠屎、警告/提示彈框

警告/彈窗Widget? ? CenterTipsAlterWidget

import 'package:flutter/cupertino.dart';

import 'package:flutter/widgets.dart';

///

/// 提示彈框

///

class CenterTipsAlterWidget? extends StatefulWidget {

final confirmCallback;

? final title;

? final desText;

? const CenterTipsAlterWidget(this.confirmCallback, this.title, this.desText);

? @override

? _CenterTipsAlterWidgetStatecreateState() =>_CenterTipsAlterWidgetState();

}

class _CenterTipsAlterWidgetStateextends State {

@override

? Widgetbuild(BuildContext context) {

///警告對話框

? ? return CupertinoAlertDialog(

? ? ? title:Text(widget.title),

? ? ? content:Column(

? ? ? ????children: [

????????????SizedBox(

????????????height:10,

? ? ? ? ? ),

? ? ? ? ? Align(

????????????child:Text(widget.desText),

? ? ? ? ? ? alignment:Alignment(0, 0),

? ? ? ? ? )

????????],

? ? ? ),

? ? ? actions: [

????????????CupertinoDialogAction(

????????????child:Text('取消'),

? ? ? ? ? ????onPressed: () {

????????????????????Navigator.pop(context);

? ? ? ? ? },

? ? ? ? ),

? ? ? ? CupertinoDialogAction(

? ? ? ? ? child:Text('確定'),

? ? ? ? ? onPressed: () {

????????????widget.confirmCallback('確定');

? ? ? ? ? ? Navigator.pop(context);

? ? ? ? ? },

? ? ? ? ),

? ? ? ],

? ? );

? }

}

調(diào)用:

///警告,提示對話框

AppTool().showCenterTipsAlter(context, centerConfirmCallback, "這是提示的標(biāo)題", '這是提示的文本');


4.輸入彈框

輸入彈框Widget??ShowInputAlertWidget


import 'package:flutter/cupertino.dart';

import 'package:flutter/widgets.dart';

///

/// 輸入提示框

///

class ShowInputAlertWidget extends StatefulWidget {

final confirmCallback;

? final title;

? final placeholder;

? const ShowInputAlertWidget(this.confirmCallback, this.title, this.placeholder);

? @override

? _ShowInputAlertWidgetStatecreateState() =>_ShowInputAlertWidgetState();

}

class _ShowInputAlertWidgetStateextends State {

?StringinputValue ='';

? @override

? Widgetbuild(BuildContext context) {

return CupertinoAlertDialog(

? ? ? title:Text(widget.title),

? ? ? content:Column(

? ? ? ?children: [

? ? ? ? ? ?CupertinoTextField(

? ? ? ? ? ? placeholder:widget.placeholder,

? ? ? ? ? ? onChanged: (value){

????????????????inputValue = value;

? ? ? ? ? ? },

? ? ? ? ? )

????],

? ? ? ),

? ? ? actions: [

? CupertinoDialogAction(

????????child:Text('取消'),

? ? ? ? ? onPressed: (){

????????????????Navigator.pop(context);

? ? ? ? ? },

? ? ? ? ),

? ? ? ? CupertinoDialogAction(

? ? ? ? ?child:Text('確定'),

? ? ? ? ? onPressed: (){

????????????????widget.confirmCallback(inputValue);

? ? ? ? ? },

? ? ? ? )

????],

? ? );

? }

}

調(diào)用:

AppTool().showCenterInputAlert(context, inputConfirmCallback, "請輸入昵稱", '請輸入昵稱');


5.自定義彈框

代碼如下? 自定義彈框??StyleDialog? Widget

import 'package:flutter/cupertino.dart';

import 'package:flutter/material.dart';

import 'package:flutter/widgets.dart';

///

/// 自定義彈窗

///

class StyleDialog extends StatefulWidget {

final confirmCallback;

? final title;

? final contentTitle;

? const StyleDialog(this.confirmCallback, this.title,this.contentTitle);

? @override

? _StyleDialogStatecreateState() =>_StyleDialogState();

}

class _StyleDialogStateextends State {

@override

? Widgetbuild(BuildContext context) {

????return SimpleDialog(

????titlePadding:EdgeInsets.only(top:15, bottom:5),

? ? ? title:Center(

????????child:Text(widget.title),

? ? ? ),

? ? ? backgroundColor: Colors.white,

? ? ? shape:RoundedRectangleBorder(borderRadius:BorderRadius.circular(10)),

? ? ? children: [

????????Divider(

????????????height:1,

? ? ? ? ),

? ? ? ? Container(

????????alignment: Alignment.center,

? ? ? ? ? height:80,

? ? ? ? ? child:Text(widget.contentTitle,

? ? ? ? ? ? ? style:TextStyle(fontSize:17, color: Colors.black)),

? ? ? ? ),

? ? ? ? Container(

????????????child:Row(

????????????????children: [

????????????????????Expanded(

????????????????????????flex:2,

? ? ? ? ? ? ? ? ? ? child:FlatButton(

????????????????????????onPressed: (){

????????????????????????Navigator.pop(context);

? ? ? ? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? ? ? ? child:Container(

????????????????????????alignment: Alignment.center,

? ? ? ? ? ? ? ? ? ? ? ? height:50,

? ? ? ? ? ? ? ? ? ? ? ? decoration:BoxDecoration(

????????????????????????borderRadius:BorderRadius.circular(10),

? ? ? ? ? ? ? ? ? ? ? ? ? color: Colors.white,

? ? ? ? ? ? ? ? ? ? ? ? ? border:Border.all(color:Color(0xffFF4B38), width:1),

? ? ? ? ? ? ? ? ? ? ? ? ),

? ? ? ? ? ? ? ? ? ? ? ? child:Text('否',

? ? ? ? ? ? ? ? ? ? ? ? ? ? style:TextStyle(

????????????????????????????color:Color(0xffFF4B38), fontSize:17)),

? ? ? ? ? ? ? ? ? ? ? ),

? ? ? ? ? ? ? ? ? ? ),

? ? ? ? ? ? ? ? ),

? ? ? ? ? ? ? ? Expanded(

????????????????????flex:3,

? ? ? ? ? ? ? ? ? ? child:FlatButton(

????????????????????????onPressed: (){

????????????????????????widget.confirmCallback('');

? ? ? ? ? ? ? ? ? ? ? ? Navigator.pop(context);

? ? ? ? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? ? ? ? ? child:Container(

? ? ? ? ? ? ? ? ? ? ? ? ?alignment: Alignment.center,

? ? ? ? ? ? ? ? ? ? ? ? ? height:50,

? ? ? ? ? ? ? ? ? ? ? ? ? decoration:BoxDecoration(

????????????????????????????borderRadius:BorderRadius.circular(10),

? ? ? ? ? ? ? ? ? ? ? ? ? ? color:Color(0xffFF4B38),

? ? ? ? ? ? ? ? ? ? ? ? ? ? // border: Border.all(color: Color(0xffFF4B38),width: 1),

? ? ? ? ? ? ? ? ? ? ? ? ? ),

? ? ? ? ? ? ? ? ? ? ? ? ? child:Text('是',

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? style:TextStyle(color: Colors.white, fontSize:17)),

? ? ? ? ? ? ? ? ? ? ? ? ))

????????)

????],

? ?))

],

? ? );

? }

}

調(diào)用:

///自定義彈框樣式

AppTool().showStyleAlert(context, styleConfirmCallback, "提示",'這里是取消關(guān)注的提示語…');

6、只有一個按鈕的彈框

代碼如下: 只有一個按鈕彈框? ?ShowDefineAlertWidget? ?Widget

import 'package:flutter/material.dart';

class ShowDefineAlertWidget extends StatefulWidget {

final confirmCallback;

? final title;

? final hintText;

? const ShowDefineAlertWidget(this.confirmCallback, this.title, this.hintText);

? @override

? _ShowDefineAlertWidgetStatecreateState() =>_ShowDefineAlertWidgetState();

}

class _ShowDefineAlertWidgetStateextends State {

@override

? Widgetbuild(BuildContext context) {

????/// 設(shè)置彈框的寬度為屏幕寬度的86%

? ? var _dialogWidth = MediaQuery.of(context).size.width *0.86;

? ? return SimpleDialog(

????????title:Column(

? ? ? children: [

? ? ? ? ? Padding(

? ? ? ? ? ? padding:EdgeInsets.only(bottom:10),

? ? ? ? ? ? child:Text(widget.title,

? ? ? ? ? ? ? ? style:TextStyle(

? ? ? ? ? ? ? ? ? color: Colors.black,

? ? ? ? ? ? ? ? ? ? fontSize:20,

? ? ? ? ? ? ? ? ? ? fontWeight: FontWeight.w100)),

? ? ? ? ? ),

? ? ? ? ? Text(widget.hintText,

? ? ? ? ? ? ? style:TextStyle(

????????????????color: Colors.black,

? ? ? ? ? ? ? ? ? fontSize:18,

? ? ? ? ? ? ? ? ? fontWeight: FontWeight.w100)),

? ? ? ? ],

? ? ? ),

? ? ? titlePadding:EdgeInsets.fromLTRB(10, 20, 10, 20),

? ? ? contentPadding: EdgeInsets.zero,

? ? ? children: [

????????Divider(

????????????height:1,

? ? ? ? ),

? ? ? ? FlatButton(

????????????onPressed: () {

? ? ? ? ? ? ? widget.confirmCallback('');

? ? ? ? ? ? ? Navigator.pop(context);

? ? ? ? ? ? },

? ? ? ? ? ? child:Container(

? ? ? ? ? ? ?width: _dialogWidth,

? ? ? ? ? ? ? height:40,

? ? ? ? ? ? ? alignment: Alignment.center,

? ? ? ? ? ? ? child:Text(

????????????????'確定',

? ? ? ? ? ? ? ? style:TextStyle(color: Colors.blue, fontSize:18),

? ? ? ? ? ? ? ),

? ? ? ? ? ? )),

? ? ? ],

? ? );

? }

}

調(diào)用:

AppTool().showDefineAlert(context, defineConfirmCallback, "溫馨提示", '登陸已過期贸营,請重新登陸');

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市岩睁,隨后出現(xiàn)的幾起案子钞脂,更是在濱河造成了極大的恐慌,老刑警劉巖捕儒,帶你破解...
    沈念sama閱讀 221,198評論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件冰啃,死亡現(xiàn)場離奇詭異,居然都是意外死亡刘莹,警方通過查閱死者的電腦和手機(jī)阎毅,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評論 3 398
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來点弯,“玉大人扇调,你說我怎么就攤上這事∏栏兀” “怎么了狼钮?”我有些...
    開封第一講書人閱讀 167,643評論 0 360
  • 文/不壞的土叔 我叫張陵碳柱,是天一觀的道長。 經(jīng)常有香客問我熬芜,道長莲镣,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,495評論 1 296
  • 正文 為了忘掉前任涎拉,我火速辦了婚禮瑞侮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘鼓拧。我一直安慰自己区岗,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,502評論 6 397
  • 文/花漫 我一把揭開白布毁枯。 她就那樣靜靜地躺著慈缔,像睡著了一般。 火紅的嫁衣襯著肌膚如雪种玛。 梳的紋絲不亂的頭發(fā)上藐鹤,一...
    開封第一講書人閱讀 52,156評論 1 308
  • 那天,我揣著相機(jī)與錄音赂韵,去河邊找鬼娱节。 笑死,一個胖子當(dāng)著我的面吹牛祭示,可吹牛的內(nèi)容都是我干的肄满。 我是一名探鬼主播,決...
    沈念sama閱讀 40,743評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼质涛,長吁一口氣:“原來是場噩夢啊……” “哼稠歉!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起汇陆,我...
    開封第一講書人閱讀 39,659評論 0 276
  • 序言:老撾萬榮一對情侶失蹤怒炸,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后毡代,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體阅羹,經(jīng)...
    沈念sama閱讀 46,200評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,282評論 3 340
  • 正文 我和宋清朗相戀三年教寂,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了捏鱼。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,424評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡酪耕,死狀恐怖导梆,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤问潭,帶...
    沈念sama閱讀 36,107評論 5 349
  • 正文 年R本政府宣布猿诸,位于F島的核電站,受9級特大地震影響狡忙,放射性物質(zhì)發(fā)生泄漏梳虽。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,789評論 3 333
  • 文/蒙蒙 一灾茁、第九天 我趴在偏房一處隱蔽的房頂上張望窜觉。 院中可真熱鬧,春花似錦北专、人聲如沸禀挫。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽语婴。三九已至,卻和暖如春驶睦,著一層夾襖步出監(jiān)牢的瞬間砰左,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評論 1 271
  • 我被黑心中介騙來泰國打工场航, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留缠导,地道東北人。 一個月前我還...
    沈念sama閱讀 48,798評論 3 376
  • 正文 我出身青樓溉痢,卻偏偏與公主長得像僻造,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子孩饼,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,435評論 2 359