【问题标题】:Flutter how to build a Widget list from data passed?Flutter 如何从传递的数据中构建一个 Widget 列表?
【发布时间】:2020-10-06 21:18:06
【问题描述】:

我是 Flutter 的新手,我想弄清楚如何编写一个函数来构建一个基于从 FirstScreenPageView() 传递到 SecondScreenPageView() 的数据的小部件?

更具体地说:

class nameCard extends StatelessWidget{
const nameCard({
  Key key, this.firstName, this.lastName
}): super(key:key);
final String fistName;
final String lastName;
@override
Widget build(BuildContext context){
  return Container(
    child: Column(
       children: <Widget>[
          Text(fistName), 
          Text(lastName),
       ],
    ),
  ),
}
};

class FirstScreenPageView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: RaisedButton(child: Text('Send'), onPressed:(){
          ///Send nameCard Widget to SecondScreenPageView here})
    );
 }
}

class SecondScreenPageView extends StatelessWidget {
   List<Widget> nameCardList = [];
   @override
   Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
       children: nameCardList,
      ),
    );
   }
 }

如何将 FirstScreenPageView 中的 nameCard 附加到 SecondScreenPageView 的 nameCardList?

【问题讨论】:

    标签: flutter flutter-widget


    【解决方案1】:

    您可以通过构造函数传递第二屏页面视图。

    class FirstScreenPageView extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(),
          body: RaisedButton(child: Text('Send'), onPressed:(){
              ///Send nameCard Widget to SecondScreenPageView here})
              Navigator.push(
                  context,
                  MaterialPageRoute(
                  builder: (context) => SecondScreenPageView(nameCardList: cardList),
              ),
            );
        );
     }
    }
    
    -------
    class SecondScreenPageView extends StatelessWidget {
       final List<Widget> nameCardList;
    
       SecondScreenPageView({this.nameCardList});
    
       @override
       Widget build(BuildContext context) {
        return Scaffold(
          body: Column(
           children: nameCardList,
          ),
        );
       }
     }
    
    

    【讨论】:

    • 感谢您的回复。这会将 cardList 传递给 SecondScreenPageView 但是,我要做的是将单个小部件 nameCard 传递给 SSPV 并将该 nameCard 附加到那里的 nameCardList 。希望能澄清我的问题
    【解决方案2】:

    使用名片而不是列表,然后将其添加到第二个屏幕的列表中,并创建一个类名片以在发送到第二个屏幕之前将其用于实现

    import 'package:flutter/material.dart';
    
    class NameCard {
      final String firstName;
      final String lastName;
    
      NameCard({this.firstName, this.lastName});
    }
    
    class FirstScreenPageView extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        NameCard nameCardToSend = NameCard(firstName: 'name', lastName: 'lastName');
    
        return Scaffold(
          appBar: AppBar(),
          body: Column(
            children: <Widget>[
              RaisedButton(
                  child: Text('Send'),
                  onPressed: () {
                    ///Send nameCard Widget to SecondScreenPageView here})
                    Navigator.of(context).push(MaterialPageRoute(builder: (_) {
                      return SecondScreenPageView(
                        nameCard: nameCardToSend,
                      );
                    }));
                  }),
            ],
          ),
        );
      }
    }
    
    class SecondScreenPageView extends StatelessWidget {
      final NameCard nameCard;
    
      SecondScreenPageView({@required this.nameCard});
    
      List<NameCard> nameCardList = [];
      @override
      Widget build(BuildContext context) {
        nameCardList.add(nameCard);
        return Scaffold(
    
          appBar: AppBar(),
    
          body: Column(
            children: <Widget>[
              Text(nameCard.firstName),
              SizedBox(
                height: 11,
              ),
              Text(nameCard.lastName),
              SizedBox(
                height: 11,
              ),
              Text('Or in a Row'),
              Row(
                children: <Widget>[
                  SizedBox(
                    width: 11,
                  ),
                  Text(nameCard.firstName),
                  SizedBox(
                    width: 11,
                  ),
                  Text(nameCard.lastName),
                ],
              ),
            ],
          ),
        );
      }
    }
    

    抱歉忘记在 SecondScreenPageView 中添加 appBar: AppBar(),这样你就可以回到第一个屏幕

    如果是,请接受它作为答案,再见

    【讨论】:

    • 感谢您的回复。这很好,但如何在按下按钮时不改变视图的情况下做到这一点? .push() 方法将从我当前的 FirstPageView 更改为 SecondPageView
    猜你喜欢
    • 2021-06-21
    • 2020-01-21
    • 2020-12-17
    • 2020-04-24
    • 2020-02-27
    • 2021-09-26
    • 2021-02-24
    • 2021-05-28
    • 1970-01-01
    相关资源
    最近更新 更多