【问题标题】:How to pass parameters in Flutter ListViewFlutter ListView中如何传递参数
【发布时间】:2020-03-29 18:01:08
【问题描述】:

我想在我的列表视图中传递标题和描述列表,我可以传递列表视图生成器,但我不知道如何传递列表视图。

我想在 ListView 中传递条目数组。无需输入每个条目,我想在卡片上显示数组中的所有内容。

 Widget build(BuildContext context) {
 final List<String> entries= <String> ['Entry one','Entry Two','Entry Three','Entry one','Entry Two','Entry Three','Entry one','Entry Two','Entry Three','Entry one','Entry Two','Entry Three'];

    return Scaffold(
      appBar: AppBar(
        title: Text('title'),
      ),

      body: ListView(

  children: const <Widget>[
  Card(
      child: ListTile(
        leading: FlutterLogo(size: 40.0),
        title: Text('all entries one by one'),
        subtitle: Text(
          'subtitle'
        ),
        trailing: Icon(Icons.favorite),
        isThreeLine: true,
      ),
    ),
  ],
)
    );

  }

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    您可以为此使用 ListView.builder()。这是一个例子。

    class TestPage extends StatelessWidget {
      final List<String> entries = <String>[
        'Entry one',
        'Entry Two',
        'Entry Three',
        'Entry one',
        'Entry Two',
        'Entry Three',
        'Entry one',
        'Entry Two',
        'Entry Three',
        'Entry one',
        'Entry Two',
        'Entry Three'
      ];
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('title'),
          ),
          body: Container(
            height: 500,
            child: ListView.builder(
              itemCount: entries.length,
              itemBuilder: (BuildContext context, int index) {
                return Card(
                  child: ListTile(
                    leading: FlutterLogo(size: 40.0),
                    title: Text(entries[index]),
                    subtitle: Text('subtitle'),
                    trailing: Icon(Icons.favorite),
                    isThreeLine: true,
                  ),
                );
              },
            ),
          ),
        );
      }
    }
    

    【讨论】:

      【解决方案2】:

      如果您已经拥有标题和描述数组,则使用 ListView.builder() 是最好的选择。首先为您的标题和描述创建一个类:

      class Info{
        String title;
        String description;
        Info(this.title, this.description);
      }
      

      现在创建这个信息类List&lt;Info&gt; _myInfo 的数组,并用您的数据填充它。现在您可以像这样创建列表视图:

      ListView.builder(
        itemCount: _myInfo.length,
        builder: (context, index) {
          return _createCard(index);
        }
      );
      

      现在您可以像这样在此处分离您的卡片创建:

      Widget _createCard(int index){
        return Card(
          child: ListTile(
            leading: FlutterLogo(size: 40.0),
            //here your title will be create from array for each new item
            title: Text(_myInfo[index].title),
            subtitle: Text(
              'subtitle'
            ),
            trailing: Icon(Icons.favorite),
            isThreeLine: true,
          ),
        ),
      }
      

      代码可能包含语法错误,如果您遇到任何问题,请随时询问

      【讨论】:

        猜你喜欢
        • 2021-09-24
        • 2020-09-19
        • 2021-03-05
        • 2018-08-19
        • 2021-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-15
        相关资源
        最近更新 更多