2024 最新 Flutter 教學 - Flutter 終極指南: 連結
從零開始學 Dart 程式設計: 連結
Flutter 程式設計入門實戰 30 天: 連結
哈囉~大家好,我是 KT ,今天【iT邦幫忙鐵人賽】挑戰第十九天,KT 將為大家來介紹,對話框視窗。
AlertDialog 訊息對話框視窗,用來顯示訊息提示用戶。
注意事項:
需要搭配 async、await 和 Future 非同步處理接收回傳資料。
AlertDialog(
title: Text('測試標題'),
content: const Text('測試內容.....'),
actions: <Widget>[
FlatButton(
child: Text('確定'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('HKT線上教室'),
),
body: HomePage(),
),
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: RaisedButton(
child: Text('顯示對話視窗訊息'),
onPressed: () {
showAlert(context);
},
),
);
}
}
Future<void> showAlert(BuildContext context) {
return showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('測試標題'),
content: const Text('測試內容.....'),
actions: <Widget>[
FlatButton(
child: Text('確定'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
enum ConfirmAction { ACCEPT, CANCEL }
Future<ConfirmAction> confirmDialog(BuildContext context) async {
return showDialog<ConfirmAction>(
context: context,
barrierDismissible: false, //控制點擊對話框以外的區域是否隱藏對話框
builder: (BuildContext context) {
return AlertDialog(
title: Text('確認取消對話視窗'),
content: const Text('內容訊息'),
actions: <Widget>[
FlatButton(
child: const Text('確認'),
onPressed: () {
Navigator.of(context).pop(ConfirmAction.ACCEPT);
},
),
FlatButton(
child: const Text('取消'),
onPressed: () {
Navigator.of(context).pop(ConfirmAction.CANCEL);
},
)
],
);
},
);
}
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('HKT線上教室'),
),
body: HomePage(),
),
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: RaisedButton(
child: Text('顯示對話視窗訊息'),
onPressed: () async {
final ConfirmAction action = await confirmDialog(context);
print("你選擇:$action");
},
),
);
}
}
enum ConfirmAction { ACCEPT, CANCEL }
Future<ConfirmAction> confirmDialog(BuildContext context) async {
return showDialog<ConfirmAction>(
context: context,
barrierDismissible: false, //控制點擊對話框以外的區域是否隱藏對話框
builder: (BuildContext context) {
return AlertDialog(
title: Text('確認取消對話視窗'),
content: const Text('內容訊息'),
actions: <Widget>[
FlatButton(
child: const Text('確認'),
onPressed: () {
Navigator.of(context).pop(ConfirmAction.ACCEPT);
},
),
FlatButton(
child: const Text('取消'),
onPressed: () {
Navigator.of(context).pop(ConfirmAction.CANCEL);
},
)
],
);
},
);
}
此範例即可以了解,可以在 AlertDialog 的 content 中,加入自定義佈局。
Future<String> inputDialog(BuildContext context) async {
String inputData = '';
return showDialog<String>(
context: context,
barrierDismissible: false, //控制點擊對話框以外的區域是否隱藏對話框
builder: (BuildContext context) {
return AlertDialog(
title: Text('帶輸入框訊息視窗'),
content: new Row(
children: <Widget>[
new Expanded(
child: new TextField(
autofocus: true,
decoration: new InputDecoration(
labelText: '測試可輸入資料對話視窗', hintText: '請輸入資料...'),
onChanged: (value) {
inputData = value;
},
))
],
),
actions: <Widget>[
FlatButton(
child: Text('送出'),
onPressed: () {
Navigator.of(context).pop(inputData);
},
),
],
);
},
);
}
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('HKT線上教室'),
),
body: HomePage(),
),
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: RaisedButton(
child: Text('顯示對話視窗訊息'),
onPressed: () async {
final String inputData = await inputDialog(context);
print("你輸入:$inputData");
},
),
);
}
}
Future<String> inputDialog(BuildContext context) async {
String inputData = '';
return showDialog<String>(
context: context,
barrierDismissible: false, //控制點擊對話框以外的區域是否隱藏對話框
builder: (BuildContext context) {
return AlertDialog(
title: Text('帶輸入框訊息視窗'),
content: new Row(
children: <Widget>[
new Expanded(
child: new TextField(
autofocus: true,
decoration: new InputDecoration(
labelText: '測試可輸入資料對話視窗', hintText: '請輸入資料...'),
onChanged: (value) {
inputData = value;
},
))
],
),
actions: <Widget>[
FlatButton(
child: Text('送出'),
onPressed: () {
Navigator.of(context).pop(inputData);
},
),
],
);
},
);
}
使用 SimpleDialog 來實作多選項訊息視窗
enum OptionDatas { Java, Python, Swift }
Future<OptionDatas> optionDialog(BuildContext context) async {
return await showDialog<OptionDatas>(
context: context,
barrierDismissible: true,
builder: (BuildContext context) {
return SimpleDialog(
title: const Text('想要報名的課程'),
children: <Widget>[
SimpleDialogOption(
onPressed: () {
Navigator.pop(context, OptionDatas.Java);
},
child: const Text('Java'),
),
SimpleDialogOption(
onPressed: () {
Navigator.pop(context, OptionDatas.Python);
},
child: const Text('Python'),
),
SimpleDialogOption(
onPressed: () {
Navigator.pop(context, OptionDatas.Swift);
},
child: const Text('Swift'),
),
],
);
});
}
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('HKT線上教室'),
),
body: HomePage(),
),
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: RaisedButton(
child: Text('顯示對話視窗訊息'),
onPressed: () async {
final OptionDatas inputData = await optionDialog(context);
print("你選擇:$inputData");
},
),
);
}
}
enum OptionDatas { Java, Python, Swift }
Future<OptionDatas> optionDialog(BuildContext context) async {
return await showDialog<OptionDatas>(
context: context,
barrierDismissible: true,
builder: (BuildContext context) {
return SimpleDialog(
title: const Text('想要報名的課程'),
children: <Widget>[
SimpleDialogOption(
onPressed: () {
Navigator.pop(context, OptionDatas.Java);
},
child: const Text('Java'),
),
SimpleDialogOption(
onPressed: () {
Navigator.pop(context, OptionDatas.Python);
},
child: const Text('Python'),
),
SimpleDialogOption(
onPressed: () {
Navigator.pop(context, OptionDatas.Swift);
},
child: const Text('Swift'),
),
],
);
});
}
那今天【iT邦幫忙鐵人賽】就介紹到這邊囉~
順帶一提,KT 線上教室,臉書粉絲團,會不定期發佈相關資訊,不想錯過最新資訊,不要忘記來按讚,加追蹤喔!也歡迎大家將這篇文章分享給更多人喔。
我們明天見囉!!!掰掰~
HKT 線上教室
http://tw-hkt.blogspot.com/
Background vector created by freepik
https://www.freepik.com