【问题标题】:Flutter Navigator Pop does not workFlutter Navigator Pop 不起作用
【发布时间】:2018-09-17 08:31:06
【问题描述】:

我昨天发现了颤振并尝试了一些,但我有一个问题。我按照教程实现了导航抽屉(this one),抽屉打开得很好,所有按钮都更改了它们应该更改的内容,但调用 Navigator.pop(context) 或 Navigator.of(context).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('')
          )
        ],
      )
    );
  }
}

【问题讨论】:

    标签: android dart flutter


    【解决方案1】:

    您的代码中有两处错误:

    1. 您使用了两次MaterialApp,您应该只有一个MaterialApp 实例,因为它包含您的根配置。
    2. 您没有在Navigator 堆栈上推送任何路由,因此您正在尝试从一个空列表中pop(即,堆栈内没有要弹出的内容)。

    为了演示,您正在使用setState 控制Scaffold 正文的内容,因此您在HomeCharaters 小部件之间切换,它们真的看不到彼此(如果您选择Home小部件,您的Char 小部件尚未构建,现在如果您选择您的Char 小部件,您的Home 小部件已被它完全取代)

    【讨论】:

    • 不是 100% 确定您的第二点哪里错了。但是在 Scaffold 中打开一个抽屉确实可以让你用 pop 关闭它。即使有一个空的导航器堆栈。也许抽屉的打开会填满 Navigator 堆栈?就像我说的。不是 100% 确定 :)
    • 嗯,我并不是说“你必须按下才能弹出”。真正令人困惑的是,使用setState 替换body 内容的部分不应允许您在pop 上反转state
    • @aziza 和 @KevinWalter 这是来自文档:When a user opens the Drawer, Flutter will add the drawer to the navigation stack under the hood. Therefore, to close the drawer, we can call Navigator.pop(context).
    • 对,如果你使用pushReplacement,那么在导航器堆栈中没有任何东西到pop
    【解决方案2】:

    在 home.dart 构建覆盖。尝试返回一个 Scaffold 而不是 Material 应用程序的另一个实例。您现在在材料应用程序的主体中返回一个材料应用程序。你应该只返回那里的脚手架。

    【讨论】:

    • 非常感谢,我只是从修复它的 main.dart xD 中盲目复制粘贴!
    【解决方案3】:

    对我有用的是这个

    onTap: () { Scaffold.of(context).openEndDrawer(); },

    【讨论】:

      猜你喜欢
      • 2019-11-12
      • 2021-08-22
      • 2020-09-19
      • 1970-01-01
      • 2021-08-02
      • 2021-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多