一文帶你了解Flutter數(shù)據(jù)表格的使用
前言
目前,越來越多的管理層(所謂的領導)都希望在手機端查看各種各樣的數(shù)據(jù)報表,以達到隨時隨地關注經營業(yè)績(監(jiān)督干活)的目的。這就要求移動端能夠提供數(shù)據(jù)表來滿足這類訴求,本篇我們就來介紹 Flutter 的數(shù)據(jù)表格的使用。通過本篇你會了解到:
- Flutter 自帶的
DataTable
的使用; - 第三方強大的數(shù)據(jù)表
SfDataGrid
的使用。
組成DataTable的基本元素
DataTable
是 Flutter 自帶的數(shù)據(jù)表組件,支持定義表頭和行數(shù)據(jù)來實現(xiàn)數(shù)據(jù)表格,同時支持列排序、選中行等操作,對于基礎的數(shù)據(jù)表格展示基本能夠滿足,DataTable
類的定義如下。
DataTable({ Key? key, required this.columns, this.sortColumnIndex, this.sortAscending = true, this.onSelectAll, this.decoration, this.dataRowColor, this.dataRowHeight, this.dataTextStyle, this.headingRowColor, this.headingRowHeight, this.headingTextStyle, this.horizontalMargin, this.columnSpacing, this.showCheckboxColumn = true, this.showBottomBorder = false, this.dividerThickness, required this.rows, this.checkboxHorizontalMargin, this.border, })
常用的屬性說明如下:
columns
:是一個DataColumn
數(shù)組,用于定義表頭。rows
:是一個DataRow
數(shù)組,用于定義每一行要顯示的數(shù)據(jù)。sortColumnIndex
:要排序的列,可以通過該值設定當前使用那一列進行排序。指定的列會有一個向上或向下的箭頭指示當前的排序方式。sortAscending
:排序的方式,默認為升序排序。onSelectAll
:全選回調事件,如果全選攜帶的參數(shù)為true
,否則為false
。
DataColumn
是數(shù)據(jù)列組件,包括了如下4個屬性:
label
:可以是任意組件,通常我們使用的是Text
組件,也可以使用其他組件。tooltip
:列的描述文字,用于列寬受限時展示完整的列內容。numeric
:是否是數(shù)字列,如果是數(shù)字列會采用右對齊方式呈現(xiàn)。onSort
:排序事件回調,攜帶兩個參數(shù)指示當前實用第幾列排序,排序方式是升序還是降序。我們可以通過這個方法來響應排序操作對要展示的行數(shù)據(jù)進行排序。
DataRow
是數(shù)據(jù)行組件,包括如下5個屬性:
cells
:DataCell
數(shù)組,用于定義每一列對應的元素。selected
:行的選中狀態(tài),默認為不選中。onSelectChanged
:行選中狀態(tài)改變時的回調函數(shù)。onLongPress
:長按行的回調,我們可以用來做長按刪除、上移、下移類的操作。color
:MaterialStateProperty<Color?>
類,可以用來定義不同狀態(tài)下的行的顏色。
DataCell
是數(shù)據(jù)單元格組件,用于定義要顯示的單元格內容以及響應單元格的交互(包括點擊、長按、雙擊等)。 由此我們就得到了一個完整的 DataTable
所需要的元素。
DataTable 示例
首先說一下,F(xiàn)lutter 提供的 DataTable 如果超出屏幕范圍默認是不支持滾動的,因此如果要支持滾動,就需要用 SingleChildScrollView
包裹,然后定義滾動的方向來實現(xiàn)橫向或縱向滾動。如果要同時支持橫向和縱向滾動,就需要使用兩個SingleChildScrollView
來包裹。下面的示例代碼就是實用了兩個SingleChildScrollView
實現(xiàn)了列表的橫向和縱向滾動。
class _DataTableDemoState extends State<DataTableDemo> { var _sortAscending = true; int? _sortColumn; final dataModels = <DataModel>[ DataModel(nation: '中國', population: 14.1, continent: '亞洲'), DataModel(nation: '美國', population: 2.42, continent: '北美洲'), DataModel(nation: '俄羅斯', population: 1.43, continent: '歐洲'), DataModel(nation: '巴西', population: 2.14, continent: '南美洲'), DataModel(nation: '印度', population: 13.9, continent: '亞洲'), DataModel(nation: '德國', population: 0.83, continent: '歐洲'), DataModel(nation: '埃及', population: 1.04, continent: '非洲'), DataModel(nation: '澳大利亞', population: 0.26, continent: '大洋洲'), DataModel(nation: '印度', population: 13.9, continent: '亞洲'), DataModel(nation: '德國', population: 0.83, continent: '歐洲'), DataModel(nation: '埃及', population: 1.04, continent: '非洲'), DataModel(nation: '澳大利亞', population: 0.26, continent: '大洋洲'), ]; Function(int, bool)? _sortCallback; @override void initState() { super.initState(); _sortCallback = (int column, bool isAscending) { setState(() { _sortColumn = column; _sortAscending = isAscending; }); }; } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.white, appBar: AppBar( title: const Text('DataTable'), backgroundColor: Colors.red[400]!, ), body: SingleChildScrollView( scrollDirection: Axis.vertical, child: SingleChildScrollView( scrollDirection: Axis.horizontal, child: DataTable( horizontalMargin: 10.0, showBottomBorder: true, sortAscending: _sortAscending, sortColumnIndex: _sortColumn, showCheckboxColumn: true, headingTextStyle: const TextStyle( fontWeight: FontWeight.bold, color: Colors.black, ), columns: [ const DataColumn(label: Text('國家')), DataColumn( label: const Text('人口(億)'), numeric: true, onSort: _sortCallback, ), DataColumn( label: const Text('大洲'), onSort: _sortCallback, ), const DataColumn(label: Text('說明')), ], rows: sortDataModels(), ), ), ), ); } List<DataRow> sortDataModels() { dataModels.sort((dataModel1, dataModel2) { bool isAscending = _sortAscending; var result = 0; if (_sortColumn == 0) { result = dataModel1.nation.compareTo(dataModel2.nation); } if (_sortColumn == 1) { result = dataModel1.population.compareTo(dataModel2.population); } if (_sortColumn == 2) { result = dataModel1.continent.compareTo(dataModel2.continent); } if (isAscending) { return result; } return -result; }); return dataModels .map((dataModel) => DataRow( onSelectChanged: (selected) {}, cells: [ DataCell( Text(dataModel.nation), ), DataCell( Text('${dataModel.population}'), ), DataCell( Text(dataModel.continent), ), const DataCell( Text('這是詳細介紹'), ), ], )) .toList(); } }
上述代碼的實現(xiàn)效果如下圖所示。
可以看到,使用 DataTable
能夠滿足我們基本的數(shù)據(jù)表格的需求,但是我們如果希望表頭固定或者列固定,實現(xiàn)起來就有點麻煩了。復雜表格的場景,推薦大家一個好用的第三方庫:SfDataGrid。
SfDataGrid
SfDataGrid
同時支持移動端、Web 端和桌面端,基本上和前端 Web 表格功能有的它都有,比如固定某些列或某些行、自動滾動、編輯單元格、設置行高和列寬、排序、單擊選擇單行或多行、自定義樣式、合并單元格、調整列寬、上拉加載或分頁瀏覽、導出到 Excel 文件等等??梢哉f,用 SfDataGrid
可以滿足絕大多數(shù)數(shù)據(jù)表格的場景,更重要的是,官方提供了詳細的文檔(點此查看使用文檔)和示例代碼,可以讓我們輕松上手。下面是實用 SfDataGrid
實現(xiàn)的一個示例效果(移動端列寬調整需要使用長按功能)。
總結
本篇介紹了 Flutter 中的數(shù)據(jù)表格組件 DataTable
的使用,并介紹了一個很強大的數(shù)據(jù)表格庫 SfDataGrid
。如果是簡單的數(shù)據(jù)表格可以使用 Flutter 自帶的 DataTable
,如果涉及到復雜的樣式和交互效果,建議實用 SfDataGrid
來搞定。
到此這篇關于一文帶你了解Flutter數(shù)據(jù)表格的使用的文章就介紹到這了,更多相關Flutter數(shù)據(jù)表格內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Kotlin示例講解標準函數(shù)with與run和apply的使用
Kotlin的標準函數(shù)是指 Standard.kt 文件中定義的函數(shù),任何Kotlin代碼都可以自由地調用所有的標準函數(shù)。文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2022-08-08Android UI設計系列之自定義SwitchButton開關實現(xiàn)類似IOS中UISwitch的動畫效果(2
這篇文章主要介紹了Android UI設計系列之自定義SwitchButton開關實現(xiàn)類似IOS中UISwitch的動畫效果,具有一定的實用性和參考價值,感興趣的小伙伴們可以參考一下2016-06-06Android開發(fā)之串口編程原理和實現(xiàn)方式
提到串口編程,就不得不提到JNI,不得不提到JavaAPI中的文件描述符類:FileDescriptor;下面我分別對JNI、FileDescriptor以及串口的一些知識點和實現(xiàn)的源碼進行分析說明,感興趣的朋友可以了解下2013-01-01Android 中 requestWindowFeature()的應用
本文主要介紹 Android requestWindowFeature()方法,這里對 requestWindowFeature()方法進行詳解,對應用程序窗體顯示狀態(tài)的操作有進一步了解,希望能幫助有需要的小伙伴2016-07-07