flutter_dialogs/example/lib/main.dart

401 lines
14 KiB
Dart
Raw Permalink Normal View History

2022-11-01 08:32:36 +01:00
// SPDX-FileCopyrightText: 2022 Iconica
//
// SPDX-License-Identifier: BSD-3-Clause
import "package:flutter/material.dart";
import "package:flutter_dialogs/flutter_dialogs.dart";
2022-10-21 12:05:05 +02:00
void main() {
runApp(const DialogDemoApp());
2022-10-21 12:05:05 +02:00
}
class DialogDemoApp extends StatelessWidget {
const DialogDemoApp({super.key});
2022-10-21 12:05:05 +02:00
@override
Widget build(BuildContext context) => MaterialApp(
title: "Flutter Dialogs demo",
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const DialogDemoPage(title: "Flutter Dialogs demo"),
builder: (context, child) => BottomAlertDialogConfig(
child: child ?? const SizedBox.shrink(),
),
);
2022-10-21 12:05:05 +02:00
}
class DialogDemoPage extends StatefulWidget {
const DialogDemoPage({required this.title, super.key});
2022-10-21 12:05:05 +02:00
final String title;
@override
State<DialogDemoPage> createState() => _DialogDemoPageState();
2022-10-21 12:05:05 +02:00
}
class _DialogDemoPageState extends State<DialogDemoPage> {
2022-10-21 12:05:05 +02:00
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) => PopUpParent(
child: Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Spacer(
flex: 3,
),
ElevatedButton(
onPressed: () {
PopUpService.instance.showIconText("test");
},
child: const Text("Show popup"),
),
const Spacer(),
ElevatedButton(
onPressed: () async => DialogService.instance.showDialog(
2022-10-21 12:05:05 +02:00
context: context,
builder: (_) => IconicaAlertDialog.multiButton(
closeButton: true,
title: "title",
body: "body",
2022-10-21 12:05:05 +02:00
iconButton: (context, onPressed) => IconButton(
onPressed: onPressed,
icon: const Icon(Icons.dangerous),
),
primaryButton: (onPressed, child, context) =>
ElevatedButton(
onPressed: () {
debugPrint("custom button");
2022-10-21 12:05:05 +02:00
onPressed.call();
},
child: child,
),
buttons: [
AlertDialogAction(
primary: true,
text: "test",
2022-10-21 12:05:05 +02:00
onPressed: () {
debugPrint("test");
2022-10-21 12:05:05 +02:00
},
),
AlertDialogAction(
secondary: true,
text: "test",
2022-10-21 12:05:05 +02:00
onPressed: () {
debugPrint("test");
2022-10-21 12:05:05 +02:00
},
),
],
),
),
child: const Text("Show dialog"),
),
const Spacer(
flex: 3,
),
ElevatedButton(
onPressed: () async => showDialog(
context: context,
builder: (context) => BottomAlertDialog.singleButton(
closeButton: true,
title: const Text("Confirm"),
body: const Text(
"Click the button to dismiss",
),
buttonText: "Ok",
onPressed: () {
Navigator.pop(context);
},
),
),
child: const Text("BottomAlertDialog.singleButton"),
),
Container(
height: 10,
),
ElevatedButton(
onPressed: () async => showDialog(
context: context,
builder: (context) => BottomAlertDialog.multiButton(
title: const Text("Favorite Color"),
body: const Text(
"Choose your favorite color",
),
buttons: [
BottomAlertDialogAction(
text: "Red",
onPressed: () {
Navigator.pop(context);
},
buttonType: ButtonType.primary,
),
BottomAlertDialogAction(
text: "Green",
onPressed: () {
Navigator.pop(context);
},
buttonType: ButtonType.primary,
),
BottomAlertDialogAction(
text: "Blue",
onPressed: () {
Navigator.pop(context);
},
buttonType: ButtonType.primary,
),
BottomAlertDialogAction(
text: "Yellow",
onPressed: () {
Navigator.pop(context);
},
buttonType: ButtonType.primary,
),
],
),
),
child: const Text("BottomAlertDialog.multiButton"),
),
Container(
height: 10,
),
ElevatedButton(
onPressed: () async => showDialog(
context: context,
builder: (context) => BottomAlertDialog.singleButtonIcon(
closeButton: true,
title: const Text("Confirm"),
body: const Text(
"Click the button to dismiss",
),
icon: const Icon(
Icons.info,
color: Colors.blue,
),
buttonText: "Ok",
onPressed: () {
Navigator.pop(context);
},
),
),
child: const Text("BottomAlertDialog.singleButtonIcon"),
),
Container(
height: 10,
),
ElevatedButton(
onPressed: () async => showDialog(
context: context,
builder: (context) => BottomAlertDialog.icon(
closeButton: true,
title: const Text("Favorite Car"),
body: const Text(
"Choose your favorite car brand",
),
icon: const Icon(
Icons.car_rental_sharp,
),
buttons: [
ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text("BMW"),
),
ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text("Opel"),
),
ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text("Mercedes"),
),
ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text("Kia"),
),
ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text("Skoda"),
),
],
),
),
child: const Text("BottomAlertDialog.icon"),
),
Container(
height: 10,
),
ElevatedButton(
onPressed: () async => showDialog(
context: context,
builder: (context) => BottomAlertDialog.yesOrNo(
title: const Text("Question"),
body: const Text(
"Do you really wanna do this?",
),
onYes: () {
Navigator.of(context).pop();
},
onNo: () {
Navigator.of(context).pop();
},
),
),
child: const Text("BottomAlertDialog.yesOrNo"),
),
Container(
height: 10,
),
ElevatedButton(
onPressed: () async => showDialog(
context: context,
builder: (context) => BottomAlertDialog.yesOrNoIcon(
title: const Text("Question"),
body: const Text(
"Do you really wanna do this?",
),
onYes: () {
Navigator.of(context).pop();
},
onNo: () {
Navigator.of(context).pop();
},
icon: const Icon(
Icons.question_mark_sharp,
color: Colors.red,
),
),
),
child: const Text("BottomAlertDialog.yesOrNoIcon"),
),
Container(
height: 10,
),
ElevatedButton(
onPressed: () async => showDialog(
context: context,
builder: (context) => BottomAlertDialog.custom(
closeButton: true,
body: SizedBox(
height: 100,
child: Column(
children: [
const Text("Custom Dialog with PageView"),
Flexible(
child: PageView(
children: const [
Center(child: Text("Page 1")),
Center(child: Text("Page 2")),
Center(child: Text("Page 3")),
],
),
),
],
),
),
buttons: [
ElevatedButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text("Ok"),
),
],
),
),
child: const Text("BottomAlertDialog.custom"),
),
Container(
height: 10,
),
ElevatedButton(
child: const Text("Multiple chained dialogs"),
onPressed: () async => showDialog(
context: context,
builder: (context) => BottomAlertDialog.yesOrNo(
closeButton: true,
title: const Text("Pokémon"),
body: const Text(
"Do you want to choose your starter Pokémon?",
),
onYes: () {
Navigator.pop(context);
showDialog(
context: context,
builder: (context) => BottomAlertDialog.multiButton(
title: const Text("Starter Pokémon"),
body: const Text("Choose a starter Pokémon"),
buttons: [
BottomAlertDialogAction(
text: "Turtwig",
buttonType: ButtonType.secondary,
onPressed: () =>
_showDoneDialog(context, "Turtwig"),
),
BottomAlertDialogAction(
text: "Chimchar",
buttonType: ButtonType.secondary,
onPressed: () =>
_showDoneDialog(context, "Chimchar"),
),
BottomAlertDialogAction(
text: "Piplup",
buttonType: ButtonType.secondary,
onPressed: () =>
_showDoneDialog(context, "Piplup"),
),
],
),
);
},
onNo: () => Navigator.pop(context),
),
),
),
const Spacer(
flex: 3,
),
],
),
2022-10-21 12:05:05 +02:00
),
),
);
Future<void> _showDoneDialog(BuildContext context, String name) async {
Navigator.pop(context);
await showDialog(
context: context,
builder: (context) => BottomAlertDialog.icon(
title: const Text("Good choice!"),
icon: Icon(
Icons.catching_pokemon,
color: Color(name.hashCode).withAlpha(255),
),
body: Text("You chose $name to be your starter Pokémon."),
buttons: const [
CloseButton(
color: Colors.green,
),
],
),
);
}
2022-10-21 12:05:05 +02:00
}