import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
class DialogView extends StatefulWidget {
const DialogView({super.key});
@override
State<DialogView> createState() => _DialogViewState();
}
class _DialogViewState extends State<DialogView> {
void _alertDialog() async {
var result = await showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text("提示信息1"),
content: Text("确认删除吗1?"),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop("quit"); //消失弹窗
},
child: Text("取消"),
),
TextButton(
onPressed: () {
Navigator.of(context).pop("ok");
},
child: Text("确定"),
),
],
);
},
);
print("弹窗$result");
}
void _simpleDialog() {
showDialog(
barrierDismissible: false, //表示点击灰色背景的时候是否消失弹出框
context: context,
builder: (context) {
return SimpleDialog(
title: Text("请选择语言"),
children: [
SimpleDialogOption(
onPressed: () {
print("汉语");
Navigator.of(context).pop();
},
child: Text("汉语"),
),
Divider(),
SimpleDialogOption(
onPressed: () {
print("日语");
Navigator.pop(context);
},
child: Text("日语"),
),
SimpleDialogOption(
onPressed: () {
print("应语");
Navigator.of(context).pop();
},
child: Text("应语"),
),
],
);
},
);
}
void _bottomSheetView() async {
var res = await showModalBottomSheet(
context: context,
builder: (context) {
return Container(
child: SizedBox(
height: 230,
child: Column(
crossAxisAlignment: .center,
children: [
ListTile(
title: Text("分享"),
onTap: () {
Navigator.pop(context, "分享");
},
),
Divider(),
ListTile(
title: Text("收藏"),
onTap: () {
Navigator.pop(context, "收藏");
},
),
Divider(),
ListTile(
title: Text("取消"),
onTap: () {
Navigator.pop(context, "取消");
},
),
],
),
),
);
},
);
print("最后返回$res");//难道最后点击的
}
void _toatView(){
Fluttertoast.showToast(
msg: "This is Center Short Toast",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 2,
backgroundColor: Colors.black,
textColor: Colors.white,
fontSize: 16.0
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("弹窗")),
body: Column(
children: [
ElevatedButton(
onPressed: () {
//AlertView
_alertDialog();
},
child: Text("AlertView"),
),
ElevatedButton(
onPressed: () {
// 弹出框,中间展示actionsheet
_simpleDialog();
},
child: Text("SimpleDialog"),
),
ElevatedButton(
onPressed: () {
//actionSheet
_bottomSheetView();
},
child: Text("actionSheet"),
),
ElevatedButton(
onPressed: () {
_toatView();
},
child: Text("actionSheet"),
),
],
),
);
}
}