zoukankan      html  css  js  c++  java
  • flutter DataTable数据表格

    数据表显示原始数据集。它们通常出现在桌面企业产品中。DataTable Widget实现这个组件

    文档:https://api.flutter.dev/flutter/material/DataTable-class.html

    import 'package:flutter/material.dart';
    import './model/post.dart';
    
    class DataTableDemo extends StatefulWidget {
      @override
      _DataTableDemoState createState() => _DataTableDemoState();
    }
    
    class _DataTableDemoState extends State<DataTableDemo> {
      int _sortColumnIndex;
      bool _sortAscending = true;
      
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('DataTableDemo'),
            elevation: 0.0,
          ),
          body: Container(
            padding: EdgeInsets.all(16.0),
            child: ListView(
              children: <Widget>[
                DataTable(
                  sortColumnIndex: _sortColumnIndex,
                  sortAscending: _sortAscending,
                  // 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;
                            }
    
                            return a.title.length.compareTo(b.title.length);
                          });
                        });
                      },
                    ),
                    DataColumn(
                      label: Text('Author'),
                    ),
                    DataColumn(
                      label: Text('Image'),
                    ),
                   
                  ],
                  rows: posts.map((post) {
                    return DataRow(
                      selected: post.selected,
                      onSelectChanged: (bool value) {
                        setState(() {
                          if (post.selected != value) {
                            post.selected = value;
                          }
                        });
                      },
                      cells: [
                        DataCell(Text(post.title)),
                        DataCell(Text(post.author)),
                        DataCell(Image.network(post.imageUrl)),
                       
                      ]
                    );
                  }).toList(),
                ),
              ],
            ),
          )
        );
      }
    }

    效果:


    分页demo

    import 'package:flutter/material.dart';
    import './model/post.dart';
    
    class PostDataSource extends DataTableSource {
      final List<Post> _posts = posts;
      int _selectedCount = 0;
    
      @override
      int get rowCount => _posts.length;
    
      @override
      bool get isRowCountApproximate => false;
    
      @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) {
        _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);
        });
    
        notifyListeners();
      }
    }
    
    class PaginatedDataTableDemo extends StatefulWidget {
      @override
      _PaginatedDataTableDemoState createState() => _PaginatedDataTableDemoState();
    }
    
    class _PaginatedDataTableDemoState extends State<PaginatedDataTableDemo> {
      int _sortColumnIndex;
      bool _sortAscending = true;
    
      final PostDataSource _postsDataSource = PostDataSource();
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
              title: Text('PaginatedDataTableDemo'),
              elevation: 0.0,
            ),
            body: Container(
              padding: EdgeInsets.all(16.0),
              child: ListView(
                children: <Widget>[
                  PaginatedDataTable(
                    header: Text('Posts'),
                    rowsPerPage: 5,
                    source: _postsDataSource,
                    sortColumnIndex: _sortColumnIndex,
                    sortAscending: _sortAscending,
                    // onSelectAll: (bool value) {},
                    columns: [
                      DataColumn(
                        label: Text('Title'),
                        onSort: (int columnIndex, bool ascending) {
                          _postsDataSource._sort((post) => post.title.length, ascending);
    
                          setState(() {
                            _sortColumnIndex = columnIndex;
                            _sortAscending = ascending;
                          });
                        },
                      ),
                      DataColumn(
                        label: Text('Author'),
                      ),
                      DataColumn(
                        label: Text('Image'),
                      ),
                    ],
                  ),
                ],
              ),
            ));
      }
    }

    效果:

  • 相关阅读:
    SQL随机生成6位数字
    安装时提示 INSTALL_PARSE_FAILED_MANIFEST_MALFORMED 解决办法
    Windows 7 完美安装 Visual C++ 6.0
    解决js中window.location.href不工作的问题
    DataList中动态显示DIV
    Gridview、DataList、Repeater获取行索引号
    Java多jdk安装
    【CentOS】samba服务器安装与配置
    【CentOS】IBM X3650M4 IMM远程管理【转载】
    【Java】Eclipse导出jar包与javadoc
  • 原文地址:https://www.cnblogs.com/loaderman/p/11344875.html
Copyright © 2011-2022 走看看