Files
sharedinbox/lib/ui/screens/mailbox_list_screen.dart
cd8c930000 fix: use Builder to get descendant context for Scaffold.of() in bottom nav (#403)
## Summary

Fixes the crash reported in #397: `Scaffold.of() called with a context that does not contain a Scaffold.`

- `Scaffold.of(context)` was called in the `onPressed` of the bottom-nav menu `IconButton` using the widget's own `build` context. That context is the *parent* of the `Scaffold` being returned, so Flutter correctly throws.
- Fix: wrap the `IconButton` in a `Builder`, which provides a child `ctx` that is a proper descendant of the `Scaffold`. `Scaffold.of(ctx)` then resolves correctly.

## Test plan

- [ ] Run app with bottom menu position enabled, tap the hamburger icon — drawer opens without crashing.
- [ ] Run app with top menu position — no regression (bottom nav is not rendered).

Closes #397

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Thomas SharedInbox <sharedinbox@thomas-guettler.de>
Reviewed-on: https://codeberg.org/guettli/sharedinbox/pulls/403
2026-06-04 06:16:24 +02:00

192 lines
6.6 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:sharedinbox/core/models/email.dart';
import 'package:sharedinbox/core/models/mailbox.dart';
import 'package:sharedinbox/core/models/user_preferences.dart';
import 'package:sharedinbox/core/repositories/email_repository.dart';
import 'package:sharedinbox/di.dart';
import 'package:sharedinbox/ui/widgets/folder_drawer.dart';
class MailboxListScreen extends ConsumerWidget {
const MailboxListScreen({super.key, required this.accountId});
final String accountId;
@override
Widget build(BuildContext context, WidgetRef ref) {
final mailboxRepo = ref.watch(mailboxRepositoryProvider);
final emailRepo = ref.watch(emailRepositoryProvider);
final accountAsync = ref.watch(accountByIdProvider(accountId));
final prefs =
ref.watch(userPreferencesProvider).value ?? const UserPreferences();
final menuAtBottom = prefs.menuPosition == MenuPosition.bottom;
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: !menuAtBottom,
title: const Text('Folders'),
actions: [
IconButton(
icon: const Icon(Icons.search),
tooltip: 'Search everywhere',
onPressed: () => context.push('/accounts/$accountId/search'),
),
accountAsync.when(
loading: () => const SizedBox.shrink(),
error: (_, __) => const SizedBox.shrink(),
data: (account) => Padding(
padding: const EdgeInsets.only(right: 12),
child: Center(
child: Text(
account?.displayName ?? '',
style: Theme.of(context).textTheme.bodySmall,
),
),
),
),
],
),
drawer: FolderDrawer(accountId: accountId),
bottomNavigationBar: menuAtBottom
? BottomAppBar(
child: Row(
children: [
Builder(
builder: (ctx) => IconButton(
icon: const Icon(Icons.menu),
tooltip: 'Open folders',
onPressed: () => Scaffold.of(ctx).openDrawer(),
),
),
],
),
)
: null,
body: Column(
children: [
// ── Failed-mutation banner ───────────────────────────────────────
StreamBuilder<List<FailedMutation>>(
stream: emailRepo.observeFailedMutations(accountId),
builder: (ctx, snap) {
final mutations = snap.data ?? [];
if (mutations.isEmpty) return const SizedBox.shrink();
return _FailedMutationBanner(
mutations: mutations,
emailRepo: emailRepo,
);
},
),
// ── Mailbox list ─────────────────────────────────────────────────
Expanded(
child: StreamBuilder(
stream: mailboxRepo.observeMailboxes(accountId),
builder: (ctx, snap) {
if (!snap.hasData) {
return const Center(child: CircularProgressIndicator());
}
final mailboxes = List.of(snap.data!)..sort(compareMailboxes);
return ListView.builder(
itemCount: mailboxes.length,
itemBuilder: (ctx, i) {
final mb = mailboxes[i];
final hasUnread = mb.unreadCount > 0;
return ListTile(
leading: const Icon(Icons.folder),
title: Text(
mb.name,
style: hasUnread
? const TextStyle(fontWeight: FontWeight.bold)
: null,
),
trailing: hasUnread
? Badge(label: Text('${mb.unreadCount}'))
: null,
onTap: () => context.push(
'/accounts/$accountId/mailboxes/${Uri.encodeComponent(mb.path)}/emails',
),
);
},
);
},
),
),
],
),
);
}
}
class _FailedMutationBanner extends StatelessWidget {
const _FailedMutationBanner({
required this.mutations,
required this.emailRepo,
});
final List<FailedMutation> mutations;
final EmailRepository emailRepo;
String _label(FailedMutation m) {
final noun = switch (m.changeType) {
'flag_seen' || 'flag_flagged' => 'flag change',
'move' => 'move',
'delete' => 'deletion',
_ => 'change',
};
return '${mutations.length} pending $noun${mutations.length > 1 ? 's' : ''} failed';
}
@override
Widget build(BuildContext context) {
return Material(
color: Theme.of(context).colorScheme.errorContainer,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
child: Row(
children: [
Icon(
Icons.warning_amber,
color: Theme.of(context).colorScheme.onErrorContainer,
size: 20,
),
const SizedBox(width: 8),
Expanded(
child: Text(
_label(mutations.first),
style: TextStyle(
color: Theme.of(context).colorScheme.onErrorContainer,
),
),
),
TextButton(
onPressed: () async {
for (final m in mutations) {
await emailRepo.retryMutation(m.id);
}
},
child: Text(
'Retry',
style: TextStyle(
color: Theme.of(context).colorScheme.onErrorContainer,
),
),
),
TextButton(
onPressed: () async {
for (final m in mutations) {
await emailRepo.discardMutation(m.id);
}
},
child: Text(
'Discard',
style: TextStyle(
color: Theme.of(context).colorScheme.onErrorContainer,
),
),
),
],
),
),
);
}
}