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

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. junit源码之Runner

    Runner 定义了执行用例的执行器方法. public abstract class Runner implements Describable { /* 获取描述 */ public abstra ...

  2. 关于KeePass实现mstsc远程桌面(rdp协议)的自动登录

    本文的Keepass版本:KeePass Password Safe Version 2.45 首先介绍一下Keepass,引用官网的解释如下: KeePass is a free open sour ...

  3. Guava Cache详解

    适用性 缓存在很多场景下都是相当有用的.例如,计算或检索一个值的代价很高,并且对同样的输入需要不止一次获取值的时候,就应当考虑使用缓存 Guava Cache与ConcurrentMap很相似,但也不 ...

  4. springboot+mybatis集成分页功能

    1.使用idea搭建srpingboot项目 在pom.xml文件中引入如下的依赖: <dependency> <groupId>org.springframework.boo ...

  5. Clover 引导 Windows 及 Linux 双系统

    Clover 引导 Windows 及 Linux 双系统UEFI cnblogs @ Orcim  此 文比较详细地介绍了通过修改 Clover 的配置文件,添加 Clover 启动项的方法(添加 ...

  6. obj2opengl:转换OBJ 3D模型到iPhone OpenGL ES兼容的数组中

    原文如下:obj2opengl: convert obj 3D models to arrays compatible with iPhone OpenGL ES obj2opengl在GitHub中 ...

  7. matlab进行FIR滤波器设计(一)

    来源:https://blog.csdn.net/leokingszx/article/details/80041910 在实际的应用中,有时需要使用FIR根据完成一些特定功能,比如近似一阶RC低通电 ...

  8. 《穷查理年鉴》金钱 & 生意 & 律师(关于金钱)

    金钱 025.钱还得快才会借得快. 030.钱和人有着复杂的友谊:人能让钱变坏,钱也能让人变坏. 034.绝望增加债务,勤奋偿还债务. 037.只有一无所有的人才会没有烦恼. 049.穷人为他的胃找食 ...

  9. nginx完美支持thinkphp3.2.2(需配置URL_MODE=>3 rewrite兼容模式)

    来源:http://www.thinkphp.cn/topic/26637.html 环境:nginx 1.6,thinkphp3.2.2 第一步,修改server块 server { listen ...

  10. Azure Media Player Logo隐藏和 视频字幕样式

    <style type="text/css"> /**hide mediaplayer logo*/ .amp-default-skin .amp-content-ti ...