颤抖如何从传递的数据构建窗口小部件列表?

时间:2020-06-17 06:09:57

标签: flutter flutter-widget

我是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,
      ),
    );
   }
 }

如何将NameScreen从FirstScreenPageView追加到SecondScreenPageView的nameCardList?

2 个答案:

答案 0 :(得分:0)

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

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,
      ),
    );
   }
 }

答案 1 :(得分:0)

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

dynamic

对不起,忘记在SecondScreenPageView中添加appBar:AppBar(),以便您可以返回到第一个屏幕

如果可以,请接受它作为答案