fluffychat/lib/widgets/adaptive_dialogs/show_modal_action_popup.dart
ggurdin 49e586a7ad
Fluffychat merge (#1685)
chore: Merge upstream changes

---------

Signed-off-by: Krille <c.kussowski@famedly.com>
Co-authored-by: krille-chan <christian-kussowski@posteo.de>
Co-authored-by: Krille <c.kussowski@famedly.com>
Co-authored-by: Linerly <linerly@proton.me>
Co-authored-by: Priit Jõerüüt <hwlate@joeruut.com>
Co-authored-by: 大王叫我来巡山 <hamburger2048@users.noreply.hosted.weblate.org>
Co-authored-by: fadelkon <fadelkon@posteo.net>
Co-authored-by: Aindriú Mac Giolla Eoin <aindriu80@gmail.com>
Co-authored-by: Edgars Andersons <Edgars+Weblate@gaitenis.id.lv>
Co-authored-by: josé m <correoxm@disroot.org>
Co-authored-by: Bezruchenko Simon <worcposj44@gmail.com>
Co-authored-by: Christian <christian-pauly@posteo.de>
Co-authored-by: - <hitekex@yandex.ru>
Co-authored-by: Angelo Schirinzi <Odi-3@users.noreply.hosted.weblate.org>
Co-authored-by: xabirequejo <xabi.rn@gmail.com>
Co-authored-by: Piotr Orzechowski <piotr@orzechowski.tech>
Co-authored-by: Rex_sa <rex.sa@pm.me>
Co-authored-by: Tewuzij <tenajeza@outlook.com>
Co-authored-by: goknarbahceli <goknarbahceli@proton.me>
Co-authored-by: தமிழ்நேரம் <anishprabu.t@gmail.com>
Co-authored-by: Erin <erin@erindesu.cz>
Co-authored-by: EpicKiwi <me@epickiwi.fr>
Co-authored-by: Christian Tietze <me@christiantietze.de>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2025-02-03 12:36:46 -05:00

116 lines
3.6 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
Future<T?> showModalActionPopup<T>({
required BuildContext context,
required List<AdaptiveModalAction<T>> actions,
String? title,
String? message,
String? cancelLabel,
bool useRootNavigator = true,
}) {
final theme = Theme.of(context);
switch (theme.platform) {
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.windows:
case TargetPlatform.linux:
return showModalBottomSheet(
isScrollControlled: true,
useRootNavigator: useRootNavigator,
context: context,
clipBehavior: Clip.hardEdge,
constraints: BoxConstraints(
maxWidth: 512,
maxHeight: MediaQuery.of(context).size.height - 32,
),
builder: (context) => ListView(
shrinkWrap: true,
children: [
if (title != null || message != null) ...[
ListTile(
title: title == null
? null
: Text(
title,
style: theme.textTheme.labelSmall,
),
subtitle: message == null ? null : Text(message),
),
const Divider(height: 1),
],
...actions.map(
(action) => ListTile(
leading: action.icon,
title: Text(
action.label,
maxLines: 1,
style: action.isDestructive
? TextStyle(
color: theme.colorScheme.error,
fontWeight:
action.isDefaultAction ? FontWeight.bold : null,
)
: null,
),
onTap: () => Navigator.of(context).pop<T>(action.value),
),
),
if (cancelLabel != null) ...[
const Divider(height: 1),
ListTile(
title: Text(cancelLabel),
onTap: () => Navigator.of(context).pop(null),
),
],
],
),
);
case TargetPlatform.iOS:
case TargetPlatform.macOS:
return showCupertinoModalPopup<T>(
context: context,
useRootNavigator: useRootNavigator,
builder: (context) => ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 512),
child: CupertinoActionSheet(
title: title == null ? null : Text(title),
message: message == null ? null : Text(message),
cancelButton: cancelLabel == null
? null
: CupertinoActionSheetAction(
onPressed: () => Navigator.of(context).pop(null),
child: Text(cancelLabel),
),
actions: actions
.map(
(action) => CupertinoActionSheetAction(
isDestructiveAction: action.isDestructive,
isDefaultAction: action.isDefaultAction,
onPressed: () => Navigator.of(context).pop<T>(action.value),
child: Text(action.label, maxLines: 1),
),
)
.toList(),
),
),
);
}
}
class AdaptiveModalAction<T> {
final String label;
final T value;
Icon? icon;
final bool isDefaultAction;
final bool isDestructive;
AdaptiveModalAction({
required this.label,
required this.value,
this.icon,
this.isDefaultAction = false,
this.isDestructive = false,
});
}