我正在为我的最终项目课程构建这个应用程序,该应用程序扫描二维码,然后在扫描二维码后重定向到另一个显示结果的 dart 页面。我开始实现底部导航栏和路线,并遵循了很多很酷的教程。我遇到的唯一问题是我的底部导航栏有两个选择,其中一个是扫描功能,另一个是帮助页面。扫描完成后,结果显示出来,但我希望能够选择底部工具栏上的扫描来执行另一次扫描,但它停留在同一个结果页面。我想我做错了什么,我尽力了,但我还是卡住了。如果有人能提供一些指导,我将不胜感激。
加载开始页,即闪屏页,然后扫描页允许扫描二维码,然后塑料页是显示结果的页面。
我尝试按照类似这样的教程进行操作。
https://codewithandrea.com/articles/flutter-bottom-navigation-bar-nested-routes-gorouter/
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:recycle/help.dart';
import 'package:recycle/main_p.dart';
import 'package:recycle/main_p_routes.dart';
import 'package:recycle/plastic.dart';
import 'package:recycle/scan.dart';
import 'package:recycle/main_splash_page.dart';
final _rootNavigatorKey = GlobalKey<NavigatorState>();
final router = GoRouter(
navigatorKey: _rootNavigatorKey,
initialLocation: Routes.start,
routes: [
GoRoute(
path: Routes.start,
builder: (BuildContext context, GoRouterState state) {
return const StartPage();
},
),
StatefulShellRoute.indexedStack(
builder:
(context, state, navigationShell) =>
LayoutScaffold(navigationShell: navigationShell),
branches: [
StatefulShellBranch(
routes: [
GoRoute(
path: Routes.scan,
builder: (context, state) => const ScanPage(),
routes: [
GoRoute(
path: Routes.plastic,
builder: (context, state) => const Plastic(),
),
],
),
],
),
StatefulShellBranch(
routes: [
GoRoute(
path: Routes.help,
builder: (context, state) => const Help(),
),
],
),
],
),
],
);
class Routes {
Routes._();
static const String start = '/start'; // start page
static const String scan = '/scan'; //scan page
static const String plastic = 'plastic';
static const String nestedPlastic = '/scan/plastic'; // plastic page
static const String help = '/help';
static const String aluminum = '/aluminum'; // help page
}
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:recycle/main_p_dest.dart';
class LayoutScaffold extends StatelessWidget {
const LayoutScaffold({required this.navigationShell, super.key});
final StatefulNavigationShell navigationShell;
@override
Widget build(BuildContext context) => Scaffold(
body: navigationShell,
bottomNavigationBar: NavigationBar(
selectedIndex: navigationShell.currentIndex,
onDestinationSelected: navigationShell.goBranch,
indicatorColor: Theme.of(context).primaryColor,
destinations:
destinations
.map(
(destination) => NavigationDestination(
icon: Icon(destination.icon),
label: destination.label,
selectedIcon: Icon(destination.icon, color: Colors.white),
),
)
.toList(),
),
);
}
import 'package:flutter/material.dart';
class Destination {
const Destination({required this.label, required this.icon});
final String label;
final IconData icon;
}
const destinations = [
Destination(label: 'Scan', icon: Icons.camera_rear),
Destination(label: 'Help', icon: Icons.help),
];