前言

对话框本质上也是UI布局,通常一个对话框会包含标题、内容,以及一些操作按钮,为此,Material库中提供了一些现成的对话框组件来用于快速的构建出一个完整的对话框。

接口描述

// 1. AlertDialog

 const AlertDialog({
Key key,
// 对话框组件标题
this.title,
// 标题填充
this.titlePadding,
// 标题文本样式
this.titleTextStyle,
// 对话框内容组件
this.content,
// 内容的填充
this.contentPadding = const EdgeInsets.fromLTRB(24.0, 20.0, 24.0, 24.0),
// 内容文本样式
this.contentTextStyle,
// 对话框操作按钮组
this.actions,
// 对话框背景色
this.backgroundColor,
// 对话框的阴影
this.elevation,
// 对话框语义化标签(用于读屏软件)
this.semanticLabel,
// 对话框外形
this.shape,
}) : assert(contentPadding != null),
super(key: key);

代码示例

// 对话框详解(dialog)

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart'; // 1. AlertDialog,消息对话框
Future<bool> showDeleteConfirmDialog(context){
return showDialog<bool>(
context: context,
//点击对话框barrier(遮罩)时是否关闭它
barrierDismissible: false,
builder: (context){
return AlertDialog(
title: Text("提示"),
content: Text("您确定要删除当前文件吗?"),
actions: <Widget>[
FlatButton(
child: Text("取消"),
// 关闭对话框
onPressed: () => Navigator.of(context).pop(),
),
FlatButton(
child: Text("确定"),
onPressed: (){
// 关闭对话框并返回true
Navigator.of(context).pop(true);
},
),
],
);
}
);
} // 2. SimpleDialog,列表对话框
Future<void> changeLanguageDialog(context) async{
int i = await showDialog<int>(
context: context,
builder: (context){
return SimpleDialog(
title: const Text("请选择语言"),
children: <Widget>[
SimpleDialogOption(
onPressed: (){
// 返回1
Navigator.pop(context, 1);
},
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 0),
child: const Text("中文简体"),
),
), SimpleDialogOption(
onPressed: (){
// 返回2
Navigator.pop(context, 2);
},
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 6),
child: const Text("美国英语"),
),
),
],
);
}
);
if(i != null){
print("选择了:${i == 1 ? "中文简体" : "美国英语"}");
}
} // 3. Dialog,对话框
Future<void> showListDialog(context) async{
int index = await showDialog(
context: context,
builder: (context){
var child = Column(
children: <Widget>[
ListTile(title: Text("请选择"),),
Expanded(
child: ListView.builder(
itemCount: 30,
itemBuilder: (BuildContext context, int index){
return ListTile(
title: Text("$index"),
onTap: () => Navigator.of(context).pop(index),
);
},
),
)
],
);
//使用AlertDialog会报错
// 实际上AlertDialog和SimpleDialog都使用了Dialog类。由于AlertDialog和SimpleDialog中使用了IntrinsicWidth来尝试通过子组件的实际尺寸来调整自身尺寸,
// 这就导致他们的子组件不能是延迟加载模型的组件(如ListView、GridView 、 CustomScrollView等)
// return AlertDialog(content: child);
return Dialog(child: child);
}
);
if (index != null) {
print("点击了:$index");
}
} // 4. showGeneralDialog,自定义非Material风格对话框
Future<T> customDialog<T>({
@required BuildContext context,
bool barrierDismissible = true,
WidgetBuilder builder,
}){
final ThemeData theme = Theme.of(context, shadowThemeOnly: true);
//
return showGeneralDialog(
context: context,
pageBuilder: (BuildContext buildContext, Animation<double> animation, Animation<double> secondaryAnimation){
final Widget pageChild = Builder(builder: builder,);
return SafeArea(
child: Builder(builder: (BuildContext context){
return theme != null
? Theme(data: theme, child: pageChild)
: pageChild;
}),
);
},
barrierDismissible: barrierDismissible,
barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
// 自定义遮罩颜色
barrierColor: Colors.black87,
transitionDuration: const Duration(milliseconds: 150),
transitionBuilder: _buildMaterialDialogTransitions, );
} Widget _buildMaterialDialogTransitions(
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
Widget child){
// 使用缩放动画
return ScaleTransition(
scale: CurvedAnimation(
parent: animation,
curve: Curves.easeOut,
),
child: child,
);
} Future<bool> showCustomDialog(context){
return customDialog<bool>(
context: context,
//点击对话框barrier(遮罩)时是否关闭它
barrierDismissible: false,
builder: (context){
return AlertDialog(
title: Text("提示"),
content: Text("您确定要删除当前文件吗?"),
actions: <Widget>[
FlatButton(
child: Text("取消"),
// 关闭对话框
onPressed: () => Navigator.of(context).pop(),
),
FlatButton(
child: Text("确定"),
onPressed: (){
// 关闭对话框并返回true
Navigator.of(context).pop(true);
},
),
],
);
}
);
} // 5. 对话框状态管理
Future<bool> showDeleteConfirmDialog1(context) {
bool _withTree = false;
return showDialog<bool>(
context: context,
builder: (context){
return AlertDialog(
title: Text("提示"),
content: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text("您确定要删除当前文件吗?"),
Row(
children: <Widget>[
Text("同时删除子目录"),
// Checkbox(
// // 使用Checkbox组件
// value: _withTree,
// onChanged: (bool value){
// // 此时context为对话框UI的根Element,我们
// // 直接将对话框UI对应的Element标记为dirty
// (context as Element).markNeedsBuild();
// _withTree = !_withTree;
// },
// )
// 通过Builder来获得构建Checkbox的`context`,
// 这是一种常用的缩小`context`范围的方式
Builder(
builder: (BuildContext context) {
return Checkbox(
value: _withTree,
onChanged: (bool value) {
(context as Element).markNeedsBuild();
_withTree = !_withTree;
},
);
},
),
],
)
],
),
actions: <Widget>[
FlatButton(
child: Text("取消"),
onPressed: () => Navigator.of(context).pop(),
),
FlatButton(
child: Text("删除"),
onPressed: (){
// 执行删除操作
Navigator.of(context).pop(_withTree);
},
)
],
);
}
);
} // 6. 底部菜单列表
Future<int> _showModalBottomSheet(context){
return showModalBottomSheet<int>(
context: context,
builder: (BuildContext context){
return ListView.builder(
itemCount: 30,
itemBuilder: (BuildContext context, int index){
return ListTile(
title: Text("$index"),
onTap: () => Navigator.of(context).pop(index),
);
}
);
}
);
} // 7. 全屏菜单列表
PersistentBottomSheetController<int> _showBottomSheet(context){
return showBottomSheet<int>(
context: context,
builder: (BuildContext context){
return ListView.builder(
itemCount: 30,
itemBuilder: (BuildContext context, int index){
return ListTile(
title: Text("$index"),
onTap: (){
print("$index");
Navigator.of(context).pop();
}
);
}
);
}
);
} // 8. Loading框,通过showDialog+AlertDialog实现
showLoadingDialog(context) {
showDialog(
context: context,
// 点击遮罩不关闭对话框
barrierDismissible: false,
builder: (context) {
return AlertDialog(
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
CircularProgressIndicator(),
Padding(
padding: const EdgeInsets.only(top: 26),
child: Text("正在加载,请稍后..."),
)
],
),
);
}
);
} // 9. 自定义对话框长度
// 只使用SizedBox或ConstrainedBox是不行的,原因是showDialog中已经给对话框设置了宽度限制,可以使用UnconstrainedBox先抵消showDialog对宽度的限制,然后再使用SizedBox指定宽度。
showCustomLoadingDialog(context) {
showDialog(
context: context,
barrierDismissible: false,
builder: (context) {
return UnconstrainedBox(
constrainedAxis: Axis.vertical,
child: SizedBox(
width: 280,
child: AlertDialog(
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
CircularProgressIndicator(),
Padding(
padding: const EdgeInsets.only(top: 26.0),
child: Text("正在加载,请稍后..."),
)
],
),
),
),
);
}
);
} // 10. Material风格的日历选择器
Future<DateTime> _showDatePicker(context) {
var date = DateTime.now();
return showDatePicker(
context: context,
initialDate: date,
firstDate: date,
lastDate: date.add(
// 未来30天可选
Duration(days: 30),
)
);
} // 11. iOS风格的日历选择器
Future<DateTime> _showDatePicker2(context) {
var date = DateTime.now();
return showCupertinoModalPopup(
context: context,
builder: (ctx) {
return SizedBox(
height: 200,
child: CupertinoDatePicker(
mode: CupertinoDatePickerMode.dateAndTime,
minimumDate: date,
maximumDate: date.add(
Duration(days: 30),
),
maximumYear: date.year + 1,
onDateTimeChanged: (DateTime value) {
print(value);
},
),
);
},
);
} class DialogWidgetRoute extends StatefulWidget{
_DialogWidgetRouteState createState() => _DialogWidgetRouteState();
} class _DialogWidgetRouteState extends State<DialogWidgetRoute>{
@override
Widget build(BuildContext context){
return Scaffold(
appBar: AppBar(
title: Text("对话框"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[ // AlertDialog
FlatButton(
color: Colors.green,
highlightColor: Colors.green,
splashColor: Colors.red,
child: Text("AlertDialog"),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0)),
onPressed: () async{
// 弹出对话框并等待其关闭
bool delete = await showDeleteConfirmDialog(context);
if(delete == null){
print("取消删除");
} else{
print("确认删除");
}
},
), // SimpleDialog
FlatButton(
color: Colors.green,
highlightColor: Colors.green,
splashColor: Colors.red,
child: Text("SimpleDialog"),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0)),
onPressed: () async{
changeLanguageDialog(context);
},
), // Dialog
FlatButton(
color: Colors.green,
highlightColor: Colors.green,
splashColor: Colors.red,
child: Text("Dialog"),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0)),
onPressed: () async{
showListDialog(context);
},
), // customDialog
FlatButton(
color: Colors.green,
highlightColor: Colors.green,
splashColor: Colors.red,
child: Text("customDialog"),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0)),
onPressed: () async{
// 弹出对话框并等待其关闭
bool delete = await showCustomDialog(context);
if(delete == null){
print("自定义取消删除");
} else{
print("自定义确认删除");
}
},
), // 对话框状态管理
FlatButton(
color: Colors.green,
highlightColor: Colors.green,
splashColor: Colors.red,
child: Text("对话框状态管理"),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0)),
onPressed: () async{
// 弹出对话框并等待其关闭
bool delete = await showDeleteConfirmDialog1(context);
if(delete == null){
print("取消删除");
} else{
print("确认删除");
}
},
), // 底部菜单列表
RaisedButton(
child: Text("底部菜单列表"),
onPressed: () async{
int type = await _showModalBottomSheet(context);
print(type);
},
), // Loading框
RaisedButton(
child: Text("Loading框"),
onPressed: () async{
showLoadingDialog(context);
},
), // 自定义对话框长度
RaisedButton(
child: Text("自定义对话框长度"),
onPressed: () async{
showCustomLoadingDialog(context);
},
), // Material风格的日历选择器
RaisedButton(
child: Text("Material风格的日历选择器"),
onPressed: () async{
_showDatePicker(context);
},
), // iOS风格的日历选择器
RaisedButton(
child: Text("iOS风格的日历选择器"),
onPressed: () async{
_showDatePicker2(context);
},
), ],
),
),
);
}
}

