🐛 Fix navigation state loss

This commit is contained in:
LittleSheep 2024-04-14 00:10:22 +08:00
parent 1f415ec3ac
commit e086aeebe4
3 changed files with 19 additions and 0 deletions

View File

@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:solian/providers/auth.dart';
import 'package:solian/providers/navigation.dart';
import 'package:solian/router.dart';
import 'package:solian/utils/timeago.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
@ -32,6 +33,7 @@ class SolianApp extends StatelessWidget {
OverlayEntry(builder: (context) {
return MultiProvider(
providers: [
Provider(create: (_) => NavigationProvider()),
Provider(create: (_) => AuthProvider()),
],
child: child,

View File

@ -0,0 +1,3 @@
class NavigationProvider {
int selectedIndex = 0;
}

View File

@ -1,4 +1,6 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:solian/providers/navigation.dart';
import 'package:solian/router.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
@ -14,9 +16,21 @@ class _SolianNavigationDrawerState extends State<SolianNavigationDrawer> {
void _onSelect(String name, int idx) {
setState(() => _selectedIndex = idx);
context.read<NavigationProvider>().selectedIndex = idx;
router.goNamed(name);
}
@override
void initState() {
Future.delayed(Duration.zero, () {
setState(() {
_selectedIndex = context.read<NavigationProvider>().selectedIndex;
});
});
super.initState();
}
@override
Widget build(BuildContext context) {
final navigationItems = [