从MaterialPageRoute

时间:2019-02-21 13:26:14

标签: dart flutter

我是Flutter的新手,我正尝试从小部件列表中删除一个元素。如果我从小部件数组中删除它,则将删除元素,但不会更新UI。 我正在尝试将这篇文章用作指南:Update UI after removing items from List,但到目前为止还没有运气。

我一直在遵循Google的代码实验室来创建无限列表。 这是有问题的代码段:

  final List<WordPair> _suggestions = <WordPair>[];
  final Set<WordPair> _saved = new Set<WordPair>();
  List<Widget> _divided = <Widget>[];
  ListView myList;
  final TextStyle _biggerFont = const TextStyle(fontSize: 18.0);

  void _pushSaved() {
        Navigator.of(context).push(
          new MaterialPageRoute<void>(
            builder: (BuildContext context) {

              final Iterable<Widget> tiles = _saved.map(
                (WordPair pair) {
                  return MyItem(pair.asPascalCase, onDelete: () => removeItem());
                },
              );

              _divided = ListTile.divideTiles(
                context: context,
                tiles: tiles,
              ).toList();
              myList = new ListView(children: _divided);

              return new Scaffold(
                appBar: new AppBar(
                  title: const Text('Saved Suggestions'),
                ),
                body: myList,
              );
            },
          ),
        );
      }

      void removeItem() {
        setState(() {
          _divided.removeLast();
          myList = new ListView(children: _divided);
          Navigator.of(context).pop();
        });
      }
    }

    class MyItem extends StatelessWidget {
      final String title;
      final VoidCallback onDelete;

      MyItem(this.title, {this.onDelete});

      @override
      Widget build(BuildContext context) {
        return ListTile(
          title: Text(
            this.title,
          ),
          onTap: () {
            showDialog(
              context: context,
              builder: (BuildContext context) {
                // return object of type Dialog
                return AlertDialog(
                  title: Text(this.title),
                  content: new Text('Would you like to remove the name?'),
                  actions: <Widget>[
                    // usually buttons at the bottom of the dialog
                    new FlatButton(
                      child: new Text("Close"),
                      onPressed: () {
                        Navigator.of(context).pop();
                      },
                    ),
                    new FlatButton(
                      child: new Text('Yes'),
                      onPressed: this.onDelete,
                    )
                  ],
                );
              },
            );
          },
        );
      }
    }

单击列表项后,将打开一个对话框,可以单击“是”,然后将删除该元素。但是,我需要将此操作通知家长。但是我不确定父级是MyItem还是在MaterialPageRoute的末尾返回新的Scaffold

0 个答案:

没有答案