diff options
| author | mirror <mirror@localhost> | 2026-11-22 08:41:00 +0900 |
|---|---|---|
| committer | mirror <mirror@localhost> | 2026-11-22 08:41:00 +0900 |
| commit | 449d8b74bd6d9dae98a07254cdfad677b920e7b0 (patch) | |
| tree | c131c3528c4d517f56e19b854a7453fedb097d40 /lib/widgets/dialogs/selection_dialogs | |
| download | aves-src-master.tar.gz aves-src-master.zip | |
Diffstat (limited to 'lib/widgets/dialogs/selection_dialogs')
4 files changed, 257 insertions, 0 deletions
diff --git a/lib/widgets/dialogs/selection_dialogs/common.dart b/lib/widgets/dialogs/selection_dialogs/common.dart new file mode 100644 index 0000000..fe34e4f --- /dev/null +++ b/lib/widgets/dialogs/selection_dialogs/common.dart @@ -0,0 +1,23 @@ +import 'package:aves/theme/durations.dart'; +import 'package:aves/widgets/dialogs/selection_dialogs/single_selection.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/scheduler.dart'; + +Future<void> showSelectionDialog<T>({ + required BuildContext context, + required WidgetBuilder builder, + required void Function(T value) onSelection, +}) async { + final value = await showDialog<T>( + context: context, + builder: builder, + routeSettings: const RouteSettings(name: AvesSingleSelectionDialog.routeName), + ); + // wait for the dialog to hide + await Future.delayed(ADurations.dialogTransitionLoose * timeDilation); + if (value != null) { + onSelection(value); + } +} + +typedef TextBuilder<T> = String? Function(T value); diff --git a/lib/widgets/dialogs/selection_dialogs/multi_selection.dart b/lib/widgets/dialogs/selection_dialogs/multi_selection.dart new file mode 100644 index 0000000..a9079b3 --- /dev/null +++ b/lib/widgets/dialogs/selection_dialogs/multi_selection.dart @@ -0,0 +1,91 @@ +import 'package:aves/widgets/common/extensions/build_context.dart'; +import 'package:aves/widgets/dialogs/aves_dialog.dart'; +import 'package:aves/widgets/dialogs/selection_dialogs/common.dart'; +import 'package:flutter/material.dart'; + +class AvesMultiSelectionDialog<T> extends StatefulWidget { + static const routeName = '/dialog/multi_selection'; + + final Set<T> initialValue; + final Map<T, String> options; + final TextBuilder<T>? optionSubtitleBuilder; + final String? title, message; + final bool? dense; + + const AvesMultiSelectionDialog({ + super.key, + required this.initialValue, + required this.options, + this.optionSubtitleBuilder, + this.title, + this.message, + this.dense, + }); + + @override + State<AvesMultiSelectionDialog<T>> createState() => _AvesMultiSelectionDialogState<T>(); +} + +class _AvesMultiSelectionDialogState<T> extends State<AvesMultiSelectionDialog<T>> { + late Set<T> _selectedValues; + + @override + void initState() { + super.initState(); + _selectedValues = widget.initialValue; + } + + @override + Widget build(BuildContext context) { + final title = widget.title; + final message = widget.message; + final verticalPadding = (title == null && message == null) ? AvesDialog.cornerRadius.y / 2 : .0; + return AvesDialog( + title: title, + scrollableContent: [ + if (verticalPadding != 0) SizedBox(height: verticalPadding), + if (message != null) + Padding( + padding: const EdgeInsets.all(16), + child: Text(message), + ), + ...widget.options.entries.map((kv) { + final value = kv.key; + final title = kv.value; + final subtitle = widget.optionSubtitleBuilder?.call(value); + return SwitchListTile( + value: _selectedValues.contains(value), + onChanged: (v) { + if (v) { + _selectedValues.add(value); + } else { + _selectedValues.remove(value); + } + setState(() {}); + }, + title: Align( + alignment: AlignmentDirectional.centerStart, + child: Text(title), + ), + subtitle: subtitle != null + ? Text( + subtitle, + softWrap: false, + overflow: TextOverflow.fade, + ) + : null, + dense: widget.dense, + ); + }), + if (verticalPadding != 0) SizedBox(height: verticalPadding), + ], + actions: [ + const CancelButton(), + TextButton( + onPressed: () => Navigator.maybeOf(context)?.pop(widget.options.keys.where(_selectedValues.contains).toList()), + child: Text(context.l10n.applyButtonLabel), + ), + ], + ); + } +} diff --git a/lib/widgets/dialogs/selection_dialogs/radio_list_tile.dart b/lib/widgets/dialogs/selection_dialogs/radio_list_tile.dart new file mode 100644 index 0000000..9307ff9 --- /dev/null +++ b/lib/widgets/dialogs/selection_dialogs/radio_list_tile.dart @@ -0,0 +1,61 @@ +import 'package:aves/widgets/common/basic/list_tiles/reselectable_radio.dart'; +import 'package:aves/widgets/dialogs/selection_dialogs/common.dart'; +import 'package:flutter/material.dart'; + +class SelectionRadioListTile<T> extends StatelessWidget { + final T value; + final String title; + final TextBuilder<T>? optionSubtitleBuilder; + final bool needConfirmation; + final bool? dense; + final Widget? secondary; + final T Function() getGroupValue; + final void Function(T value) setGroupValue; + + const SelectionRadioListTile({ + super.key, + required this.value, + required this.title, + this.optionSubtitleBuilder, + required this.needConfirmation, + this.dense, + this.secondary, + required this.getGroupValue, + required this.setGroupValue, + }); + + @override + Widget build(BuildContext context) { + final subtitle = optionSubtitleBuilder?.call(value); + return ReselectableRadioListTile<T>( + // key is expected by test driver + key: Key('$value'), + value: value, + groupValue: getGroupValue(), + onChanged: (v) { + // always update the group value even when popping afterwards, + // so that the group value can be used in pop handlers + // as well as the regular return value from navigation + setGroupValue(v as T); + if (!needConfirmation) { + Navigator.maybeOf(context)?.pop(v); + } + }, + reselectable: true, + title: Text( + title, + overflow: TextOverflow.ellipsis, + maxLines: 2, + ), + subtitle: subtitle != null + ? Text( + subtitle, + softWrap: false, + overflow: TextOverflow.fade, + ) + : null, + dense: dense, + secondary: secondary, + ); + } +} diff --git a/lib/widgets/dialogs/selection_dialogs/single_selection.dart b/lib/widgets/dialogs/selection_dialogs/single_selection.dart new file mode 100644 index 0000000..a39b030 --- /dev/null +++ b/lib/widgets/dialogs/selection_dialogs/single_selection.dart @@ -0,0 +1,82 @@ +import 'package:aves/widgets/dialogs/aves_dialog.dart'; +import 'package:aves/widgets/dialogs/selection_dialogs/common.dart'; +import 'package:aves/widgets/dialogs/selection_dialogs/radio_list_tile.dart'; +import 'package:flutter/material.dart'; + +// do not use as `T` a record containing a collection +// because radio value comparison will fail without deep equality +class AvesSingleSelectionDialog<T> extends StatefulWidget { + static const routeName = '/dialog/selection'; + + final T initialValue; + final Map<T, String> options; + final TextBuilder<T>? optionSubtitleBuilder; + final String? title, message, confirmationButtonLabel; + final bool? dense; + + const AvesSingleSelectionDialog({ + super.key, + required this.initialValue, + required this.options, + this.optionSubtitleBuilder, + this.title, + this.message, + this.confirmationButtonLabel, + this.dense, + }); + + @override + State<AvesSingleSelectionDialog<T>> createState() => _AvesSingleSelectionDialogState<T>(); +} + +class _AvesSingleSelectionDialogState<T> extends State<AvesSingleSelectionDialog<T>> { + late T _selectedValue; + + @override + void initState() { + super.initState(); + _selectedValue = widget.initialValue; + } + + @override + Widget build(BuildContext context) { + final title = widget.title; + final message = widget.message; + final verticalPadding = (title == null && message == null) ? AvesDialog.cornerRadius.y / 2 : .0; + final confirmationButtonLabel = widget.confirmationButtonLabel; + final needConfirmation = confirmationButtonLabel != null; + return AvesDialog( + title: title, + scrollableContent: [ + if (verticalPadding != 0) SizedBox(height: verticalPadding), + if (message != null) + Padding( + padding: const EdgeInsets.all(16), + child: Text(message), + ), + ...widget.options.entries.map((kv) { + final radioValue = kv.key; + final radioTitle = kv.value; + return SelectionRadioListTile( + value: radioValue, + title: radioTitle, + optionSubtitleBuilder: widget.optionSubtitleBuilder, + needConfirmation: needConfirmation, + dense: widget.dense, + getGroupValue: () => _selectedValue, + setGroupValue: (v) => setState(() => _selectedValue = v), + ); + }), + if (verticalPadding != 0) SizedBox(height: verticalPadding), + ], + actions: [ + const CancelButton(), + if (needConfirmation) + TextButton( + onPressed: () => Navigator.maybeOf(context)?.pop(_selectedValue), + child: Text(confirmationButtonLabel), + ), + ], + ); + } +} |
