refactor: Implement own adaptive dialogs and remove package

This commit is contained in:
krille-chan 2024-12-10 18:47:58 +01:00
commit 8819c40ebd
No known key found for this signature in database
42 changed files with 597 additions and 439 deletions

View file

@ -0,0 +1,116 @@
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,
});
}

View file

@ -0,0 +1,77 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:fluffychat/widgets/adaptive_dialogs/adaptive_dialog_action.dart';
enum OkCancelResult { ok, cancel }
Future<OkCancelResult?> showOkCancelAlertDialog({
required BuildContext context,
required String title,
String? message,
String? okLabel,
String? cancelLabel,
bool isDestructive = false,
bool useRootNavigator = true,
}) =>
showAdaptiveDialog<OkCancelResult>(
context: context,
useRootNavigator: useRootNavigator,
builder: (context) => AlertDialog.adaptive(
title: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 256),
child: Text(title),
),
content: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 256),
child: message == null ? null : Text(message),
),
actions: [
AdaptiveDialogAction(
onPressed: () => Navigator.of(context)
.pop<OkCancelResult>(OkCancelResult.cancel),
child: Text(cancelLabel ?? L10n.of(context).cancel),
),
AdaptiveDialogAction(
onPressed: () =>
Navigator.of(context).pop<OkCancelResult>(OkCancelResult.ok),
child: Text(
okLabel ?? L10n.of(context).ok,
style: isDestructive
? TextStyle(color: Theme.of(context).colorScheme.error)
: null,
),
),
],
),
);
Future<OkCancelResult?> showOkAlertDialog({
required BuildContext context,
required String title,
String? message,
String? okLabel,
bool useRootNavigator = true,
}) =>
showAdaptiveDialog<OkCancelResult>(
context: context,
useRootNavigator: useRootNavigator,
builder: (context) => AlertDialog.adaptive(
title: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 256),
child: Text(title),
),
content: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 256),
child: message == null ? null : Text(message),
),
actions: [
AdaptiveDialogAction(
onPressed: () =>
Navigator.of(context).pop<OkCancelResult>(OkCancelResult.ok),
child: Text(okLabel ?? L10n.of(context).close),
),
],
),
);

View file

@ -0,0 +1,101 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:fluffychat/widgets/adaptive_dialogs/adaptive_dialog_action.dart';
Future<String?> showTextInputDialog({
required BuildContext context,
required String title,
String? message,
String? okLabel,
String? cancelLabel,
bool useRootNavigator = true,
String? hintText,
String? labelText,
String? initialText,
String? prefixText,
String? suffixText,
bool obscureText = false,
bool isDestructive = false,
int? minLines,
int? maxLines,
String? Function(String input)? validator,
TextInputType? keyboardType,
int? maxLength,
bool autocorrect = true,
}) =>
showAdaptiveDialog<String>(
context: context,
useRootNavigator: useRootNavigator,
builder: (context) {
final controller = TextEditingController(text: initialText);
final error = ValueNotifier<String?>(null);
return ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 512),
child: AlertDialog.adaptive(
title: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 256),
child: Text(title),
),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
if (message != null)
Padding(
padding: const EdgeInsets.only(bottom: 16.0),
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 256),
child: Text(message),
),
),
ValueListenableBuilder<String?>(
valueListenable: error,
builder: (context, error, _) {
return TextField(
controller: controller,
obscureText: obscureText,
minLines: minLines,
maxLines: maxLines,
maxLength: maxLength,
keyboardType: keyboardType,
autocorrect: autocorrect,
decoration: InputDecoration(
errorText: error,
hintText: hintText,
labelText: labelText,
prefixText: prefixText,
suffixText: suffixText,
),
);
},
),
],
),
actions: [
AdaptiveDialogAction(
onPressed: () => Navigator.of(context).pop(null),
child: Text(cancelLabel ?? L10n.of(context).cancel),
),
AdaptiveDialogAction(
onPressed: () {
final input = controller.text;
final errorText = validator?.call(input);
if (errorText != null) {
error.value = errorText;
return;
}
Navigator.of(context).pop<String>(input);
},
child: Text(
okLabel ?? L10n.of(context).ok,
style: isDestructive
? TextStyle(color: Theme.of(context).colorScheme.error)
: null,
),
),
],
),
);
},
);

