繁体   English   中英

在 Flutter 中动态生成小部件

[英]Dynamically generate widgets in Flutter

我正在尝试根据特定条件动态生成一组小部件。 在这种情况下,我试图生成一个 RadioTiles 列表

这就是我试图生成的方式

  List _listings = new List();

  Widget _getListings() {
    // TODO this will accept json objects in order to display the data
    List listings = new List();
    int i = 0;
    for (i = 0; i < 5; i++) {
      listings.add(
        new RadioListTile<SingingCharacter>(
          title: const Text('Lafayette'),
          value: SingingCharacter.lafayette,
          groupValue: _character,
          onChanged: (SingingCharacter value) {
            setState(() {
              _character = value;
            });
          },
        ),
      );
    }
//     return listings;
  }

我试图在这样的有状态小部件中显示它:

 return new SafeArea(
        child: Column(children: <Widget>[
      new Padding(
        padding: const EdgeInsets.all(20.0),
        child: new Text(
          "Verify and Select a Single Listing?",
          style: _textStyle,
        ),
      ),
      ListView(
        shrinkWrap: true,
        padding: const EdgeInsets.all(20.0),
        children: <Widget>[
          _getListings(),
        ],
      ),
    ]));

问题是列表的值为空,因此我无法在屏幕上显示任何小部件。

任何见解都会有用。

谢谢,

编辑 :

如果我确实尝试返回列表,这就是我所看到的: 在此处输入图片说明

我不确定这是否是动态创建小部件的最佳方式。

以下是您的代码的一些更新:

      Widget build(BuildContext context) {

    return Scaffold(body: SafeArea(
        child: Container(child: Column(children: <Widget>[
          Padding(
            padding: const EdgeInsets.all(20.0),
            child: Text("Verify and Select a Single Listing?",),
          ),
          Expanded(child:  ListView(
            padding: const EdgeInsets.all(20.0),
            children: _getListings(), // <<<<< Note this change for the return type
          ),
          )
        ])
        )));
  }

  List _listings = new List();

  List<Widget> _getListings() { // <<<<< Note this change for the return type
    List listings = List<Widget>();
    int i = 0;
    for (i = 0; i < 5; i++) {
      listings.add(
        RadioListTile<String>(
          title: const Text('Lafayette'),
          value: "c",
          groupValue: "x",
          onChanged: (_) {

          },
        ),
      );
    }
     return listings;
  }

上面需要考虑的一些事情:

我已经对代码进行了更改,以便编译并用于此答案。

  • 添加了对显着更改的评论
  • 列表_listings未使用
  • 您还可以在创建新对象时删除new关键字(新版本的 dart 能够处理此问题)

结果:

在此处输入图片说明

对上一个答案的一些评论;

  • 请不要使用不必要的 Container,如果 Container 只有一个孩子而没有其他东西,请将其删除。
  • 不必使用 new 关键字,Dart linters 甚至会告诉不要使用它。 像这儿..

此外,如果您的列表没有更改,您可以使用 List.unmodifiable ,如下例所示。

final List<Widget> widgets = List.unmodifiable(() sync* {
  for (int i = 0; i < 5; i++) {
    yield RadioListTile<String>(
      title: const Text('Lafayette'),
      value: "c",
      groupValue: "x",
      onChanged: (_) {

      },
    );
  }
}());

这可以用来避免不必要的for loop 在 2 行中做同样的事情

int numberOfWidgets = 5;
List<Widget> listings = List<Widget>.filled(numberOfWidgets, buildWidget());

这将制作一个包含确切数量的小部件的列表。 此外,这仅在您想要列表中类似类型的小部件时才有用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM