first commit
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../../providers/auth_provider.dart';
|
||||
import '../add/add_page.dart';
|
||||
import '../home/home_page.dart';
|
||||
import '../login/login_page.dart';
|
||||
import '../mine/mine_page.dart';
|
||||
import '../statistics/statistics_page.dart';
|
||||
|
||||
class MainShell extends ConsumerStatefulWidget {
|
||||
const MainShell({super.key});
|
||||
|
||||
@override
|
||||
ConsumerState<MainShell> createState() => _MainShellState();
|
||||
}
|
||||
|
||||
class _MainShellState extends ConsumerState<MainShell> {
|
||||
int _index = 0; // 默认选中「首页」
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
// 兜底:未登录直接回登录页
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (!ref.read(authProvider).isLoggedIn) {
|
||||
_goLogin();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void _goLogin() {
|
||||
Navigator.of(context).pushAndRemoveUntil(
|
||||
MaterialPageRoute(builder: (_) => const LoginPage()),
|
||||
(route) => false,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// IndexedStack 保证四个页面常驻:写操作后首页/统计的数据失效能立即重取,
|
||||
// 用户切回时看到的就是最新数据。
|
||||
const pages = [
|
||||
HomePage(),
|
||||
AddPage(),
|
||||
StatisticsPage(),
|
||||
MinePage(),
|
||||
];
|
||||
return Scaffold(
|
||||
body: IndexedStack(index: _index, children: pages),
|
||||
bottomNavigationBar: NavigationBar(
|
||||
selectedIndex: _index,
|
||||
onDestinationSelected: (i) => setState(() => _index = i),
|
||||
backgroundColor: Colors.white,
|
||||
indicatorColor: const Color(0x33C8956E),
|
||||
destinations: const [
|
||||
NavigationDestination(icon: Icon(Icons.home_outlined), selectedIcon: Icon(Icons.home), label: '首页'),
|
||||
NavigationDestination(icon: Icon(Icons.add_circle_outline), selectedIcon: Icon(Icons.add_circle), label: '记账'),
|
||||
NavigationDestination(icon: Icon(Icons.bar_chart_outlined), selectedIcon: Icon(Icons.bar_chart), label: '统计'),
|
||||
NavigationDestination(icon: Icon(Icons.person_outline), selectedIcon: Icon(Icons.person), label: '我的'),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user