View file

@ -2,11 +2,11 @@ import 'dart:async';
import 'package:flutter/material.dart';
import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:go_router/go_router.dart';
import 'package:matrix/matrix.dart';
import 'package:fluffychat/widgets/adaptive_dialogs/show_ok_cancel_alert_dialog.dart';
import 'package:fluffychat/widgets/future_loading_dialog.dart';
import 'matrix.dart';
@ -62,7 +62,7 @@ class ChatSettingsPopupMenuState extends State<ChatSettingsPopupMenu> {
okLabel: L10n.of(context).ok,
cancelLabel: L10n.of(context).cancel,
message: L10n.of(context).archiveRoomDescription,
isDestructiveAction: true,
isDestructive: true,
);
if (confirmed == OkCancelResult.ok) {
final success = await showFutureLoadingDialog(

View file

@ -6,7 +6,7 @@ import 'package:async/async.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:fluffychat/utils/localized_exception_extension.dart';
import 'package:fluffychat/widgets/adaptive_dialog_action.dart';
import 'package:fluffychat/widgets/adaptive_dialogs/adaptive_dialog_action.dart';
/// Displays a loading dialog which reacts to the given [future]. The dialog
/// will be dismissed and the value will be returned when the future completes.

View file

@ -4,7 +4,6 @@ import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:collection/collection.dart';
import 'package:desktop_notifications/desktop_notifications.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
@ -24,6 +23,7 @@ import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_file_extension.dar
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/utils/uia_request_manager.dart';
import 'package:fluffychat/utils/voip_plugin.dart';
import 'package:fluffychat/widgets/adaptive_dialogs/show_ok_cancel_alert_dialog.dart';
import 'package:fluffychat/widgets/fluffy_chat_app.dart';
import 'package:fluffychat/widgets/future_loading_dialog.dart';
import '../config/app_config.dart';
@ -344,13 +344,11 @@ class MatrixState extends State<Matrix> with WidgetsBindingObserver {
this,
onFcmError: (errorMsg, {Uri? link}) async {
final result = await showOkCancelAlertDialog(
barrierDismissible: true,
context: FluffyChatApp
.router.routerDelegate.navigatorKey.currentContext ??
context,
title: L10n.of(context).pushNotificationsNotAvailable,
message: errorMsg,
fullyCapitalizedForMaterial: false,
okLabel:
link == null ? L10n.of(context).ok : L10n.of(context).learnMore,
cancelLabel: L10n.of(context).doNotShowAgain,
@ -470,7 +468,7 @@ class MatrixState extends State<Matrix> with WidgetsBindingObserver {
Future<void> dehydrateAction(BuildContext context) async {
final response = await showOkCancelAlertDialog(
context: context,
isDestructiveAction: true,
isDestructive: true,
title: L10n.of(context).dehydrate,
message: L10n.of(context).dehydrateWarning,
);

View file

@ -1,9 +1,10 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:fluffychat/widgets/adaptive_dialogs/show_text_input_dialog.dart';
Future<int?> showPermissionChooser(
BuildContext context, {
int currentLevel = 0,
@ -11,24 +12,20 @@ Future<int?> showPermissionChooser(
final customLevel = await showTextInputDialog(
context: context,
title: L10n.of(context).setPermissionsLevel,
textFields: [
DialogTextField(
initialText: currentLevel.toString(),
keyboardType: TextInputType.number,
autocorrect: false,
validator: (text) {
if (text == null) {
return L10n.of(context).pleaseEnterANumber;
}
final level = int.tryParse(text);
if (level == null) {
return L10n.of(context).pleaseEnterANumber;
}
return null;
},
),
],
initialText: currentLevel.toString(),
keyboardType: TextInputType.number,
autocorrect: false,
validator: (text) {
if (text.isEmpty) {
return L10n.of(context).pleaseEnterANumber;
}
final level = int.tryParse(text);
if (level == null) {
return L10n.of(context).pleaseEnterANumber;
}
return null;
},
);
if (customLevel == null) return null;
return int.tryParse(customLevel.first);
return int.tryParse(customLevel);
}