実装
main.dart
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyButtonPage(),
);
}
}
class MyButtonPage extends StatelessWidget {
void _showAlertDialog(BuildContext context) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Alert'),
content: Text('This is an alert message.'),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop(); // Close the dialog
},
child: Text('OK'),
),
],
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Button with Alert Example'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
_showAlertDialog(
context); // Show the alert when the button is tapped
},
child: Text('Show Alert'),
),
),
);
}
}