feat: 添加goRouter

This commit is contained in:
2026-07-23 18:28:12 +08:00
parent 5a6eff5e12
commit 725f911a6f
4 changed files with 75 additions and 6 deletions

View File

@@ -0,0 +1,13 @@
import 'package:flutter/material.dart';
class LoginPage extends StatelessWidget {
const LoginPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Login')),
body: Center(child: Text('Login Page')),
);
}
}

View File

@@ -1,16 +1,50 @@
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'com/xiaoyan/main/LoginPage.dart';
void main() {
runApp(const MainApp());
runApp(MainApp());
}
class MainApp extends StatelessWidget {
const MainApp({super.key});
MainApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: Scaffold(body: Center(child: Text('Hello World!'))),
);
return MaterialApp.router(routerConfig: _route);
}
final _route = GoRouter(
routes: <RouteBase>[
GoRoute(path: '/login', builder: (context, state) => const LoginPage()),
StatefulShellRoute.indexedStack(
builder: (context, state, navigationShell) => Scaffold(
body: navigationShell,
bottomNavigationBar: BottomNavigationBar(
items: const [
BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
BottomNavigationBarItem(
icon: Icon(Icons.settings),
label: 'Settings',
),
],
onTap: (index) {
navigationShell.goBranch(index); // 跳转到GoRoute中定义的分支
},
),
),
branches: <StatefulShellBranch>[
StatefulShellBranch(
routes: <RouteBase>[
GoRoute(
path: '/',
builder: (context, state) => const HomePage(),
),
],
),
],
),
],
);
}