fix: show success snackbar from HomeScreen instead of dying dialog
The AddCarDialog now pops with a boolean result instead of trying to show a SnackBar on its own context after Navigator.pop() disposes it. HomeScreen shows the snackbar using its own stable mounted context.
This commit is contained in:
parent
16d1145efd
commit
c104ec16ed
1 changed files with 19 additions and 10 deletions
|
|
@ -411,12 +411,23 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
setState(() => _isBusy = false);
|
setState(() => _isBusy = false);
|
||||||
|
|
||||||
|
if (data != null) {
|
||||||
await showDialog<void>(
|
await showDialog<void>(
|
||||||
context: context,
|
context: context,
|
||||||
builder: (_) => data != null
|
builder: (_) => _AlreadyExistsDialog(hwId: hwId),
|
||||||
? _AlreadyExistsDialog(hwId: hwId)
|
|
||||||
: _AddCarDialog(hwId: hwId),
|
|
||||||
);
|
);
|
||||||
|
} else {
|
||||||
|
// Dialog returns true if the car was added successfully.
|
||||||
|
final added = await showDialog<bool>(
|
||||||
|
context: context,
|
||||||
|
builder: (_) => _AddCarDialog(hwId: hwId),
|
||||||
|
);
|
||||||
|
if (added == true && mounted) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(content: Text('$hwId added to your collection! 🎉')),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
setState(() => _isBusy = false);
|
setState(() => _isBusy = false);
|
||||||
|
|
@ -469,10 +480,8 @@ class _AddCarDialogState extends State<_AddCarDialog> {
|
||||||
'user_id': supabase.auth.currentUser!.id,
|
'user_id': supabase.auth.currentUser!.id,
|
||||||
});
|
});
|
||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
Navigator.of(context).pop();
|
// Return true to signal success — snackbar shown by HomeScreen.
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
Navigator.of(context).pop(true);
|
||||||
SnackBar(content: Text('${widget.hwId} added to your collection! 🎉')),
|
|
||||||
);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
setState(() => _isAdding = false);
|
setState(() => _isAdding = false);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue