gpt4 book ai didi

dart - 从抽屉路由到新主体不起作用

转载 作者:行者123 更新时间:2023-12-03 02:47:53 25 4
gpt4 key购买 nike

我希望应用栏保留在应用程序的顶部,而不会在更改选项卡时更改或动画化,所以我这样设置我的代码,这是 main.dart:

import 'package:flutter/material.dart';

import 'package:stewart_inc_app/tabs/first.dart';
import 'package:stewart_inc_app/tabs/second.dart';
import 'package:stewart_inc_app/tabs/third.dart';

void main() {
runApp(MaterialApp(
home: Home(),
));
}

class Home extends StatefulWidget {
@override
HomeState createState() => HomeState();
}

class HomeState extends State<Home> {
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(
title: new Text(
"Hello World",
),
),
body: Navigator(
initialRoute: 'tabs/third',
onGenerateRoute: (RouteSettings settings) {
WidgetBuilder builder;
switch (settings.name) {
case 'tabs/first':
builder = (BuildContext _) => FirstTab();
break;
case 'tabs/second':
builder = (BuildContext _) => SecondTab();
break;
case 'tabs/third':
builder = (BuildContext _) => ThirdTab();
break;
default:
throw Exception('Invalid route: ${settings.name}');
}
return MaterialPageRoute(builder: builder, settings: settings);
}),
drawer: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
child: Text(
'Select Tab',
style: TextStyle(color: Colors.white),
),
decoration: BoxDecoration(
color: Colors.black,
),
),
ListTile(
title: Text('First Tab'),
onTap: () {
Navigator.pushNamed(context, 'tabs/first');
},
),
ListTile(
title: Text('Second Tab'),
onTap: () {
Navigator.pushNamed(context, 'tabs/second');
},
),
ListTile(
title: Text('Third Tab'),
onTap: () {
Navigator.pushNamed(context, 'tabs/third');
},
),
],
),
),
);
}
}

这是第三个标签,third.dart:

import 'package:flutter/material.dart';

class ThirdTab extends StatelessWidget {

static const String routeName = "/third";

@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Container(
child: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new RaisedButton(
onPressed: () {
Navigator.pushNamed(context, 'tabs/second');
},
child: new Text("Second Tab"),
),
],
),
),
),
);
}
}

当我在 third.dart 中触发 Navigator.pushNamed(context, 'tabs/second'); 时,它工作正常并为第二个选项卡的页面更改设置动画。但是,当我从 main.dart 抽屉中触发相同的代码时,出现以下错误:

flutter: The following assertion was thrown while handling a gesture: flutter: Could not find a generator for route "tabs/second" in the _WidgetsAppState.

如果有人能帮助我解决这个问题,我们将不胜感激。

最佳答案

这可能是 Navigator.pushNamed(context, 'tabs/second'); 的错误上下文

GlobalKey 传递给 MaterialApp.navigatorKey https://docs.flutter.io/flutter/material/MaterialApp/navigatorKey.html并在调用导航方法时使用此键获取 Navigator 的上下文。

关于dart - 从抽屉路由到新主体不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54389261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com