refactored broad range of the application, split files, modularized calendar and file views, centralized bottom sheets and clipboard handling, and implemented unit test coverage

This commit is contained in:
2026-05-08 19:05:16 +02:00
parent 3b1b0d0c19
commit c62a14645a
68 changed files with 4633 additions and 3141 deletions
+14 -537
View File
@@ -1,543 +1,20 @@
/// Family of async-aware buttons + helpers. Implementation is split across
/// `async_actions/` for readability; everything still lives in this single
/// library so private widgets like `_AsyncMixin` and `_InlineErrorWrapper`
/// can stay private and shared.
library;
import 'package:flutter/material.dart';
import '../api/errors/error_mapper.dart';
import 'app_progress_indicator.dart';
import 'info_dialog.dart';
Future<bool> runWithErrorDialog(
BuildContext context,
AsyncActionCallback action, {
AsyncErrorBuilder? errorBuilder,
}) async {
try {
await action();
return true;
} catch (e) {
if (!context.mounted) return false;
final message = errorBuilder != null ? errorBuilder(e) : errorToUserMessage(e);
final details = errorToTechnicalDetails(e);
final body = details != null && details != message ? '$message\n\n$details' : message;
InfoDialog.show(context, body, copyable: true, title: 'Fehler');
return false;
}
}
typedef AsyncActionCallback = Future<void> Function();
typedef AsyncErrorBuilder = String Function(Object error);
class AsyncActionController extends ChangeNotifier {
bool _busy = false;
String? _error;
bool get busy => _busy;
String? get error => _error;
Future<bool> run(
AsyncActionCallback action, {
AsyncErrorBuilder? errorBuilder,
}) async {
if (_busy) return false;
_busy = true;
_error = null;
notifyListeners();
try {
await action();
_busy = false;
notifyListeners();
return true;
} catch (e) {
_busy = false;
_error = errorBuilder != null ? errorBuilder(e) : errorToUserMessage(e);
notifyListeners();
return false;
}
}
void clearError() {
if (_error == null) return;
_error = null;
notifyListeners();
}
}
class _AsyncMixin extends StatefulWidget {
final AsyncActionCallback? onPressed;
final AsyncActionController? controller;
final AsyncErrorBuilder? errorBuilder;
final void Function(String message)? onError;
final VoidCallback? onSuccess;
final Widget Function(BuildContext context, bool busy, VoidCallback? handler) builder;
const _AsyncMixin({
required this.onPressed,
required this.builder,
this.controller,
this.errorBuilder,
this.onError,
this.onSuccess,
});
@override
State<_AsyncMixin> createState() => _AsyncMixinState();
}
class _AsyncMixinState extends State<_AsyncMixin> {
late final AsyncActionController _internal;
AsyncActionController get _controller => widget.controller ?? _internal;
@override
void initState() {
super.initState();
if (widget.controller == null) {
_internal = AsyncActionController();
}
_controller.addListener(_onControllerChange);
}
@override
void didUpdateWidget(covariant _AsyncMixin oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.controller != widget.controller) {
(oldWidget.controller ?? _internal).removeListener(_onControllerChange);
_controller.addListener(_onControllerChange);
}
}
@override
void dispose() {
_controller.removeListener(_onControllerChange);
if (widget.controller == null) {
_internal.dispose();
}
super.dispose();
}
void _onControllerChange() {
if (mounted) setState(() {});
}
Future<void> _trigger() async {
final action = widget.onPressed;
if (action == null) return;
final success = await _controller.run(action, errorBuilder: widget.errorBuilder);
if (!mounted) return;
if (success) {
widget.onSuccess?.call();
} else if (widget.onError != null && _controller.error != null) {
widget.onError!(_controller.error!);
}
}
@override
Widget build(BuildContext context) {
final handler = widget.onPressed == null ? null : _trigger;
return widget.builder(context, _controller.busy, _controller.busy ? null : handler);
}
}
class AsyncActionButton extends StatelessWidget {
final AsyncActionCallback? onPressed;
final Widget child;
final IconData? icon;
final ButtonStyle? style;
final AsyncActionController? controller;
final AsyncErrorBuilder? errorBuilder;
final void Function(String message)? onError;
final VoidCallback? onSuccess;
final bool showInlineError;
const AsyncActionButton({
required this.onPressed,
required this.child,
this.icon,
this.style,
this.controller,
this.errorBuilder,
this.onError,
this.onSuccess,
this.showInlineError = true,
super.key,
});
@override
Widget build(BuildContext context) => _AsyncMixin(
onPressed: onPressed,
controller: controller,
errorBuilder: errorBuilder,
onError: onError,
onSuccess: onSuccess,
builder: (context, busy, handler) {
final spinner = AppProgressIndicator.small(
color: Theme.of(context).colorScheme.onPrimary,
);
final content = busy
? Row(
mainAxisSize: MainAxisSize.min,
children: [spinner, const SizedBox(width: 8), child],
)
: (icon != null
? Row(
mainAxisSize: MainAxisSize.min,
children: [Icon(icon), const SizedBox(width: 8), child],
)
: child);
final button = ElevatedButton(
onPressed: handler,
style: style,
child: content,
);
return _withInlineError(context, button);
},
);
Widget _withInlineError(BuildContext context, Widget button) {
if (!showInlineError) return button;
return _InlineErrorWrapper(controller: controller, child: button);
}
}
class AsyncTextButton extends StatelessWidget {
final AsyncActionCallback? onPressed;
final Widget child;
final AsyncActionController? controller;
final AsyncErrorBuilder? errorBuilder;
final void Function(String message)? onError;
final VoidCallback? onSuccess;
final bool showInlineError;
const AsyncTextButton({
required this.onPressed,
required this.child,
this.controller,
this.errorBuilder,
this.onError,
this.onSuccess,
this.showInlineError = true,
super.key,
});
@override
Widget build(BuildContext context) => _AsyncMixin(
onPressed: onPressed,
controller: controller,
errorBuilder: errorBuilder,
onError: onError,
onSuccess: onSuccess,
builder: (context, busy, handler) {
final content = busy
? Row(
mainAxisSize: MainAxisSize.min,
children: [
AppProgressIndicator.small(
color: Theme.of(context).colorScheme.primary,
),
const SizedBox(width: 8),
child,
],
)
: child;
return _InlineErrorWrapper(
controller: controller,
child: TextButton(onPressed: handler, child: content),
);
},
);
}
class AsyncIconButton extends StatelessWidget {
final AsyncActionCallback? onPressed;
final IconData icon;
final Color? color;
final String? tooltip;
final AsyncActionController? controller;
final AsyncErrorBuilder? errorBuilder;
final void Function(String message)? onError;
final VoidCallback? onSuccess;
const AsyncIconButton({
required this.onPressed,
required this.icon,
this.color,
this.tooltip,
this.controller,
this.errorBuilder,
this.onError,
this.onSuccess,
super.key,
});
@override
Widget build(BuildContext context) => _AsyncMixin(
onPressed: onPressed,
controller: controller,
errorBuilder: errorBuilder,
onError: onError,
onSuccess: onSuccess,
builder: (context, busy, handler) {
if (busy) {
return Padding(
padding: const EdgeInsets.all(12),
child: AppProgressIndicator.small(color: color),
);
}
return IconButton(
icon: Icon(icon, color: color),
tooltip: tooltip,
onPressed: handler,
);
},
);
}
class AsyncFab extends StatelessWidget {
final AsyncActionCallback? onPressed;
final IconData icon;
final Color? backgroundColor;
final Color? foregroundColor;
final Object? heroTag;
final AsyncActionController? controller;
final AsyncErrorBuilder? errorBuilder;
final void Function(String message)? onError;
final VoidCallback? onSuccess;
final bool mini;
const AsyncFab({
required this.onPressed,
required this.icon,
this.backgroundColor,
this.foregroundColor,
this.heroTag,
this.controller,
this.errorBuilder,
this.onError,
this.onSuccess,
this.mini = false,
super.key,
});
@override
Widget build(BuildContext context) => _AsyncMixin(
onPressed: onPressed,
controller: controller,
errorBuilder: errorBuilder,
onError: onError,
onSuccess: onSuccess,
builder: (context, busy, handler) {
final fg = foregroundColor ?? Theme.of(context).colorScheme.onPrimary;
return FloatingActionButton(
heroTag: heroTag,
backgroundColor: backgroundColor,
foregroundColor: fg,
mini: mini,
onPressed: handler,
child: busy ? AppProgressIndicator.small(color: fg) : Icon(icon),
);
},
);
}
class AsyncListTile extends StatefulWidget {
final AsyncActionCallback onPressed;
final Widget? leading;
final Widget title;
final Widget? subtitle;
final bool closeOnSuccess;
final VoidCallback? onSuccess;
final AsyncErrorBuilder? errorBuilder;
final bool enabled;
const AsyncListTile({
required this.onPressed,
required this.title,
this.leading,
this.subtitle,
this.closeOnSuccess = true,
this.onSuccess,
this.errorBuilder,
this.enabled = true,
super.key,
});
@override
State<AsyncListTile> createState() => _AsyncListTileState();
}
class _AsyncListTileState extends State<AsyncListTile> {
final AsyncActionController _controller = AsyncActionController();
@override
void dispose() {
_controller.dispose();
super.dispose();
}
Future<void> _handleTap() async {
final ok = await _controller.run(widget.onPressed, errorBuilder: widget.errorBuilder);
if (!mounted) return;
if (ok) {
widget.onSuccess?.call();
if (widget.closeOnSuccess && Navigator.of(context).canPop()) {
Navigator.of(context).pop();
}
}
}
@override
Widget build(BuildContext context) => AnimatedBuilder(
animation: _controller,
builder: (context, _) {
final busy = _controller.busy;
final err = _controller.error;
final leading = busy
? const SizedBox(
width: 24,
height: 24,
child: AppProgressIndicator.small(),
)
: widget.leading;
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
ListTile(
leading: leading,
title: widget.title,
subtitle: widget.subtitle,
enabled: widget.enabled && !busy,
onTap: busy ? null : _handleTap,
),
if (err != null)
Padding(
padding: const EdgeInsets.only(left: 16, right: 16, bottom: 8),
child: Text(
err,
style: TextStyle(color: Theme.of(context).colorScheme.error, fontSize: 13),
),
),
],
);
},
);
}
class _InlineErrorWrapper extends StatelessWidget {
final AsyncActionController? controller;
final Widget child;
const _InlineErrorWrapper({required this.controller, required this.child});
@override
Widget build(BuildContext context) {
final c = controller;
if (c == null) return child;
return AnimatedBuilder(
animation: c,
builder: (context, _) {
final err = c.error;
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
child,
if (err != null) ...[
const SizedBox(height: 8),
Text(
err,
textAlign: TextAlign.center,
style: TextStyle(color: Theme.of(context).colorScheme.error, fontSize: 13),
),
],
],
);
},
);
}
}
class AsyncDialogAction extends StatefulWidget {
final String confirmLabel;
final AsyncActionCallback onConfirm;
final String? cancelLabel;
final AsyncErrorBuilder? errorBuilder;
final ButtonStyle? confirmStyle;
const AsyncDialogAction({
required this.confirmLabel,
required this.onConfirm,
this.cancelLabel = 'Abbrechen',
this.errorBuilder,
this.confirmStyle,
super.key,
});
@override
State<AsyncDialogAction> createState() => _AsyncDialogActionState();
}
class _AsyncDialogActionState extends State<AsyncDialogAction> {
final AsyncActionController _controller = AsyncActionController();
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) => AnimatedBuilder(
animation: _controller,
builder: (context, _) {
final err = _controller.error;
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
if (err != null)
Padding(
padding: const EdgeInsets.only(bottom: 8),
child: Text(
err,
textAlign: TextAlign.center,
style: TextStyle(color: Theme.of(context).colorScheme.error, fontSize: 13),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
if (widget.cancelLabel != null)
TextButton(
onPressed: _controller.busy ? null : () => Navigator.of(context).pop(),
child: Text(widget.cancelLabel!),
),
TextButton(
style: widget.confirmStyle,
onPressed: _controller.busy
? null
: () async {
final ok = await _controller.run(
widget.onConfirm,
errorBuilder: widget.errorBuilder,
);
if (ok && context.mounted) {
Navigator.of(context).pop(true);
}
},
child: _controller.busy
? Row(
mainAxisSize: MainAxisSize.min,
children: [
AppProgressIndicator.small(
color: Theme.of(context).colorScheme.primary,
),
const SizedBox(width: 8),
Text(widget.confirmLabel),
],
)
: Text(widget.confirmLabel),
),
],
),
],
);
},
);
}
part 'async_actions/async_action_controller.dart';
part 'async_actions/async_action_button.dart';
part 'async_actions/async_dialog_action.dart';
part 'async_actions/async_fab.dart';
part 'async_actions/async_icon_button.dart';
part 'async_actions/async_list_tile.dart';
part 'async_actions/async_mixin.dart';
part 'async_actions/async_text_button.dart';
@@ -0,0 +1,58 @@
part of '../async_action_button.dart';
class AsyncActionButton extends StatelessWidget {
final AsyncActionCallback? onPressed;
final Widget child;
final IconData? icon;
final ButtonStyle? style;
final AsyncActionController? controller;
final AsyncErrorBuilder? errorBuilder;
final void Function(String message)? onError;
final VoidCallback? onSuccess;
final bool showInlineError;
const AsyncActionButton({
required this.onPressed,
required this.child,
this.icon,
this.style,
this.controller,
this.errorBuilder,
this.onError,
this.onSuccess,
this.showInlineError = true,
super.key,
});
@override
Widget build(BuildContext context) => _AsyncMixin(
onPressed: onPressed,
controller: controller,
errorBuilder: errorBuilder,
onError: onError,
onSuccess: onSuccess,
builder: (context, busy, handler) {
final spinner = AppProgressIndicator.small(
color: Theme.of(context).colorScheme.onPrimary,
);
final content = busy
? Row(
mainAxisSize: MainAxisSize.min,
children: [spinner, const SizedBox(width: 8), child],
)
: (icon != null
? Row(
mainAxisSize: MainAxisSize.min,
children: [Icon(icon), const SizedBox(width: 8), child],
)
: child);
final button = ElevatedButton(
onPressed: handler,
style: style,
child: content,
);
if (!showInlineError) return button;
return _InlineErrorWrapper(controller: controller, child: button);
},
);
}
@@ -0,0 +1,63 @@
part of '../async_action_button.dart';
typedef AsyncActionCallback = Future<void> Function();
typedef AsyncErrorBuilder = String Function(Object error);
/// Wraps [action] with a try/catch that pops up an [InfoDialog] on failure
/// (using [errorBuilder] or the default error mapper). Returns `true` on
/// success, `false` on caught failure.
Future<bool> runWithErrorDialog(
BuildContext context,
AsyncActionCallback action, {
AsyncErrorBuilder? errorBuilder,
}) async {
try {
await action();
return true;
} catch (e) {
if (!context.mounted) return false;
final message = errorBuilder != null ? errorBuilder(e) : errorToUserMessage(e);
final details = errorToTechnicalDetails(e);
final body = details != null && details != message ? '$message\n\n$details' : message;
InfoDialog.show(context, body, copyable: true, title: 'Fehler');
return false;
}
}
/// Reusable busy/error state for the async-button family. Multiple buttons
/// can share the same controller (e.g. a parent toolbar wanting to disable
/// while any one child is running).
class AsyncActionController extends ChangeNotifier {
bool _busy = false;
String? _error;
bool get busy => _busy;
String? get error => _error;
Future<bool> run(
AsyncActionCallback action, {
AsyncErrorBuilder? errorBuilder,
}) async {
if (_busy) return false;
_busy = true;
_error = null;
notifyListeners();
try {
await action();
_busy = false;
notifyListeners();
return true;
} catch (e) {
_busy = false;
_error = errorBuilder != null ? errorBuilder(e) : errorToUserMessage(e);
notifyListeners();
return false;
}
}
void clearError() {
if (_error == null) return;
_error = null;
notifyListeners();
}
}
@@ -0,0 +1,90 @@
part of '../async_action_button.dart';
class AsyncDialogAction extends StatefulWidget {
final String confirmLabel;
final AsyncActionCallback onConfirm;
final String? cancelLabel;
final AsyncErrorBuilder? errorBuilder;
final ButtonStyle? confirmStyle;
const AsyncDialogAction({
required this.confirmLabel,
required this.onConfirm,
this.cancelLabel = 'Abbrechen',
this.errorBuilder,
this.confirmStyle,
super.key,
});
@override
State<AsyncDialogAction> createState() => _AsyncDialogActionState();
}
class _AsyncDialogActionState extends State<AsyncDialogAction> {
final AsyncActionController _controller = AsyncActionController();
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) => AnimatedBuilder(
animation: _controller,
builder: (context, _) {
final err = _controller.error;
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
if (err != null)
Padding(
padding: const EdgeInsets.only(bottom: 8),
child: Text(
err,
textAlign: TextAlign.center,
style: TextStyle(color: Theme.of(context).colorScheme.error, fontSize: 13),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
if (widget.cancelLabel != null)
TextButton(
onPressed: _controller.busy ? null : () => Navigator.of(context).pop(),
child: Text(widget.cancelLabel!),
),
TextButton(
style: widget.confirmStyle,
onPressed: _controller.busy
? null
: () async {
final ok = await _controller.run(
widget.onConfirm,
errorBuilder: widget.errorBuilder,
);
if (ok && context.mounted) {
Navigator.of(context).pop(true);
}
},
child: _controller.busy
? Row(
mainAxisSize: MainAxisSize.min,
children: [
AppProgressIndicator.small(
color: Theme.of(context).colorScheme.primary,
),
const SizedBox(width: 8),
Text(widget.confirmLabel),
],
)
: Text(widget.confirmLabel),
),
],
),
],
);
},
);
}
+48
View File
@@ -0,0 +1,48 @@
part of '../async_action_button.dart';
class AsyncFab extends StatelessWidget {
final AsyncActionCallback? onPressed;
final IconData icon;
final Color? backgroundColor;
final Color? foregroundColor;
final Object? heroTag;
final AsyncActionController? controller;
final AsyncErrorBuilder? errorBuilder;
final void Function(String message)? onError;
final VoidCallback? onSuccess;
final bool mini;
const AsyncFab({
required this.onPressed,
required this.icon,
this.backgroundColor,
this.foregroundColor,
this.heroTag,
this.controller,
this.errorBuilder,
this.onError,
this.onSuccess,
this.mini = false,
super.key,
});
@override
Widget build(BuildContext context) => _AsyncMixin(
onPressed: onPressed,
controller: controller,
errorBuilder: errorBuilder,
onError: onError,
onSuccess: onSuccess,
builder: (context, busy, handler) {
final fg = foregroundColor ?? Theme.of(context).colorScheme.onPrimary;
return FloatingActionButton(
heroTag: heroTag,
backgroundColor: backgroundColor,
foregroundColor: fg,
mini: mini,
onPressed: handler,
child: busy ? AppProgressIndicator.small(color: fg) : Icon(icon),
);
},
);
}
@@ -0,0 +1,46 @@
part of '../async_action_button.dart';
class AsyncIconButton extends StatelessWidget {
final AsyncActionCallback? onPressed;
final IconData icon;
final Color? color;
final String? tooltip;
final AsyncActionController? controller;
final AsyncErrorBuilder? errorBuilder;
final void Function(String message)? onError;
final VoidCallback? onSuccess;
const AsyncIconButton({
required this.onPressed,
required this.icon,
this.color,
this.tooltip,
this.controller,
this.errorBuilder,
this.onError,
this.onSuccess,
super.key,
});
@override
Widget build(BuildContext context) => _AsyncMixin(
onPressed: onPressed,
controller: controller,
errorBuilder: errorBuilder,
onError: onError,
onSuccess: onSuccess,
builder: (context, busy, handler) {
if (busy) {
return Padding(
padding: const EdgeInsets.all(12),
child: AppProgressIndicator.small(color: color),
);
}
return IconButton(
icon: Icon(icon, color: color),
tooltip: tooltip,
onPressed: handler,
);
},
);
}
@@ -0,0 +1,85 @@
part of '../async_action_button.dart';
class AsyncListTile extends StatefulWidget {
final AsyncActionCallback onPressed;
final Widget? leading;
final Widget title;
final Widget? subtitle;
final bool closeOnSuccess;
final VoidCallback? onSuccess;
final AsyncErrorBuilder? errorBuilder;
final bool enabled;
const AsyncListTile({
required this.onPressed,
required this.title,
this.leading,
this.subtitle,
this.closeOnSuccess = true,
this.onSuccess,
this.errorBuilder,
this.enabled = true,
super.key,
});
@override
State<AsyncListTile> createState() => _AsyncListTileState();
}
class _AsyncListTileState extends State<AsyncListTile> {
final AsyncActionController _controller = AsyncActionController();
@override
void dispose() {
_controller.dispose();
super.dispose();
}
Future<void> _handleTap() async {
final ok = await _controller.run(widget.onPressed, errorBuilder: widget.errorBuilder);
if (!mounted) return;
if (ok) {
widget.onSuccess?.call();
if (widget.closeOnSuccess && Navigator.of(context).canPop()) {
Navigator.of(context).pop();
}
}
}
@override
Widget build(BuildContext context) => AnimatedBuilder(
animation: _controller,
builder: (context, _) {
final busy = _controller.busy;
final err = _controller.error;
final leading = busy
? const SizedBox(
width: 24,
height: 24,
child: AppProgressIndicator.small(),
)
: widget.leading;
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
ListTile(
leading: leading,
title: widget.title,
subtitle: widget.subtitle,
enabled: widget.enabled && !busy,
onTap: busy ? null : _handleTap,
),
if (err != null)
Padding(
padding: const EdgeInsets.only(left: 16, right: 16, bottom: 8),
child: Text(
err,
style: TextStyle(color: Theme.of(context).colorScheme.error, fontSize: 13),
),
),
],
);
},
);
}
+109
View File
@@ -0,0 +1,109 @@
part of '../async_action_button.dart';
class _AsyncMixin extends StatefulWidget {
final AsyncActionCallback? onPressed;
final AsyncActionController? controller;
final AsyncErrorBuilder? errorBuilder;
final void Function(String message)? onError;
final VoidCallback? onSuccess;
final Widget Function(BuildContext context, bool busy, VoidCallback? handler) builder;
const _AsyncMixin({
required this.onPressed,
required this.builder,
this.controller,
this.errorBuilder,
this.onError,
this.onSuccess,
});
@override
State<_AsyncMixin> createState() => _AsyncMixinState();
}
class _AsyncMixinState extends State<_AsyncMixin> {
late final AsyncActionController _internal;
AsyncActionController get _controller => widget.controller ?? _internal;
@override
void initState() {
super.initState();
if (widget.controller == null) {
_internal = AsyncActionController();
}
_controller.addListener(_onControllerChange);
}
@override
void didUpdateWidget(covariant _AsyncMixin oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.controller != widget.controller) {
(oldWidget.controller ?? _internal).removeListener(_onControllerChange);
_controller.addListener(_onControllerChange);
}
}
@override
void dispose() {
_controller.removeListener(_onControllerChange);
if (widget.controller == null) {
_internal.dispose();
}
super.dispose();
}
void _onControllerChange() {
if (mounted) setState(() {});
}
Future<void> _trigger() async {
final action = widget.onPressed;
if (action == null) return;
final success = await _controller.run(action, errorBuilder: widget.errorBuilder);
if (!mounted) return;
if (success) {
widget.onSuccess?.call();
} else if (widget.onError != null && _controller.error != null) {
widget.onError!(_controller.error!);
}
}
@override
Widget build(BuildContext context) {
final handler = widget.onPressed == null ? null : _trigger;
return widget.builder(context, _controller.busy, _controller.busy ? null : handler);
}
}
class _InlineErrorWrapper extends StatelessWidget {
final AsyncActionController? controller;
final Widget child;
const _InlineErrorWrapper({required this.controller, required this.child});
@override
Widget build(BuildContext context) {
final c = controller;
if (c == null) return child;
return AnimatedBuilder(
animation: c,
builder: (context, _) {
final err = c.error;
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
child,
if (err != null) ...[
const SizedBox(height: 8),
Text(
err,
textAlign: TextAlign.center,
style: TextStyle(color: Theme.of(context).colorScheme.error, fontSize: 13),
),
],
],
);
},
);
}
}
@@ -0,0 +1,49 @@
part of '../async_action_button.dart';
class AsyncTextButton extends StatelessWidget {
final AsyncActionCallback? onPressed;
final Widget child;
final AsyncActionController? controller;
final AsyncErrorBuilder? errorBuilder;
final void Function(String message)? onError;
final VoidCallback? onSuccess;
final bool showInlineError;
const AsyncTextButton({
required this.onPressed,
required this.child,
this.controller,
this.errorBuilder,
this.onError,
this.onSuccess,
this.showInlineError = true,
super.key,
});
@override
Widget build(BuildContext context) => _AsyncMixin(
onPressed: onPressed,
controller: controller,
errorBuilder: errorBuilder,
onError: onError,
onSuccess: onSuccess,
builder: (context, busy, handler) {
final content = busy
? Row(
mainAxisSize: MainAxisSize.min,
children: [
AppProgressIndicator.small(
color: Theme.of(context).colorScheme.primary,
),
const SizedBox(width: 8),
child,
],
)
: child;
return _InlineErrorWrapper(
controller: controller,
child: TextButton(onPressed: handler, child: content),
);
},
);
}
+13 -16
View File
@@ -1,7 +1,8 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../../utils/clipboard_helper.dart';
class JsonViewer extends StatelessWidget {
final String title;
@@ -19,30 +20,26 @@ class JsonViewer extends StatelessWidget {
child: Text(format(data)),
),
);
static final _encoder = const JsonEncoder.withIndent(' ');
static String format(Map<String, dynamic> jsonInput) => _encoder.convert(jsonInput);
static void asDialog(BuildContext context, Map<String, dynamic> dataMap) {
showDialog(context: context, builder: (context) => AlertDialog(
showDialog(context: context, builder: (dialogCtx) => AlertDialog(
scrollable: true,
title: const Row(children: [Icon(Icons.bug_report_outlined), Text('Rohdaten')]),
content: Text(JsonViewer.format(dataMap)),
actions: [
TextButton(onPressed: () {
Clipboard.setData(ClipboardData(text: JsonViewer.format(dataMap))).then((value) {
if (!context.mounted) return;
showDialog(context: context, builder: (context) => const AlertDialog(content: Text('Formatiertes JSON wurde erfolgreich in deiner Zwischenlage abgelegt.')));
});
}, child: const Text('Kopieren')),
TextButton(onPressed: () {
Clipboard.setData(ClipboardData(text: dataMap.toString())).then((value) {
if (!context.mounted) return;
showDialog(context: context, builder: (context) => const AlertDialog(content: Text('Unformatiertes JSON wurde erfolgreich in deiner Zwischenablage abgelegt.')));
});
}, child: const Text('Inline Kopieren')),
TextButton(onPressed: () => Navigator.of(context).pop(), child: const Text('Schließen'))
TextButton(
onPressed: () => copyToClipboard(dialogCtx, JsonViewer.format(dataMap), successMessage: 'Formatiertes JSON kopiert'),
child: const Text('Kopieren'),
),
TextButton(
onPressed: () => copyToClipboard(dialogCtx, dataMap.toString(), successMessage: 'Inline JSON kopiert'),
child: const Text('Inline Kopieren'),
),
TextButton(onPressed: () => Navigator.of(dialogCtx).pop(), child: const Text('Schließen'))
],
));
}
+34
View File
@@ -0,0 +1,34 @@
import 'package:flutter/material.dart';
/// Shows a modal bottom sheet for a detail view (appointment, file, lesson,
/// custom event, etc.). All detail sheets in the app share this layout: drag
/// handle on top, default theme background, optional ListTile-style header
/// followed by a divider, scrollable body below.
void showDetailsBottomSheet(
BuildContext context, {
Widget? header,
required List<Widget> Function(BuildContext sheetContext) children,
}) {
showModalBottomSheet<void>(
context: context,
isScrollControlled: true,
showDragHandle: true,
useSafeArea: true,
builder: (sheetContext) => SafeArea(
child: SingleChildScrollView(
padding: const EdgeInsets.only(bottom: 16),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
if (header != null) ...[
header,
const Divider(height: 1),
],
...children(sheetContext),
],
),
),
),
);
}
+3 -11
View File
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../utils/clipboard_helper.dart';
class InfoDialog {
/// Shows a single-text dialog. When [copyable] is true (default for error
@@ -26,16 +27,7 @@ class InfoDialog {
actions: [
if (copyable)
TextButton.icon(
onPressed: () async {
await Clipboard.setData(ClipboardData(text: info));
if (!dialogContext.mounted) return;
ScaffoldMessenger.of(dialogContext).showSnackBar(
const SnackBar(
content: Text('In Zwischenablage kopiert'),
duration: Duration(seconds: 2),
),
);
},
onPressed: () => copyToClipboard(dialogContext, info),
icon: const Icon(Icons.copy_outlined, size: 18),
label: const Text('Kopieren'),
),