DataTable(
// 表头颜色
          headingTextStyle: TextStyle(
            color: Colors.black,
            //fontSize: 14,
            // 表头字体粗细
            fontWeight: FontWeight.w900,
          ),
          //horizontalMargin: 30.0,
          // 表头背景颜色
          //headingRowColor: MaterialStateProperty.all(Colors.blueGrey),
          // 每列宽度设置
          columnSpacing: 30.0,
          //bAutoWidth: true,
          sortColumnIndex: _sortColumnIndex, // 设置表格按哪一列排序
          sortAscending: _sortAscending, // 设置表格是升序还是降序 false为升序
          //onSelectAll: (bool value) {}, // 定制全选的动作
          columns: [
            // 数据表格的栏目
            DataColumn(label: Text('年份')),
            DataColumn(
                label: Text('最低分'),
                onSort: (int index, bool ascending) {
                  // 点击标题触发排序方法
                  setState(() {
                    _sortColumnIndex = index;
                    _sortAscending = ascending;

                    _items.sort((a, b) {
                      if (!ascending) {
                        final c = a;
                        a = b;
                        b = c;
                      }
                      return a.lowScore.compareTo(b.lowScore);
                    });
                  });
                }),
            DataColumn(label: Text('最低位次')),
            DataColumn(label: Text('录取数')),
          ],
          rows: _items.map((item) {
            return DataRow(
                cells: [
                  DataCell(Text(item.year)),
                  DataCell(Text('${item.lowScore}')),
                  DataCell(Text(item.lowNum)),
                  DataCell(Text('${item.personNum}')),
                ]);
          }).toList(),
        ),

未设置排序与选项按钮
在这里插入图片描述
数据的定义方式
在这里插入图片描述
效果图
注意:headingRowColor: MaterialStateProperty.all(Colors.blueGrey),
flutter MaterialStateProperty颜色的设置方式,可真是坑苦了我

DataTable 列宽的设置方式:
columnSpacing: 30.0,

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