2020-05-02 13:32:06 +00:00
|
|
|
import 'package:comunic/ui/widgets/dialogs/auto_sized_dialog_content_widget.dart';
|
2020-04-27 16:44:41 +00:00
|
|
|
import 'package:comunic/utils/intl_utils.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
/// Build and show a dialog to offer to the user to choose between several
|
|
|
|
/// options
|
|
|
|
///
|
|
|
|
/// @author Pierre HUBERT
|
|
|
|
|
|
|
|
class MultiChoiceEntry<T> {
|
|
|
|
final T id;
|
|
|
|
final String title;
|
2022-03-10 18:39:57 +00:00
|
|
|
final String? subtitle;
|
2020-05-02 15:36:14 +00:00
|
|
|
final bool hidden;
|
2020-04-27 16:44:41 +00:00
|
|
|
|
|
|
|
const MultiChoiceEntry({
|
2022-03-10 18:39:57 +00:00
|
|
|
required this.id,
|
|
|
|
required this.title,
|
2020-04-27 16:44:41 +00:00
|
|
|
this.subtitle,
|
2020-05-02 15:36:14 +00:00
|
|
|
this.hidden = false,
|
2022-03-11 16:16:46 +00:00
|
|
|
}) : assert(id != null);
|
2020-05-02 15:26:03 +00:00
|
|
|
|
|
|
|
bool get hasSubtitle => subtitle != null;
|
2020-04-27 16:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Show multiple choices dialog
|
2022-03-10 18:39:57 +00:00
|
|
|
Future<T?> showMultiChoicesDialog<T>({
|
|
|
|
required BuildContext context,
|
|
|
|
required List<MultiChoiceEntry<T>> choices,
|
|
|
|
required T defaultChoice,
|
|
|
|
required String? title,
|
2020-04-27 16:44:41 +00:00
|
|
|
}) async {
|
|
|
|
return await showDialog<T>(
|
|
|
|
context: context,
|
|
|
|
builder: (c) => _MultiChoicesEntryDialog(
|
2022-03-10 18:39:57 +00:00
|
|
|
title: title!,
|
2020-04-27 16:44:41 +00:00
|
|
|
defaultChoice: defaultChoice,
|
|
|
|
choices: choices,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
class _MultiChoicesEntryDialog<T> extends StatefulWidget {
|
|
|
|
final String title;
|
|
|
|
final T defaultChoice;
|
|
|
|
final List<MultiChoiceEntry<T>> choices;
|
|
|
|
|
|
|
|
const _MultiChoicesEntryDialog({
|
2022-03-10 18:39:57 +00:00
|
|
|
Key? key,
|
|
|
|
required this.title,
|
|
|
|
required this.defaultChoice,
|
|
|
|
required this.choices,
|
2022-03-11 16:09:37 +00:00
|
|
|
}) : assert(defaultChoice != null),
|
2020-04-27 16:44:41 +00:00
|
|
|
super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
__MultiChoicesEntryDialogState createState() =>
|
|
|
|
__MultiChoicesEntryDialogState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class __MultiChoicesEntryDialogState<T>
|
|
|
|
extends State<_MultiChoicesEntryDialog<T>> {
|
2022-03-10 18:39:57 +00:00
|
|
|
T? _currChoice;
|
2020-04-27 16:44:41 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
_currChoice = widget.defaultChoice;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return AlertDialog(
|
|
|
|
title: Text(widget.title),
|
2020-05-01 19:04:50 +00:00
|
|
|
content: AutoSizeDialogContentWidget(
|
|
|
|
child: Column(
|
|
|
|
children: widget.choices
|
2020-05-02 15:36:14 +00:00
|
|
|
.where((f) => !f.hidden)
|
2020-05-01 19:04:50 +00:00
|
|
|
.map((f) => ListTile(
|
|
|
|
leading: Radio<T>(
|
|
|
|
groupValue: _currChoice,
|
|
|
|
value: f.id,
|
|
|
|
onChanged: (v) => setState(() => _currChoice = v),
|
|
|
|
),
|
|
|
|
title: Text(f.title),
|
2022-03-10 18:39:57 +00:00
|
|
|
subtitle: f.hasSubtitle ? Text(f.subtitle!) : null,
|
2020-05-01 19:04:50 +00:00
|
|
|
))
|
|
|
|
.toList(),
|
2020-04-27 16:44:41 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
actions: <Widget>[
|
|
|
|
MaterialButton(
|
|
|
|
onPressed: () => Navigator.of(context).pop(),
|
2022-03-10 18:39:57 +00:00
|
|
|
child: Text(tr("Cancel")!.toUpperCase()),
|
2020-04-27 16:44:41 +00:00
|
|
|
textColor: Colors.red,
|
|
|
|
),
|
|
|
|
MaterialButton(
|
|
|
|
onPressed: () => Navigator.of(context).pop(_currChoice),
|
2022-03-10 18:39:57 +00:00
|
|
|
child: Text(tr("Confirm")!.toUpperCase()),
|
2020-04-27 16:44:41 +00:00
|
|
|
textColor: Colors.green,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|