【问题标题】:return multiple data to another screen flutter将多个数据返回到另一个屏幕颤动
【发布时间】:2020-12-21 20:17:48
【问题描述】:

我正在努力将不同的数据返回到我的主屏幕。我有 Mypage() 屏幕,其中列表视图中的卡片显示有标题和文本。我是 Flutter 的新手,我真的不知道如何从另一个名为 WritingPage() 的屏幕返回数据,其中有两个文本字段。这是我在 Mypage() 上的按钮的实际代码:

FlatButton(
          onPressed: (){
            Navigator.of(context).push(MaterialPageRoute(
                builder: (context){
                  return WritingPage();
                })).then((value) {
              dreams.add(value);
              print(dreams);
              setState(() {});
            })
            ;
          },
          child: Icon(Icons.add, size: 30,),
          color: Colors.black,
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(15)),
        ),

那么,WritingPage() 的代码如下:

class WritingPage extends StatefulWidget {
@override
_WritingPageState createState() => _WritingPageState();
}

class _WritingPageState extends State<WritingPage> {
String text;

void submit(){
Navigator.of(context).pop(_textEditingController.text);
}

TextEditingController _textEditingController = TextEditingController();
TextEditingController _titleEditingController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
  backgroundColor: Colors.black,
  appBar: AppBar(
    backgroundColor: kEdgeColor,
    title: Text('Writing part'),
  ),
  body: Container(
    child: Column(
      children: [
        TextField(
          controller: _textEditingController,
          obscureText: false,
          decoration: InputDecoration(
            border: OutlineInputBorder(
              borderRadius: BorderRadius.all(const Radius.circular(12))
            )
          ),
        ),
        TextField(
          controller: _titleEditingController,
          obscureText: false,
          decoration: InputDecoration(
              border: OutlineInputBorder(
                  borderRadius: BorderRadius.all(const Radius.circular(12))
              )
          ),
        ),
        RaisedButton(onPressed : ()=> submit()),
      ],
    ),
  ),
);
}
}

我希望每次按下 WritingPage 中的按钮时,都会在列表视图中创建一张卡片,其中包含两个文本字段的数据。

如果您需要更多详细信息,请询问我,提前谢谢!

【问题讨论】:

    标签: flutter dart navigation screen


    【解决方案1】:

    在我的页面中

    FlatButton(
        onPressed: () async {
          final result = await Navigator.of(context).push(MaterialPageRoute(builder: (context){return WritingPage();})); 
          if (null != result) {
            dreams.add(result);
            print(dreams);
            setState(() {});
          }
        },
        child: Icon(Icons.add, size: 30,),
        color: Colors.black,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(15)),
    ),
    

    在写作页面中

    void submit() {
        Navigator.of(context).pop({
          "text": _textEditingController.text.trim(),
          "title": _titleEditingController.text.trim();
        });
    }
    

    【讨论】:

    • 我遇到了一个错误,但我认为这是因为梦想是一个列表而不是地图。好吧,我不知道用什么来保存标题和内容,然后在列表视图的卡片中使用它们。这是错误:类型'_InternalLinkedHashMap'不是'String'类型的子类型
    • 变量dreams的内容类型是什么?
    • 静态列表梦想 = [];
    • 您在卡片小部件中使用dreams 的位置?
    • 嗯,这是我的卡片构造函数,但我真的不知道如何使用它:class DreamCard extends StatelessWidget { DreamCard({this.Content, this.title});最终字符串标题;最终字符串内容;
    【解决方案2】:

    对此的一种解决方案是在另一个文件中定义一个类,然后您可以从多个页面中引用该类,只需记住使用 setState。此外,如果您要弹出上下文,请确保在返回时重建原始页面。

    【讨论】:

      猜你喜欢
      • 2019-07-18
      • 2023-01-11
      • 2021-11-07
      • 2022-01-12
      • 2019-09-21
      • 1970-01-01
      • 2020-07-12
      • 2019-05-10
      • 1970-01-01
      相关资源
      最近更新 更多