总结

对话框最终都是由showGeneralDialog方法打开的,直接调用Navigator的push方法打开了一个新的对话框路由_DialogRoute,然后返回了push的返回值。可见对话框实际上正是通过路由的形式实现的,这也是为什么我们可以使用Navigator的pop 方法来退出对话框的原因。

【Flutter】功能型组件之对话框详解的更多相关文章

  1. vc中调用Com组件的方法详解

    vc中调用Com组件的方法详解 转载自:网络,来源未知,如有知晓者请告知我.需求:1.创建myCom.dll,该COM只有一个组件,两个接口:   IGetRes--方法Hello(),   IGet ...

  2. iOS 组件化流程详解(git创建流程)

    [链接]组件化流程详解(一)https://www.jianshu.com/p/2deca619ff7e

  3. React—组件生命周期详解

    React—组件生命周期详解 转自 明明的博客  http://blog.csdn.net/slandove/article/details/50748473 (非原创) 版权声明:转载请注明出处,欢 ...

  4. Vue组件通信方式全面详解

    vue组件通信方式全面详解 众所周知,Vue主要思想就是组件化开发.因为,在实际的项目开发中,肯定会以组件的开发模式进行.形如页面和页面之间需要通信一样,Vue 组件和组件之间肯定也需要互通有无.共享 ...

  5. Kubernetes学习之路(二十)之K8S组件运行原理详解总结

    目录 一.看图说K8S 二.K8S的概念和术语 三.K8S集群组件 1.Master组件 2.Node组件 3.核心附件 四.K8S的网络模型 五.Kubernetes的核心对象详解 1.Pod资源对 ...

  6. 【转载】BootStrap表格组件bootstrap table详解

    (转载,来源“脚本之家”,作者不详) 一.Bootstrap Table的引入 关于Bootstrap Table的引入,一般来说还是两种方法: 1.直接下载源码,添加到项目里面来.由于Bootstr ...

  7. 中间件:ElasticSearch组件RestHighLevelClient用法详解

    本文源码:GitHub·点这里 || GitEE·点这里 一.基础API简介 1.RestHighLevelClient RestHighLevelClient的API作为ElasticSearch备 ...

  8. Vue基础语法-数据绑定、事件处理和扩展组件等知识详解(案例分析,简单易懂,附源码)

    前言: 本篇文章主要讲解了Vue实例对象的创建.常用内置指令的使用.自定义组件的创建.生命周期(钩子函数)等.以及个人的心得体会,汇集成本篇文章,作为自己对Vue基础知识入门级的总结与笔记. 其中介绍 ...

  9. 【Android】事件总线(解耦组件) EventBus 详解

    当Android项目越来越庞大的时候,应用的各个部件之间的通信变得越来越复杂,例如:当某一条件发生时,应用中有几个部件对这个消息感兴趣,那么我们通常采用的就是观察者模式,使用观察者模式有一个弊病就是部 ...

