美文网首页
Flutter-数据表格

Flutter-数据表格

作者: 梦幽辰 | 来源:发表于2020-01-08 15:52 被阅读0次
class DataTableDemo extends StatefulWidget {
  @override
  _DataTableDemoState createState() => _DataTableDemoState();
}

class _DataTableDemoState extends State<DataTableDemo> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('DataTableDemo'),
      ),
      body: Container(
        padding: EdgeInsets.all(16),
        child: ListView(
          children: <Widget>[
            DataTable(columns: [
              DataColumn(label: Text('Title')),
              DataColumn(label: Text('Author')),
            ], rows: [
              DataRow(
                cells: [
                  DataCell(Text('a')), 
                  DataCell(Text('b'))
                ]
              )
            ])
          ],
        ),
      ),
    );
  }
}
image.png

列表生成

child: ListView(
  children: <Widget>[
    DataTable(
      columns: [
        DataColumn(
          label: Text('Title'),
        ),
        DataColumn(
          label: Text('Author')
        ),
        DataColumn(
          label: Text('Author')
        ),
      ],
      rows: posts.map(
        (post){
          return DataRow(
            cells: [
              DataCell(Text(post.title)),
              DataCell(Text(post.author)),
              DataCell(Image.network(post.imageUrl)),
            ]
          );
        }
      ).toList(),
    )
  ],
),
image.png

排序

DataColumn(
  label: Text('Title'),
  onSort: (int index, bool ascending) {
    setState(() {
      _sortColumnIndex = index;
      _sortAscending = ascending;

      posts.sort((a, b) {
        if (!ascending) {
          final c = a;
          a = b;
          b = c;
        }

        return a.title.length.compareTo(b.title.length);
      });
    });
 }),
image.png

总的代码

class DataTableDemo extends StatefulWidget {
  @override
  _DataTableDemoState createState() => _DataTableDemoState();
}

class _DataTableDemoState extends State<DataTableDemo> {
  int _sortColumnIndex;
  bool _sortAscending = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('DataTableDemo'),
      ),
      body: Container(
        padding: EdgeInsets.all(16),
        child: ListView(
          children: <Widget>[
            DataTable(
              sortColumnIndex: _sortColumnIndex, // 选择相应的列进行排序
              sortAscending: _sortAscending, // true为升序,false为降序
              onSelectAll: (bool value){
                
              },
              columns: [
                DataColumn(
                    label: Text('Title'),
                    onSort: (int index, bool ascending) {
                      setState(() {
                        _sortColumnIndex = index;
                        _sortAscending = ascending;

                        posts.sort((a, b) {
                          if (!ascending) {
                            final c = a;
                            a = b;
                            b = c;
                          }

                          // 1.compareTo(2); -1
                          // 1.compareTo(0); 1
                          // 1.compareTo(1); 0
                          return a.title.length.compareTo(b.title.length);
                        });
                      });
                    }),
                DataColumn(label: Text('Author')),
                DataColumn(label: Text('Author')),
              ],
              rows: posts.map((post) {
                return DataRow(
                    selected: post.selected,
                    onSelectChanged: (bool value) {
                      // value 表示当前行选中的状态,被选了则value=true,否则就是false
                      setState(() {
                        if (post.selected != value) {
                          post.selected = value;
                        }
                      });
                    },
                    cells: [
                      DataCell(Text(post.title)),
                      DataCell(Text(post.author)),
                      DataCell(Image.network(post.imageUrl)),
                    ]);
              }).toList(),
            )
          ],
        ),
      ),
    );
  }
}
image.png

分页表格数据

class PostDataSource extends DataTableSource{
  final List<Post> _posts = posts;
  int _selectedCount = 0;

  @override
  int get rowCount => _posts.length; // 行的数量

  @override
  bool get isRowCountApproximate => false; // 如果不确定行的数量,可以让其为true

  @override
  int get selectedRowCount => _selectedCount;

