昨天我发现扑了一下,虽然我遇到了一个问题,但我已经尝试了一下。我已经按照教程实现了导航抽屉(this one),抽屉打开很好,所有按钮都会改变他们应该更改的内容但是调用Navigator.pop(上下文)或Navigator.of(上下文)。 pop()(两者)的行为如下:
在点击"字符"
之前点击"字符"
我在网上搜索过,但我找不到有同样问题的人:/这是我的代码:
main.dart:
import 'package:flutter/material.dart';
import 'package:dnd/home.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'DnD',
home: new Home()
);
}
}
home.dart:
import 'package:flutter/material.dart';
import 'package:dnd/home_fragment.dart';
import 'package:dnd/char_fragment.dart';
class Home extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return new HomeState();
}
}
class HomeState extends State<Home> {
int selectedDrawerIndex = 0;
getDrawerItemWidget(int pos) {
switch(pos) {
case 0:
return new HomeFragment();
case 1:
return new CharFragment();
default:
return new Text('ERROR');
}
}
onSelectItem(int index) {
setState(() => selectedDrawerIndex = index);
print(Navigator.of(context).pop());
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'DnD',
home: new Scaffold(
appBar: new AppBar(
title: new Text(
'DnD'
)
),
drawer: new Drawer(
child: new Column(
children: <Widget>[
new UserAccountsDrawerHeader(
accountName: new Text(
'bla'
),
accountEmail: null,
),
new Column(
children: <Widget>[
new ListTile(
title: new Text(
'Home'
),
leading: new Icon(
Icons.home
),
selected: 0 == selectedDrawerIndex,
onTap: () => onSelectItem(0),
),
new ListTile(
title: new Text(
'Characters'
),
leading: new Icon(
Icons.person
),
selected: 1 == selectedDrawerIndex,
onTap: () => onSelectItem(1),
)
],
),
],
),
),
body: getDrawerItemWidget(selectedDrawerIndex)
)
);
}
}
home_fragment.dart:
import 'package:flutter/material.dart';
class HomeFragment extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Center(
child: new Text(
'moin'
)
);
}
}
char_fragment.dart:
import 'package:flutter/material.dart';
class CharFragment extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return new CharState();
}
}
class CharState extends State<CharFragment> {
characterNew() async {
await showDialog<bool>(
context: context,
child: new AlertDialog(
title: new Text('Create new Character?'),
actions: <Widget>[
new FlatButton(
child: new Text('Yes'),
onPressed: () => Navigator.of(context).pop(true)
),
new FlatButton(
child: new Text('No'),
onPressed: () => Navigator.of(context).pop(false)
)
],
)
);
}
@override
Widget build(BuildContext context) {
return new Center(
child: new GridView.count(
crossAxisCount: 3,
crossAxisSpacing: 10.0,
mainAxisSpacing: 10.0,
children: <Widget>[
new RaisedButton.icon(
onPressed: characterNew,
icon: new Icon(Icons.add, size: 80.0,),
label: new Text('')
)
],
)
);
}
}
答案 0 :(得分:6)
您的代码中存在两个错误:
MaterialApp
两次,因为它拥有您的根配置,您应该只有一个MaterialApp
实例。Navigator
堆栈上推送任何路由,因此您尝试从空列表中pop
(即,堆栈中没有任何内容可以弹出)。为了演示,您使用Scaffold
控制setState
正文的内容,以便在Home
和Charaters
小部件之间切换,他们实际上看不到每个其他(如果您选择Home
窗口小部件,则不会构建您的Char
窗口小部件,现在,如果您选择Char
窗口小部件,则Home
窗口小部件已完全替换它<) / p>
答案 1 :(得分:2)
在home.dart构建覆盖中。尝试返回Scaffold而不是Material app的另一个实例。您现在可以在材质应用的正文中返回材质应用。你应该只返回那里的脚手架。