gpt4 book ai didi

mobile - Flutter 无法更改路由,因为 undefined name context with PopupMenuButton 如何解决?

转载 作者:IT王子 更新时间:2023-10-29 06:58:20 26 4
gpt4 key购买 nike

我想单击一个项目菜单 (PopupMenuItem) 并使用 Navigator.push 转到另一条路线,但方法内未定义上下文。

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {

final List<Choice> choices = const <Choice>[
const Choice(title: 'Settings', icon: Icons.settings),
const Choice(title: 'Log out', icon: Icons.exit_to_app),
];



@override
Widget build(BuildContext context) {
final title = 'MyTitle';

return MaterialApp(
title: title,
home: Scaffold(
appBar: AppBar(
title: Text(title),
actions: <Widget>[
PopupMenuButton<Choice>(
onSelected: onItemMenuPress,
itemBuilder: (BuildContext context) {
return choices.map((Choice choice) {
return PopupMenuItem<Choice>(
value: choice,
child: Row(
children: <Widget>[
Icon(
choice.icon,
),
Container(
width: 10.0,
),
Text(
choice.title,
),
],
));
}).toList();
},
),
],
),
body: Text("Hello world")
),
);

}

void onItemMenuPress(Choice choice) {
if (choice.title == 'Log out') {
print("Logout");
Navigator.push(context, MaterialPageRoute(builder: (context) => LogoutRoute()));
}
}

}

class Choice {
const Choice({this.title, this.icon});

final String title;
final IconData icon;
}

class LogoutRoute extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Logout"),
),
body: Center(
child: Text("Screen"),
),
);
}
}

我尝试以这种方式在 onItemMenuPress 中传递上下文:

void onItemMenuPress(Choice choice, BuildContext context)

但是:

onSelected: onItemMenuPress(context)

不工作。

这种方法都行不通:

onSelected: (Choice c) { Navigator.push(context, MaterialPageRoute(builder: (context) => LogoutRoute())); }

我正在学习本教程: https://medium.com/flutter-community/building-a-chat-app-with-flutter-and-firebase-from-scratch-9eaa7f41782e

他的代码片段(与我的相似)似乎对他有用: https://github.com/duytq94/flutter-chat-demo/blob/master/lib/main.dart

我指的是第 235 行(onSelected)和第 199-205 行(实际的 onItemMenuPress 方法)

这怎么可能?我该如何缓解?谢谢

最佳答案

给你:

MyApp    <------ context
--> MaterialApp
(--> Navigator built within MaterialApp)
--> Scaffold
--> App Bar
--> ...

因此,当您使用上下文查找导航器时,您使用的是不在导航器下的 MyApp 的上下文。所以我们可以创建一个新的 Stateless 或 Stateful Widget 子类来包含您的 Scaffold,因为其中的构建函数将指向该级别,或者我们可以使用 Builder并定义生成器回调(具有指向生成器的上下文)以返回 Scaffold

工作代码我们创建了新的子类 - HomeScreen :

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final title = 'MyTitle';
return MaterialApp(
title: title,
home: HomeScreen(title),
);
}
}

class HomeScreen extends StatelessWidget {
final String title;

HomeScreen(this.title);

final List<Choice> choices = const <Choice>[
const Choice(title: 'Settings', icon: Icons.settings),
const Choice(title: 'Log out', icon: Icons.exit_to_app),
];

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
actions: <Widget>[
PopupMenuButton<Choice>(
onSelected: (val) => onItemMenuPress(val, context),
itemBuilder: (BuildContext context) {
return choices.map((Choice choice) {
return PopupMenuItem<Choice>(
value: choice,
child: Row(
children: <Widget>[
Icon(
choice.icon,
),
Container(
width: 10.0,
),
Text(
choice.title,
),
],
));
}).toList();
},
),
],
),
body: Text("Hello world"));
}

void onItemMenuPress(Choice choice, BuildContext context) {
if (choice.title == 'Log out') {
print("Logout");
Navigator.push(
context, MaterialPageRoute(builder: (context) => LogoutRoute()));
}
}
}

class Choice {
const Choice({this.title, this.icon});

final String title;
final IconData icon;
}

class LogoutRoute extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Logout"),
),
body: Center(
child: Text("Screen"),
),
);
}
}

关于mobile - Flutter 无法更改路由,因为 undefined name context with PopupMenuButton 如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54811646/

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