如何解决Flutter namedRoutes 不会导航到点击的屏幕,但不会抛出错误日志
长期潜伏在这里 我的情况是我一直在尝试使用带有自定义“NavLink”组件的 namedRoutes 来添加到我的抽屉中,但链接没有导航到单击的屏幕。当我点击它们时,它们不会抛出错误日志,所以我不知道发生了什么。
main.dart
import 'package:Flutter/material.dart';
import 'package:hire_me/homepage.dart';
import 'package:hire_me/screens/notFound.dart';
import 'package:hire_me/screens/todoList.dart';
import 'package:hire_me/screens/nodeJs.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My Portfolio',theme: ThemeData(
primarySwatch: Colors.blue,textTheme: TextTheme(
headline1: TextStyle(
fontSize: 36.0,fontWeight: FontWeight.bold,color: Colors.black),headline2:
TextStyle(fontSize: 18.0,fontWeight: FontWeight.bold),bodyText1:
TextStyle(fontSize: 12.0,fontWeight: FontWeight.normal))),initialRoute: '/',routes: {
'/': (context) => HomePage(),'/todoList': (context) => TodoList(),'/nodeJs': (context) => NodeJs(),'/notFound': (context) => NotFound(),});
}
}
首页组件
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Carlos Gumucio'),centerTitle: true,),drawer: Drawer(
child: ListView(
padding: EdgeInsets.zero,children: <Widget>[
DrawerHeader(
decoration: Boxdecoration(
color: Colors.blue,child: Text('More from me')),NavLink(title: 'Todo List',route: '/todoList'),NavLink(title: 'NodeJs API',route: '/nodeJs'),],)),body: SingleChildScrollView(...
navLink.dart 组件
import 'package:Flutter/material.dart';
class NavLink extends StatelessWidget {
const NavLink({Key? key,this.title = 'link here',this.route = '/notFound'})
: super(key: key);
final String title;
final String route;
@override
Widget build(BuildContext context) {
return ListTile(
title: Text(title),onTap: () {
// go to route ... Navigator.push(context,route)
Navigator.pushNamed(context,route);
// close the nav after navigating
Navigator.pop(context);
});
}
}
pubspec.yaml
name: hire_me
description: Reach me hub.
publish_to: 'none'
version: 1.0.0+1
environment:
sdk: ">=2.12.0 <3.0.0"
dependencies:
Flutter:
sdk: Flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
url_launcher: ^6.0.4
material_design_icons_Flutter: ^4.0.5955
google_fonts: ^2.0.0
dev_dependencies:
Flutter_test:
sdk: Flutter
# For information on the generic Dart part of this file,see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter.
Flutter:
# The following line ensures that the Material Icons font is
# included with your application,so that you can use the icons in
# the material Icons class.
uses-material-design: true
assets:
- assets/images/
扑医生
[√] Flutter (Channel beta,2.2.0-10.3.pre,on Microsoft Windows [Versión 10.0.19042.985],locale
es-CL)
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[√] Chrome - develop for the web
[√] Android Studio
[√] VS Code (version 1.56.0)
[√] Connected device (2 available)
• No issues found!
提前,谢谢!
解决方法
嗯,我看到你在按下屏幕后立即弹出:
@override
Widget build(BuildContext context) {
return ListTile(
title: Text(title),onTap: () {
// go to route ... Navigator.push(context,route)
Navigator.pushNamed(context,route);
// close the nav after navigating
Navigator.pop(context); // This will pop your last pushed route
});
}
尝试用 Navigator.pop(context);
注释掉该行。
编辑 1:
因为在您推送新路线之前关闭了抽屉:
@override
Widget build(BuildContext context) {
return ListTile(
title: Text(title),onTap: () {
// close the nav before navigating
Navigator.pop(context); // This will pop your last pushed
// go to route ... Navigator.push(context,route);
route
});
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。