【Flutter 程式設計入門實戰 30 天】Day 19:對話框視窗
【Flutter 程式設計入門實戰 30 天】線上教學課程目錄 使用 Dart 程式語言,開發 Android 和 iOS APP 應用程式。
哈囉~大家好,我是 KT ,今天實戰第十九天,KT 將為大家來介紹,對話框視窗。
AlertDialog 訊息對話框視窗
AlertDialog 訊息對話框視窗,用來顯示訊息提示用戶。
注意事項:
需要搭配 async、await 和 Future 非同步處理接收回傳資料。
AlertDialog 訊息對話框視窗,常用的屬性
- title:訊息對話框視窗標題
- content:訊息對話框視窗內文
- actions:訊息對話框視窗,按鈕觸發事件
範例:基本訊息視窗
AlertDialog(
title: Text('測試標題'),
content: const Text('測試內容.....'),
actions: <Widget>[
ElevatedButton(
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: ElevatedButton(
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>[
ElevatedButton(
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>[
ElevatedButton(
child: const Text('確認'),
onPressed: () {
Navigator.of(context).pop(ConfirmAction.ACCEPT);
},
),
ElevatedButton(
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: ElevatedButton(
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>[
ElevatedButton(
child: const Text('確認'),
onPressed: () {
Navigator.of(context).pop(ConfirmAction.ACCEPT);
},
),
ElevatedButton(
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>[
ElevatedButton(
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: ElevatedButton(
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>[
ElevatedButton(
child: Text('送出'),
onPressed: () {
Navigator.of(context).pop(inputData);
},
),
],
);
},
);
}
範例:SimpleDialog 多選項訊息視窗
使用 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: ElevatedButton(
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'),
),
],
);
});
}
那今天就介紹到這邊囉~
順帶一提,KT 線上教室,臉書粉絲團,會不定期發佈相關資訊,不想錯過最新資訊,不要忘記來按讚,加追蹤喔!也歡迎大家將這篇文章分享給更多人喔。
我們明天見囉!!!掰掰~
參考資料
HKT 線上教室
http://tw-hkt.blogspot.com/
Background vector created by freepik
https://www.freepik.com