(十四)Flutter dataTable 数据表格的的使用

简单使用

import 'package:flutter/material.dart';
import '../model/post.dart';
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'),
        elevation: 0.0,
      ),
      body: Container(
        padding: EdgeInsets.all(16.0),
        child: ListView(
          children: <Widget>[
            DataTable(columns: [
              DataColumn(label: Text("Title")),
              DataColumn(label: Text('Anthor')),
            ], rows: [
              DataRow(cells: [
                DataCell(Text("hello ~")),
                DataCell(Text("liuan")),
              ]),
              DataRow(cells: [
                DataCell(Text("hi~")),
                DataCell(Text("futao")),
              ]),
              DataRow(cells: [
                DataCell(Text("a ~")),
                DataCell(Text("zuguo")),
              ]),
            ]),
          ],
        ),
      ),
    );
  }
}

读取列表数据

import 'package:flutter/material.dart';
import '../model/post.dart';

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'),
        elevation: 0.0,
      ),
      body: Container(
        padding: EdgeInsets.all(16.0),
        child: ListView(
          children: <Widget>[
            DataTable(columns: [
              DataColumn(label: Text("Title")),
              DataColumn(label: Text('Anthor')),
              DataColumn(label: Text('Image')),
            ], rows: posts.map(
              (post){
                return DataRow(
                  cells: [
                    DataCell(Text(post.title)),
                    DataCell(Text(post.author)),
                    DataCell(Image.network(post.imgeUrl)),
                  ]
                );
              }
            ).toList()
            
            ),
          ],
        ),
      ),
    );
  }
}

post.dart

class Post {
  const Post({
    this.title,
    this.author,
    this.imgeUrl,
    this.description,
  });
  final String title;
  final String author;
  final String imgeUrl;
  final String description;
}

final List<Post> posts = [
  Post(
    title: '天天练123141',
    author: 'Mohamed Chahin1',
    imgeUrl: 'https://www.baidu.com/img/bd_logo1.png?where=super',
    description: '1',
  ),
  Post(
    title: '天天练',
    author: 'Mohamed Chahin',
    imgeUrl: 'https://www.baidu.com/img/bd_logo1.png?where=super',
    description: '2',
  ),
  Post(
    title: '天天练4124',
    author: 'Mohamed Chahin',
    imgeUrl: 'https://www.baidu.com/img/bd_logo1.png?where=super',
    description: '2',
  ),
  Post(
    title: '天天练123',
    author: 'Mohamed Chahin',
    imgeUrl: 'https://www.baidu.com/img/bd_logo1.png?where=super',
    description: '2',
  ),
  Post(
    title: '天天练123',
    author: 'Mohamed Chahin',
    imgeUrl: 'https://www.baidu.com/img/bd_logo1.png?where=super',
    description: '2',
  ),
  Post(
    title: '天天练12',
    author: 'Mohamed Chahin',
    imgeUrl: 'https://www.baidu.com/img/bd_logo1.png?where=super',
    description: '2',
  )
];

数据表格的排序

import 'package:flutter/material.dart';
import '../model/post.dart';

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

class _DataTableDemoState extends State<DataTableDemo> {
  int _sortColumnIndex = 0;
  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(
                // 排序的列 0的话就是第一列
                sortColumnIndex: _sortColumnIndex,
                //true 升序 false 降序
                sortAscending: _sortAscending,
                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('Anthor')),
                  DataColumn(label: Text('Image')),
                ],
                rows: posts.map((post) {
                  return DataRow(cells: [
                    DataCell(Text(post.title)),
                    DataCell(Text(post.author)),
                    DataCell(Image.network(post.imgeUrl)),
                  ]);
                }).toList()),
          ],
        ),
      ),
    );
  }
}

 选择数据表格行

import 'package:flutter/material.dart';
import '../model/post.dart';

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

class _DataTableDemoState extends State<DataTableDemo> {
  int _sortColumnIndex = 0;
  bool _sortAscending = true;
  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(
                // 排序的列 0的话就是第一列
                sortColumnIndex: _sortColumnIndex,
                //true 升序 false 降序
                sortAscending: _sortAscending,
                
   
                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('Anthor')),
                  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.imgeUrl)),
                      ]);
                }).toList()),
          ],
        ),
      ),
    );
  }
}

post.dart

class Post {
   Post({
    this.title,
    this.author,
    this.imgeUrl,
    this.description,
  });
  final String title;
  final String author;
  final String imgeUrl;
  final String description;
    bool selected =false;

}

final List<Post> posts = [
  Post(
    title: '天天练123141',
    author: 'Mohamed Chahin1',
    imgeUrl: 'https://www.baidu.com/img/bd_logo1.png?where=super',
    description: '1',
  ),
  Post(
    title: '天天练',
    author: 'Mohamed Chahin',
    imgeUrl: 'https://www.baidu.com/img/bd_logo1.png?where=super',
    description: '2',
  ),
  Post(
    title: '天天练4124',
    author: 'Mohamed Chahin',
    imgeUrl: 'https://www.baidu.com/img/bd_logo1.png?where=super',
    description: '2',
  ),
  Post(
    title: '天天练123',
    author: 'Mohamed Chahin',
    imgeUrl: 'https://www.baidu.com/img/bd_logo1.png?where=super',
    description: '2',
  ),
  Post(
    title: '天天练123',
    author: 'Mohamed Chahin',
    imgeUrl: 'https://www.baidu.com/img/bd_logo1.png?where=super',
    description: '2',
  ),
  Post(
    title: '天天练12',
    author: 'Mohamed Chahin',
    imgeUrl: 'https://www.baidu.com/img/bd_logo1.png?where=super',
    description: '2',
  )
];

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安果移不动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值