Skip to content

Instantly share code, notes, and snippets.

@edemekong
Created January 4, 2022 15:12
Show Gist options
  • Save edemekong/19997c5e910a3f866cd50e4b7230d119 to your computer and use it in GitHub Desktop.
Save edemekong/19997c5e910a3f866cd50e4b7230d119 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Showcasing Flutter',
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
String message = '';
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(message, style: const TextStyle(color: Colors.black, fontSize: 30)),
const SizedBox(height: 18),
TextButton(
style: ButtonStyle(backgroundColor: MaterialStateProperty.all(Colors.green)),
onPressed: onTapShowDialog,
child: const Text(
'Show Dialog',
style: TextStyle(color: Colors.white),
),
),
],
),
),
);
}
void onTapShowDialog() {
showAlertDialogs(
context,
title: 'Showcasing Flutter',
content: 'Purely for learning & teaching...',
).then((value) {
setState(() {
if (value == true) {
message = '[Yes] was tapped!!!';
} else if (value == false) {
message = '[NO] was tapped!!!';
} else {
message = '[Nothing] was Tapped!!!';
}
});
debugPrint(value.toString());
});
}
}
class MyAlertDialog extends StatelessWidget {
final String title;
final String content;
final String buttonText1;
final String? buttonText2;
final void Function(bool v) callback;
const MyAlertDialog(
{Key? key,
required this.title,
required this.content,
required this.buttonText1,
this.buttonText2,
required this.callback})
: super(key: key);
@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text(title),
content: Text(content),
actions: <Widget>[
TextButton(
style: ButtonStyle(backgroundColor: MaterialStateProperty.all(Colors.green)),
child: Text(buttonText1, style: const TextStyle(color: Colors.white)),
onPressed: () {
///When a user taps on [YES]
callback(true);
Navigator.pop(context);
},
),
if (buttonText2 != null)
TextButton(
child: Text("$buttonText2", style: const TextStyle(color: Colors.black)),
onPressed: () {
///When a user taps on [NO]
callback(false);
Navigator.pop(context);
},
),
],
);
}
}
Future<bool?> showAlertDialogs(BuildContext context, {required String title, required String content}) async {
bool? response;
await showDialog(
context: context,
builder: (context) => MyAlertDialog(
title: title,
content: content,
buttonText1: 'Yes',
buttonText2: 'No',
callback: (bool v) {
response = v;
},
),
);
return response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment