ente/lib/ui/common/dialogs.dart

79 lines
1.8 KiB
Dart
Raw Normal View History

// @dart=2.9
2021-08-20 11:22:01 +00:00
import 'package:flutter/material.dart';
2022-07-12 06:30:02 +00:00
import 'package:photos/ente_theme_data.dart';
2021-08-20 11:22:01 +00:00
2021-10-26 13:35:13 +00:00
enum DialogUserChoice { firstChoice, secondChoice }
2021-08-20 11:22:01 +00:00
2021-08-27 13:40:43 +00:00
enum ActionType {
confirm,
critical,
}
2022-06-02 03:36:50 +00:00
// if dialog is dismissed by tapping outside, this will return null
2022-05-12 12:19:06 +00:00
Future<DialogUserChoice> showChoiceDialog<T>(
2021-08-20 11:22:01 +00:00
BuildContext context,
String title,
String content, {
2022-05-30 11:13:13 +00:00
String firstAction = 'Ok',
2022-02-26 12:25:15 +00:00
Color firstActionColor,
2022-05-30 11:13:13 +00:00
String secondAction = 'Cancel',
2022-02-26 12:25:15 +00:00
Color secondActionColor,
2021-08-27 13:40:43 +00:00
ActionType actionType = ActionType.confirm,
2021-08-20 11:22:01 +00:00
}) {
2022-08-29 14:43:31 +00:00
final AlertDialog alert = AlertDialog(
2021-10-26 13:35:13 +00:00
title: Text(
title,
2021-08-27 13:40:43 +00:00
style: TextStyle(
2022-06-02 03:36:50 +00:00
color: actionType == ActionType.critical
? Colors.red
: Theme.of(context).colorScheme.primary,
2021-10-26 13:35:13 +00:00
),
),
content: Text(
content,
2022-07-04 06:02:17 +00:00
style: const TextStyle(
2021-10-26 13:35:13 +00:00
height: 1.4,
2021-08-27 13:40:43 +00:00
),
),
2021-08-20 11:22:01 +00:00
actions: [
TextButton(
child: Text(
firstAction,
style: TextStyle(
2022-02-26 12:25:15 +00:00
color: firstActionColor ??
2022-03-05 20:52:00 +00:00
(actionType == ActionType.critical
? Colors.red
: Theme.of(context).colorScheme.onSurface),
2021-08-20 11:22:01 +00:00
),
),
onPressed: () {
2021-08-27 13:40:43 +00:00
Navigator.of(context, rootNavigator: true)
.pop(DialogUserChoice.firstChoice);
2021-08-20 11:22:01 +00:00
},
),
TextButton(
2021-08-27 13:40:43 +00:00
child: Text(
secondAction,
style: TextStyle(
2022-07-12 06:30:02 +00:00
color: secondActionColor ??
Theme.of(context).colorScheme.greenAlternative,
2021-08-27 13:40:43 +00:00
),
),
2021-08-20 11:22:01 +00:00
onPressed: () {
2021-08-27 13:40:43 +00:00
Navigator.of(context, rootNavigator: true)
.pop(DialogUserChoice.secondChoice);
2021-08-20 11:22:01 +00:00
},
),
],
);
return showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
barrierColor: Colors.black87,
);
2021-10-26 13:35:13 +00:00
}