refactor: Implement own adaptive dialogs and remove package
parent
dea29161c8
commit
8819c40ebd
@ -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,
|
||||
});
|
||||
}
|
@ -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),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
@ -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,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
Loading…
Reference in New Issue