Flutter中的弹框
一起从0开始学习Flutter!
在Flutter中我们也有着丰富的弹出框控件,Flutter给我们准备好了SimpleDialog,AlertDialog,AboutDialog,CupertinoAlertDialog这些弹出框,我们可以依次来认识它们。
在认识它们之前还需要先学习一下如果让这些组件显示在屏幕上,这些组件创建完之后是否直接显示的,我们还需要调用一个show方法,比如showDialog();showAboutDialog();showCupertinoDialog();这样的显示方法。
Show方法
show方法是根据想要显示的对话框的样式不同进行了封装,showDialog可以放下我们想要的所有dialog,只是针对不同的dialog会有时比较繁琐,我们先看下showDialog方法中需要传入哪些参数:
Future<T> showDialog<T>({
@required BuildContext context, //传入上下文
bool barrierDismissible = true, //点击遮罩层是否对话框消失
@Deprecated(
'Instead of using the "child" argument, return the child from a closure '
'provided to the "builder" argument. This will ensure that the BuildContext '
'is appropriate for widgets built in the dialog. '
'This feature was deprecated after v0.2.3.'
)
Widget child,//过时了,不再使用
WidgetBuilder builder, //需要构建的对话框
bool useRootNavigator = true, //是否将dialog放到根的Navigator中,默认是添加的
})
如果我们想要一个AboutDialog那么我们可以直接使用showAboutDialog()方法来直接创建一个AboutDialog。AboutDialog我们一般用来显示应用的基本信息,用的比较少,我们看下showAboutDialog():
void showAboutDialog({
@required BuildContext context,//传入上下文
String applicationName,//传入应用的名字
String applicationVersion,//应用的版本号
Widget applicationIcon,//应用的图标
String applicationLegalese,//添加一些应用的文字说明
List<Widget> children,//添加一些自定义的子控件,根据大家的需要自行补充就行
bool useRootNavigator = true,//是否将dialog放到根的Navigator中,默认是添加的
})
如果我们想要创建一个iOS风格的dialog,那么我们可以使用showCupertinoDialog();来直接创建,可以看下showCupertinoDialog():
Future<T>