  @override
  DataRow getRow(int index) { // 每一行具体的内容
    final Post post = _posts[index];

    return DataRow.byIndex(
      index: index,
      cells: <DataCell>[
        DataCell(Text(post.title)),
        DataCell(Text(post.author)),
        DataCell(Image.network(post.imageUrl)),
      ]
    );
  }

  void _sort(getField(post), bool ascending){ // 第一个参数是一个函数,这个函数中的参数是post
    _posts.sort((a, b){
      if (!ascending) {
        final c = a;
        a = b;
        b = c;
      }

      final aValue = getField(a);
      final bValue = getField(b);

      return Comparable.compare(aValue, bValue); // 等同于aValue.compareTo(bValue)
    });

    notifyListeners(); // 在状态发生变化时(increment)通知所有用到了该model的子项更新状态
  }
}

class PaginatedDataTableDemo extends StatefulWidget {
  @override
  _PaginatedDataTableDemoState createState() => _PaginatedDataTableDemoState();
}

class _PaginatedDataTableDemoState extends State<PaginatedDataTableDemo> {
  int _sortColumnIndex;
  bool _sortAscending = false;

  final PostDataSource _postDataSource = PostDataSource();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('PaginatedDataTableDemo'),
      ),
      body: Container(
        padding: EdgeInsets.all(16),
        child: ListView(
          children: <Widget>[
            PaginatedDataTable(
              header: Text('Posts'),
              source: _postDataSource,
              rowsPerPage: 5, // 每一页显示的行数
              sortColumnIndex: _sortColumnIndex, // 选择相应的列进行排序
              sortAscending: _sortAscending, // true为升序,false为降序
              onSelectAll: (bool value){

              },
              columns: [
                DataColumn(
                    label: Text('Title'),
                    onSort: (int columnIndex, bool ascending) {
                      _postDataSource._sort((post) => post.title.length, ascending);

                      setState(() {
                        _sortColumnIndex = columnIndex;
                        _sortAscending = ascending;
                      });
                    }),
                DataColumn(label: Text('Author')),
                DataColumn(label: Text('Source')),
              ],
            )
          ],
        ),
      ),
    );
  }
}
image.png

相关文章

  • Flutter-数据表格

    列表生成 排序 总的代码 分页表格数据

  • Flutter-现有iOS工程引入Flutter

    Flutter-现有iOS工程引入Flutter Flutter-现有iOS工程引入Flutter

  • mysql基本操作

    创建数据库、使用数据库、查看表格: 创建表格: 插入数据到表格: 查看数据: 创建索引: 使用索引的好处: 右侧通...

  • HTML自适应表格制作

    Dreamweaver的“表格式数据导入(T)”功能依据:Dw的“表格式数据导入(T)”功能可以将存有表格数据的U...

  • 医学SCI论文如何正确的使用表格--辑思编译

    罗列数据,我们需要表格;对比数据,我们需要表格;分析数据,我们需要表格。在一篇医学论文中,表格是不可或缺的内容。那...

  • 医学SCI论文如何正确的使用表格--Editideas

    罗列数据,我们需要表格;对比数据,我们需要表格;分析数据,我们需要表格。在一篇医学论文中,表格是不可或缺的内容。那...

  • HTML表格标签

    表格标签: 表格标签的作用:用来给一堆数据添加表格语义表格标签是一种数据的展现形式,当数据量特别大的时候,表格的展...

  • bootstrap-table方法

    初始化表格 销毁表格 刷新表格 获取选中行数据 获取所有行数据 添加请求参数 Bootstrap表格方法-小书童文...

  • HTML5学习(六):基础标签(二)

    基础标签 表格标签 作用:用来给数据添加表格语义的。其实表格是一种数据展现形式,数据量大的时候,表格这种展现形式被...

  • element ui ----table表格(带多选框)+分页实

    大概的需求是,把A表格里面选中的数据,赋值到B表格中,当B表格移除某条数据时,A表格中对应的那条数据也应该取消选中...

网友评论

      本文标题:Flutter-数据表格

      本文链接:https://www.haomeiwen.com/subject/wltsactx.html