我正在开发Flutter/Dart上的一个小应用程序,以适应这门语言,因为我是新手。我正在检查以在我的应用程序栏中实现一个PopupMenuButton,我做得很正确。我正在尝试做的是获得不同的屏幕(小部件),这取决于从PopupMenuButton中选择了哪一项。例如: PopupMenuItems:设置、配置文件、注销。因此,只要用户选择其中之一,它就会重定向到另一个小部件(屏幕)。到目前为止,我实现了PopupMenuBotton,并且不同的选项在屏幕上呈现不同的值:
class _MyHomePage extends State<MyHomePage> {
String _selectedValue;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
// centers the title in the appBar
centerTitle: true,
title: Text(
"Alba's Playground",
// Mentioning the theme
style: Theme.of(context).textTheme.headline1,
),
actions: <Widget>[
PopupMenuButton(onSelected: (String str) {
setState(() {
_selectedValue = str;
});
}, itemBuilder: (BuildContext context) {
return <PopupMenuEntry<String>>[
PopupMenuItem(
child: Text(
"Item 1",
// Mentioning the theme
style: Theme.of(context).textTheme.headline2,
),
value: "Item1",
),
PopupMenuItem(
child: Text(
"Item 2",
style: Theme.of(context).textTheme.headline2,
),
value: "Item2",
),
PopupMenuItem(
child: Text(
"Item 3",
style: Theme.of(context).textTheme.headline2,
),
value: "Item3",
),
];
})
],
),
body: Container(
Text("$_selectedValue"),因此,只要用户访问选项"item1",就会呈现值"item1“,但不会更改屏幕。我想要实现的是不同按钮的不同屏幕。如果有人知道怎么做,或者可以给我一个提示,我将非常感激。
谢谢。
发布于 2020-08-07 16:23:02
使用这个:-
GestureDetector(onTap: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => SettingScreen()),
},
child://your child
);发布于 2020-08-07 16:20:05
看一看必须提供的setState((){})函数。此外,您还需要使用onTap:功能按钮。如果您要用作按钮的小部件不是这种情况,请查看GestureDetector。
https://stackoverflow.com/questions/63297780
复制相似问题