API form fixes (#937)

* API form fixes

- Await form completion
- Improved error code handling
- Additional testing

* improve error message display in forms

* add text
This commit is contained in:
Oliver authored and GitHub committed 2026-11-22 22:32:05 +11:00
1 parent e9601116b4
commit ed9590cddf
8 files changed
+585 -31

No files matched your search

+85
View File
@@ -1,4 +1,7 @@
import "dart:convert";
import "package:flutter/material.dart";
import "package:flutter/services.dart";
import "package:flutter_tabler_icons/flutter_tabler_icons.dart";
import "package:one_context/one_context.dart";
@@ -8,6 +11,7 @@ import "package:inventree/helpers.dart";
import "package:inventree/l10.dart";
import "package:inventree/preferences.dart";
import "package:inventree/widget/snacks.dart";
/*
* Launch a dialog allowing the user to select from a list of options
@@ -290,6 +294,87 @@ Future<void> showErrorDialog(
});
}
/*
* Format the data returned in an API response for display
* (JSON data is pretty-printed)
*/
String formatResponseData(dynamic data) {
if (data is Map || data is List) {
try {
return const JsonEncoder.withIndent(" ").convert(data);
} catch (_) {
// Fall through to the string representation
}
}
return data?.toString() ?? "";
}
/*
* Construct a view of the full data returned in an API response
*/
Widget buildResponseDataContent(APIResponse response) {
return ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 400),
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"${L10().statusCode}: ${response.statusCode}",
style: const TextStyle(fontWeight: FontWeight.bold),
),
if (response.url.isNotEmpty) Text(response.url),
const SizedBox(height: 8),
Text(
L10().responseData,
style: const TextStyle(fontWeight: FontWeight.bold),
),
SelectableText(
formatResponseData(response.data),
style: const TextStyle(fontFamily: "monospace", fontSize: 12),
),
],
),
),
);
}
/*
* Display the full data returned in an API response (e.g. form errors)
*/
Future<void> showResponseDataDialog(String title, APIResponse response) async {
if (!hasContext()) {
return;
}
final String data = formatResponseData(response.data);
OneContext().showDialog(
builder: (context) => AlertDialog(
icon: Icon(TablerIcons.info_circle, color: COLOR_DANGER),
title: Text(title),
content: buildResponseDataContent(response),
actions: [
TextButton(
child: Text(L10().copyToClipboard),
onPressed: () {
Clipboard.setData(ClipboardData(text: data));
showSnackIcon(L10().copiedToClipboard, success: true);
},
),
TextButton(
child: Text(L10().ok),
onPressed: () {
Navigator.pop(context);
},
),
],
),
);
}
/*
* Display a message indicating the nature of a server / API error
*/
+36 -11
View File
@@ -12,6 +12,9 @@ OverlayEntry? _currentSnackOverlay;
/*
* Display a configurable 'snackbar' at the top of the screen
*
* If both [icon] and [onAction] are provided, tapping the icon runs the action
* (and the text action button is only shown if [actionText] is provided)
*/
void showSnackIcon(
String text, {
@@ -50,17 +53,22 @@ void showSnackIcon(
}
}
final String actionLabel = actionText ?? L10().details;
// An action icon replaces the default text action button
final bool iconAction = onAction != null && icon != null;
final String? actionLabel = (iconAction && actionText == null)
? null
: actionText ?? L10().details;
final Duration duration = Duration(seconds: onAction == null ? 5 : 10);
late OverlayEntry entry;
entry = OverlayEntry(
builder: (ctx) => _TopSnackBar(
builder: (ctx) => TopSnackBar(
text: text,
icon: icon,
backgroundColor: backgroundColor,
onAction: onAction,
actionText: actionLabel,
iconAction: iconAction,
duration: duration,
onDismiss: () {
entry.remove();
@@ -75,8 +83,12 @@ void showSnackIcon(
Overlay.of(context).insert(entry);
}
class _TopSnackBar extends StatefulWidget {
const _TopSnackBar({
/*
* Snackbar widget displayed by showSnackIcon()
* (public for testing, as snackbars are not displayed in tests)
*/
class TopSnackBar extends StatefulWidget {
const TopSnackBar({
required this.text,
required this.backgroundColor,
required this.duration,
@@ -84,6 +96,7 @@ class _TopSnackBar extends StatefulWidget {
this.icon,
this.onAction,
this.actionText,
this.iconAction = false,
});
final String text;
@@ -91,6 +104,10 @@ class _TopSnackBar extends StatefulWidget {
final Color backgroundColor;
final Function()? onAction;
final String? actionText;
// Tapping the icon runs the action
final bool iconAction;
final Duration duration;
final VoidCallback onDismiss;
@@ -98,7 +115,7 @@ class _TopSnackBar extends StatefulWidget {
_TopSnackBarState createState() => _TopSnackBarState();
}
class _TopSnackBarState extends State<_TopSnackBar>
class _TopSnackBarState extends State<TopSnackBar>
with SingleTickerProviderStateMixin {
late final AnimationController _controller;
late final Animation<Offset> _slideAnimation;
@@ -130,6 +147,11 @@ class _TopSnackBarState extends State<_TopSnackBar>
super.dispose();
}
void _runAction() {
_dismiss();
widget.onAction?.call();
}
void _dismiss() {
if (_dismissed) return;
_dismissed = true;
@@ -170,7 +192,7 @@ class _TopSnackBarState extends State<_TopSnackBar>
style: const TextStyle(color: Colors.white),
),
),
if (widget.icon != null)
if (widget.icon != null && !widget.iconAction)
Padding(
padding: const EdgeInsets.only(left: 8),
child: Icon(widget.icon, color: Colors.white),
@@ -179,12 +201,15 @@ class _TopSnackBarState extends State<_TopSnackBar>
),
),
),
if (widget.onAction != null)
if (widget.iconAction)
IconButton(
icon: Icon(widget.icon, color: Colors.white),
tooltip: widget.actionText ?? L10().details,
onPressed: _runAction,
),
if (widget.onAction != null && widget.actionText != null)
TextButton(
onPressed: () {
_dismiss();
widget.onAction!();
},
onPressed: _runAction,
child: Text(
widget.actionText ?? "",
style: const TextStyle(color: Colors.white),