------------恢复内容开始------------

Flutter - 自定义Dialog弹窗

应用场景:app系统版本升级弹窗,系统退出登录弹窗,首页广告弹窗,消息中心弹窗,删除文件弹窗等等各种应用场景中,我们开发中都会遇到此情形。

废话不多话,先看效果图如下:(以上场景中代码逻辑都差不多,源代码自行修改即可!!!)------这里仅展示退出登录场景

逻辑其实很简单,重写Dialog类即可。

逻辑代码如下:

import 'package:flutter/material.dart';

class DialogWidget extends Dialog {
final String title; //标题
final String content; //内容
final String cancelText; //是否需要"取消"按钮
final String confirmText; //是否需要"确定"按钮
final Function cancelFun; //取消回调
final Function confirmFun; //确定回调 DialogWidget({
Key key,
@required this.title,
@required this.content,
this.cancelText,
this.confirmText,
@required this.cancelFun,
this.confirmFun
}) : super(key: key); @override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.all(15),
child: Material(
type: MaterialType.transparency,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
decoration: ShapeDecoration(
color: Color(0xfff2f2f2),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(10),
),
),
),
margin: EdgeInsets.all(15),
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.all(10),
child: Center(
child: Text(title, style: TextStyle(color: Color(0xff666666))),
),
),
Container(
color: Color(0xffffffff),
height: 1.0,
),
Container(
constraints: BoxConstraints(minHeight: 100),
child: Padding(
padding: const EdgeInsets.all(12.0),
child: IntrinsicHeight(
child: Text(title, style: TextStyle(color: Color(0xff666666))),
),
),
),
Container(
color: Color(0xffeeeeee),
height: 1.0,
),
this._buildBottomButtonGroup()
],
),
)
],
),
),
);
} Widget _buildBottomButtonGroup() {
var widgets = <Widget>[];
if (cancelText != null && cancelText.isNotEmpty) widgets.add(_buildBottomCancelButton());
if (confirmText != null && confirmText.isNotEmpty && confirmText != null && confirmText.isNotEmpty) widgets.add(_buildBottomOnline());
if (confirmText != null && confirmText.isNotEmpty) widgets.add(_buildBottomPositiveButton()); return Flex(
direction: Axis.horizontal,
children: widgets,
);
} Widget _buildBottomOnline() {
return Container(
color: Color(0xffeeeeee),
height: 38,
width: 1,
);
} Widget _buildBottomCancelButton() {
return Flexible(
fit: FlexFit.tight,
child: FlatButton(
onPressed: this.cancelFun,
child: Text(title, style: TextStyle(color: Color(0xff666666))),
),
);
} Widget _buildBottomPositiveButton() {
return Flexible(
fit: FlexFit.tight,
child: FlatButton(
onPressed: this.confirmFun,
child: Text(title, style: TextStyle(color: Color(0xff666666))),
),
);
}
}

代码中实际的效果可能与上面附图可能不一样, 但是逻辑是一样的。(如背景颜色字体等等,自行配置)

下面该如何使用调用呢?

点击"退出登录"按钮:

FlatButton(
child: Text("退出登录"),
onPressed: logOut, //退出登录方法
)

  

方法:

// 退出登录
void logOut(){
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context){
return DialogWidget(
title: '提示',
content: '确定要退出吗?',
cancelText: '取消',
confirmText: '确定',
cancelFun: (){
Navigator.pop(context);
},
confirmFun: (){
Navigator.pop(context);
Navigator.pop(context);
Provider.of<UserModel>(context).clearUserInfo();
},
);
}
);
}

到这里基本结束了,希望大家学以致用,举一反三!!!

------------恢复内容结束------------

