🐛 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:flutter/material.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:solian/providers/auth.dart'; import 'package:solian/providers/auth.dart';
import 'package:solian/providers/navigation.dart';
import 'package:solian/router.dart'; import 'package:solian/router.dart';
import 'package:solian/utils/timeago.dart'; import 'package:solian/utils/timeago.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart';
@ -32,6 +33,7 @@ class SolianApp extends StatelessWidget {
OverlayEntry(builder: (context) { OverlayEntry(builder: (context) {
return MultiProvider( return MultiProvider(
providers: [ providers: [
Provider(create: (_) => NavigationProvider()),
Provider(create: (_) => AuthProvider()), Provider(create: (_) => AuthProvider()),
], ],
child: child, 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:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:solian/providers/navigation.dart';
import 'package:solian/router.dart'; import 'package:solian/router.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.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) { void _onSelect(String name, int idx) {
setState(() => _selectedIndex = idx); setState(() => _selectedIndex = idx);
context.read<NavigationProvider>().selectedIndex = idx;
router.goNamed(name); router.goNamed(name);
} }
@override
void initState() {
Future.delayed(Duration.zero, () {
setState(() {
_selectedIndex = context.read<NavigationProvider>().selectedIndex;
});
});
super.initState();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final navigationItems = [ final navigationItems = [