// AlertDialog 、SimpleDialog、showModalBottomSheet、showToast
// 使用showToast安装插件 https://pub.dev/packages/fluttertoast
Fluttertoast.showToast( msg: "This is Short Toast", toastLength: Toast.LENGTH_SHORT, timeInSecForIos: 1);
// 改变弹窗内的值
StatefulBuilder(
builder: (context, changeState) {
return '内容'
},
)
// 点击背景消失
showDialog(
context: context,
builder: (context) {
return CupertinoDialog (
child: Text('zxcvbnm123456', style: TextStyle(fontSize: Screen.width(30), decoration: TextDecoration.none, color: Colors.black, fontWeight: FontWeight.normal)));
}
);
// 点击背景不消失
showCupertinoDialog(
context: context,
builder: (context) {
return CupertinoDialog (
child: Text('zxcvbnm123456', style: TextStyle(fontSize: Screen.width(30), decoration: TextDecoration.none, color: Colors.black, fontWeight: FontWeight.normal)));
}
);
import 'package:flutter/material.dart';
import 'myDialog.dart';
class DialogPage extends StatefulWidget{
DialogPage({Key key});
_Dialog createState() => _Dialog();
}
class _Dialog extends State {
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text('Dialog')
),
body: ListView(
children: <Widget>[
RaisedButton(
child: Text('AlertDialog'),
onPressed: () {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text('提示'),
content: Text('确定要取消吗'),
actions: <Widget>[
FlatButton(child: Text('取消'), onPressed: () {Navigator.pop(context, '222');},),
FlatButton(child: Text('确定'), onPressed: () {Navigator.pop(context, '222');},),
],
);
}
);
},
),
RaisedButton(
child: Text('弹出选择卡'),
onPressed: () {
showDialog(
context: context,
builder: (context) {
return SimpleDialog(
title: Text('选择卡'),
children: <Widget>[
SimpleDialogOption(child: Text('A'), onPressed: () { Navigator.pop(context);},),
SimpleDialogOption(child: Text('B'), onPressed: () { Navigator.pop(context);},),
SimpleDialogOption(child: Text('C'), onPressed: () { Navigator.pop(context);},),
],
);
}
);
},
),
RaisedButton(
child: Text('弹出自定义'),
onPressed: () {
showDialog(
context: context,
builder: (context) {
return MyDialog(title:"关于我们", content:"关于我们");
}
);
},
)
],
)
);
}
}
// 自定义dialog
import 'dart:async';
import 'package:flutter/material.dart';
//MyDialog(title:"关于我们", content:"关于我们"); 引入文件 然后调用方法
class MyDialog extends Dialog {
String title;
String content;
MyDialog({this.title="",this.content=""});
_showTimer(context){
var timer;
timer = Timer.periodic(
Duration(milliseconds: 3000), (t) {
print('关闭');
Navigator.pop(context);
t.cancel(); //取消定时器 timer.cancel();
});
}
@override
Widget build(BuildContext context) {
_showTimer(context);
// TODO: implement build
return Material(
type: MaterialType.transparency,
child: Center(
child: Container(
height: 300,
300,
color: Colors.white,
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.all(10),
child: Stack(
children: <Widget>[
Align(
alignment: Alignment.center,
child: Text("${this.title}"),
),
Align(
alignment: Alignment.centerRight,
child: InkWell(
child: Icon(Icons.close),
onTap: (){
Navigator.pop(context);
},
),
)
],
),
),
Divider(),
Container(
padding: EdgeInsets.all(10),
double.infinity,
child: Text("${this.content}",textAlign: TextAlign.left),
)
],
),
)),
);
}
}