首页 > 解决方案 > Flutter 在 ListTile 中显示 sql 数据而不是 DataCell

问题描述

我正在使用从我的 sql 数据库中获取的数据列表显示DataCell,但我不太喜欢它的外观并希望将其切换为使用显示它ListTile,这是我用来显示它的代码DataCell

return SingleChildScrollView(
      scrollDirection: Axis.vertical,
      child: SingleChildScrollView(
        scrollDirection: Axis.horizontal,
        child: DataTable(
          columns: [
            DataColumn(
              label: Text(''),
            )
          ],
          rows: _chatUsers
              .map(
                (user) => DataRow(cells: [
                  DataCell(
                    Text(user.firstNameUser),
                    // Add tap in the row and populate the
                    // textfields with the corresponding values to update
                    onTap: () {
                      // Set the Selected employee to Update
                      _selectedUser = user;
                      setState(() {

                      });
                    },
                  ),
                ]),
              )
              .toList(),
        ),
      ),
    );

标签: flutterdart

解决方案


为此,您需要使用ListView小部件。API 参考部分中有很多解释,我认为您将能够在阅读后重新设计您的应用程序。

所以你会有一个ListView属性children设置为 smth like

_chatUsers
              .map(
                (user) => 
                  ListTile(
                    title: Text(user.firstNameUser),
                    // Add tap in the row and populate the
                    // textfields with the corresponding values to update
                    onTap: () {
                      // Set the Selected employee to Update
                      _selectedUser = user;
                      setState(() {

                      });
                    },
                  ),
              )
              .toList()

推荐阅读