add the alert dialog and snackbar examples

This commit is contained in:
pong 2025-07-28 21:27:14 +08:00
parent 22d044da21
commit b00f8509d0
4 changed files with 119 additions and 1 deletions

48
lib/AlertDialogPage.dart Normal file
View File

@ -0,0 +1,48 @@
import 'package:flutter/material.dart';
import 'AppDrawer.dart';
class AlertDialogPage extends StatefulWidget {
const AlertDialogPage({super.key});
@override
State<StatefulWidget> createState() => _AlertDialogPageState();
}
class _AlertDialogPageState extends State<AlertDialogPage> {
void _showAlertDialog() {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Alert Dialog'),
content: Text('This is an example of an Alert Dialog.'),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop(); // Close the dialog
},
child: Text('OK'),
),
],
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Alert Dialog Example'),
),
drawer: AppDrawer(),
body: Center(
child: ElevatedButton(
onPressed: _showAlertDialog,
child: Text('Show Alert Dialog'),
),
),
);
}
}

View File

@ -1,6 +1,8 @@
import 'package:flutter/material.dart';
import 'listViewPage.dart';
import 'buttonPage.dart';
import 'AlertDialogPage.dart';
import 'snackbarPage.dart';
class AppDrawer extends StatefulWidget {
const AppDrawer({super.key});
@ -58,6 +60,28 @@ class _AppDrawerState extends State<AppDrawer>{
);
},
),
ListTile(
leading: Icon(Icons.warning),
title: Text('Alert Dialog'),
onTap: () {
Navigator.pop(context);
Navigator.push(
context,
MaterialPageRoute(builder: (context) => AlertDialogPage()),
);
},
),
ListTile(
leading: Icon(Icons.notifications),
title: Text('Snackbar'),
onTap: () {
Navigator.pop(context);
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SnackbarPage()),
);
},
),
ListTile(
leading: Icon(Icons.settings),
title: Text('Settings'),

View File

@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import 'loginPage.dart';
import 'buttonPage.dart';
void main() {
runApp(const MyApp());

46
lib/snackbarPage.dart Normal file
View File

@ -0,0 +1,46 @@
import 'package:flutter/material.dart';
import 'AppDrawer.dart';
class SnackbarPage extends StatefulWidget {
const SnackbarPage({super.key});
@override
State<SnackbarPage> createState() => _SnackbarPageState();
}
class _SnackbarPageState extends State<SnackbarPage> {
void _showSnackbar() {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('This is a Snackbar message!'),
duration: Duration(seconds: 2),
action: SnackBarAction(
label: 'Undo',
onPressed: () {
// Code to execute when the user presses the action button
},
),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Snackbar Example'),
),
drawer: AppDrawer(),
body: Center(
child: ElevatedButton(
onPressed: _showSnackbar,
child: Text('Show Snackbar'),
),
),
);
}
}