1. 若想要使用選項清單,可以用SimpleDialog / SimpleDialogOption
2. enum是枚舉
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(home: HomePage()));
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
_HomePageState createState() => _HomePageState();
}
enum ConfirmAction { BYE, SURE }
class _HomePageState extends State<HomePage> {
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
child: Text('clickMe'),
onPressed: () async {
final ConfirmAction? action = await showAlert(context);
print(action);
// 將印出:flutter: ConfirmAction.SURE
// 或:flutter: ConfirmAction.BYE
},
),
),
);
}
Future<ConfirmAction?> showAlert(BuildContext context) {
return showDialog<ConfirmAction?>(
context: context,
builder: (context) {
return AlertDialog(
title: Text('標題'),
content: Text('內容'),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop(ConfirmAction.BYE);
},
child: Text('掰掰')),
TextButton(
onPressed: () {
Navigator.of(context).pop(ConfirmAction.SURE);
},
child: Text('好喔'))
],
);
});
}
}
沒有留言:
張貼留言