随机推荐

  1. Redis5 压力测试结果反馈报告

    Redis 相信很多人都用过了,关于性能啥的,网上一堆报告,闲得蛋痛,又随便测测写写一些狗屁文章,来刷存在感了. 安装最新Redis5.0.10 Redis 官方地址 下载页默认是redis6.0,5 ...

  2. 将命令行提示符里的执行结果导出到text文件中

    为便于查看和保存命令行提示符里的执行结果, 可以使用 ">" 将执行结果导入到指定.txt文件中. 例如: 在命令行提示符里查看C盘文件,并将结果导入到E盘dir-c-out ...

  3. .NET Core +Angular 项目 部署到CentOS

    前言: 最近公司需要开发项目能在Linux系统上运行,示例开发项目采用.Net Core + Angular开发:理论上完全支持跨平台. 但是实践才是检验真理的唯一标准:那么还是动手来验证实现下:过程 ...

  4. 思想无语言边界:以 cglib 介绍 AOP 在 java 的一个实现方式

    0. 前言 上接:基于 Source Generators 做个 AOP 静态编织小实验 作为第三篇,我们基于cglib在java做一个简单的aop例子, 以此简单作为例子说一个思路在什么样的语言里面 ...

  5. IDEA的基本操作——导入导出jar包

    在使用Jmeter工具测试时,有时也需要导出jar包,测试对应功能,或者自己二次开发Jmeter工具,也是需要导出jar包的.既然经常用,所以就总结了下导入导出jar包的方法. 导入jar包 先打开i ...

  6. 使用docker-maven-plugin打包

    今天在部署的时候遇到点问题,总结一下,docker部署的步骤,如果对您有帮助,关注一下,就是对我最大的肯定, 谢谢! 微服务部署有两种方法: (1)手动部署:首先基于源码打包生成jar包(或war包) ...

  7. 不一样的资产安全 3D 可视化平台

    前言   数字经济时代,应用好数据是企业数字化转型的关键,基于前沿科学技术进行数据的有效管控,更是对数字增值服务的新趋势.近年来,整个安全行业对资产管理的重视程度正在提高.据IDC发布的相关数据显示, ...

  8. 关于新创公司所需的icp,网文,软著和备案的申请

    刚从一个集团离职来到了创业团队,前期是什么都没有,甚至是公司名字都不知道,哈哈.所以就有了后面的坑踩了一遍又一遍.刚开始是在霍尔果斯注册,结果办icp费了半年的时间,东找西找还没下证.又碰上新疆严查不 ...

  9. 基于LNMP架构搭建wordpress博客之安装架构说明

    架构情况 架构情况:基于LNMP架构搭建wordpress系统 软件包版本说明: 系统要求 :  CentOS-6.9-x86_64-bin-DVD1.iso PHP版本  :  php-7.2.29 ...

  10. 如何查看打印机的IP地址和MAC地址

    1.  打开控制面板,选择设备和打印机: 2.  选中打印机,右键单机,选择打印机 "属性": 3. 选择web服务,可以直接查看打印机的IP地址或MAC地址,如下图所示: 4. ...