Flutter - 自定义Dialog弹窗的更多相关文章

  1. 30 Flutter自定义Dialog

    MyDialog.dart import 'dart:async'; import 'package:flutter/material.dart'; class MyDialog extends Di ...

  2. Flutter学习笔记(41)--自定义Dialog实现版本更新弹窗

    如需转载,请注明出处:Flutter学习笔记(41)--自定义Dialog实现版本更新弹窗 功能点: 1.更新弹窗UI 2.强更与非强更且别控制 3.屏蔽物理返回键(因为强更的时候点击返回键,弹窗会消 ...

  3. 自定义Dialog布局的弹窗功能的简单实现

    package com.loaderman.dialogdemo; import android.os.Bundle; import android.support.v7.app.AlertDialo ...

  4. AlertDialog 、SimpleDialog、 showModalBottomSheet、showToast 自定义 Dialog

    // AlertDialog .SimpleDialog.showModalBottomSheet.showToast // 使用showToast安装插件 https://pub.dev/packa ...

  5. Android自定义Dialog

    Android开发过程中,常常会遇到一些需求场景——在界面上弹出一个弹框,对用户进行提醒并让用户进行某些选择性的操作, 如退出登录时的弹窗,让用户选择“退出”还是“取消”等操作. Android系统提 ...

  6. 非自定义和自定义Dialog的介绍!!!

    一.非自定义Dialog的几种形式介绍 转自:http://www.kwstu.com/ArticleView/kwstu_20139682354515 前言 对话框对于应用也是必不可少的一个组件,在 ...

  7. Android—自定义Dialog

    在 Android 日常的开发中,Dialog 使用是比较广泛的.无论是提示一个提示语,还是确认信息,还是有一定交互的(弹出验证码,输入账号密码登录等等)对话框. 而我们去看一下原生的对话框,虽然随着 ...

  8. Android自定义Dialog(美化界面)

    前言:在做项目的时候,发现dialog界面太丑陋,从csdn上下载了一份自定义dialog的源码,在他的基础上对界面进行美化...有需要的朋友可以直接拿走 效果图如下: 主要代码: /** * 自定义 ...

  9. 通用的Dialog自定义dialog

    图样:

随机推荐

  1. 栈帧的内部结构--动态链接 (Dynamic Linking)

    每个栈帧中包含: 局部变量表(Local Variables) 操作数栈(Opreand Stack) 或表达式栈 动态链接 (Dynamic Linking) (或指向运行时常量的方法引用) 动态返 ...

  2. pwnable.kr-cmd2-witeup

    程序清除了环境变量,解决对策是使用绝对地址使用各种命令和文件. 程序的过滤策略更加严格,过滤了/.天哪,使用绝对路径必用/,怎么办咩? 解决办法是使用pwd变量,它会显示当前目录,然而在/目录下执行此 ...

  3. CCNP七层参考模型

    一.OSI七层参考模型 七层参考模型由ISO组织提出,为什么是参考模型呢?因为我们现在实际应用的是TCP/IP协议栈,OSI模型仅供学习参考,下面具体说一下有哪七层: (7)应用层:应用程序和服务功能 ...

  4. Hbuilder给手机发送短信与拨打电话

    前言:业务场景 需要给手机号码拨打电话与发送短信.html5已经提供接口. methods: { Call: function() { var that = this; if(that.ptel == ...

  5. JVM系列【6】GC与调优1

    JVM系列笔记目录 虚拟机的基础概念 class文件结构 class文件加载过程 jvm内存模型 JVM常用指令 GC与调优 GC基础知识 什么是垃圾 ​ 没有任何引用指向的一个对象或多个对象(循环引 ...

  6. MeteoInfoLab脚本示例:地图投影

    在用axesm函数创建地图坐标系的时候可以指定地图投影(设置projinfo参数),地图投影可以通过projinfo函数来创建,里面的参数依据proj4投影字符串,可以参考此网页:http://rem ...

  7. 物联网wifi模块

    物联网wifi模块 物联网wifi模块 是上海卓岚推出的MQTT+JSON转Modbus物联网WiFi核心模块.支持以MQTT的方式连接云端服务器,支持可以界面话配置,自主采集Modbus仪表/645 ...

  8. 【思维】Luogu P3941 入阵曲

    题目大意 洛谷链接 给出一个矩阵和 \(K\) ,问有多少子矩阵中的元素和能整除 \(K\). 数据范围 \(2\leq n,m\leq 400\),\(0\leq K\leq 10^6\). 思路 ...

  9. 【贪心算法】HDU 5969 最大的位或

    题目内容 Vjudge链接 给出一个闭区间,找该区间内两个数,使这两个数的按位或最大. 输入格式 包含至多\(10001\)组测试数据. 第一行有一个正整数,表示数据的组数. 接下来每一行表示一组数据 ...

  10. ImageMagick实现图片的旋转/翻转/裁剪(ImageMagick6.9.10)

    一,imagemagick的安装 请参见: https://www.cnblogs.com/architectforest/p/12807514.html 说明:刘宏缔的架构森林是一个专注架构的博客, ...