flutter - 在复选框选择屏幕上使用Navigator.pop时,如何在其他屏幕上显示列表?

标签 flutter dart

我有一个CheckBox项目选择屏幕(在 float 按钮中触发Navigator.push时打开)以检查我喜欢的项目,并且我希望这些项目在使用Navigator.pop时(在轻按 float 按钮时在屏幕上显示) )返回到其原始屏幕。但是我无法做到这一点,我认为list.map将在这里使用,但是我能够正确实现它。任何帮助都非常感谢。

您可以检查整个代码here

我遇到问题的代码:

// CheckBox Item Selection Screen!
class FavoriteList extends StatefulWidget {
@override
_FavoriteListState createState() => _FavoriteListState();
}

class _FavoriteListState extends State<FavoriteList> {

final Set _saved = Set();

@override
Widget build(BuildContext context) {
  return Scaffold(
  appBar: AppBar(title: Text('Add to Favorites!'), centerTitle:  true, backgroundColor: Colors.red),
  // backgroundColor: Colors.indigo,
  body: SafeArea(
          child: ListView.builder(
      itemCount: 53,
      itemBuilder: (context, index) {
        return CheckboxListTile(
          activeColor: Colors.red,
          checkColor: Colors.white,
          value: _saved.contains(index),
             onChanged: (val) {
              setState(() {
              if(val == true){
                _saved.add(index);
              } else{
                _saved.remove(index);
              }
            });
          },
          title: Row(
            children: <Widget>[
              Image.asset('lib/images/${images[index]}'),
              SizedBox(width: 10,),
              Text(nameOfSite[index]),
            ],
          ),
        );
      },
    ),
  ),
  floatingActionButton: FloatingActionButton(foregroundColor: Colors.red,
  child: Icon(Icons.check),
    onPressed: (){
      Navigator.pop(context, _saved); // Navigator.pop
    },
  ),
);
    }
   }

在此原始屏幕上,我要对return列表进行_saved的位置(我使用Set来避免重复)
class SecondPage extends StatefulWidget {
@override
_SecondPageState createState() => _SecondPageState();
}


class _SecondPageState extends State<SecondPage> {
@override
Widget build(BuildContext context) {
  return 
   // if the `_saved` list contains something return the "_saved" list from 
   the CheckBox item selection screen, if not then 
    return  Column(
           mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
        Text(
           'Add Your Favorite Sites Here!❤',
           style: TextStyle(color: Colors.white),
           ),
           Container(
            child: Icon(Icons.favorite, size: 150, color: Colors.blue[100]),
          ),
          SizedBox(height: 250),
            FloatingActionButton(
             onPressed: () {
               Navigator.push( //Navigator.push is here!
                  context,
                   MaterialPageRoute(
            builder: (context) => FavoriteList(),
                ),
              );
           },
            child: Icon(Icons.add),
          foregroundColor: Colors.blue,
         ),
        ],
       );
      }
   }

最佳答案

您可以使用Event_Bus在Screen之间传递数据。

这是lib

https://pub.dev/packages/event_bus

关于flutter - 在复选框选择屏幕上使用Navigator.pop时,如何在其他屏幕上显示列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59350615/

相关文章:

android - VS 代码 : Failed to install the following Android SDK packages as some licences have not been accepted

dart - Flutter 有 "overscroll gesture recognizer"小部件吗?

Dart/flutter : iOS app crashes with Firebase ML Vision

dart - 如何在没有用户交互的情况下发送短信?

dart - 如何在Dart中收听浏览器历史记录事件

unit-testing - 在dart中进行单元测试时,如何一致地设置环境?

android - 在 flutter/Dart 中获取 youtube 视频详细信息

Flutter:IconButton 没有收到 onPressed 函数()

ListView 或 BLoC 的 Flutter 状态恢复

android - 如何在 flutter 中具有这样的双向夹具 View ?