Dart 语言 Table 表格布局示例

Dart阿木 发布于 2025-06-19 12 次阅读


Dart 语言 Table 表格布局示例详解

在 Dart 语言中,表格布局是一种常见的界面设计方式,它能够帮助我们以网格的形式展示数据,使得信息更加清晰、有序。Dart 语言提供了丰富的组件和库来支持表格布局,其中最常用的就是 `DataTable` 组件。本文将围绕 Dart 语言中的 Table 表格布局进行详细介绍,包括基本用法、高级特性以及一些实用的示例。

基础表格布局

1. 引入库

我们需要在 Dart 项目中引入 `flutter/material.dart` 库,这个库包含了 `DataTable` 组件。

dart

import 'package:flutter/material.dart';


2. 创建基本表格

下面是一个简单的表格布局示例:

dart

void main() {


runApp(MyApp());


}

class MyApp extends StatelessWidget {


@override


Widget build(BuildContext context) {


return MaterialApp(


home: Scaffold(


appBar: AppBar(


title: Text('Dart Table Layout Example'),


),


body: MyTable(),


),


);


}


}

class MyTable extends StatelessWidget {


@override


Widget build(BuildContext context) {


return DataTable(


columns: [


DataColumn(label: Text('Name')),


DataColumn(label: Text('Age')),


DataColumn(label: Text('City')),


],


rows: [


DataRow(cells: [


DataCell(Text('Alice')),


DataCell(Text('25')),


DataCell(Text('New York')),


]),


DataRow(cells: [


DataCell(Text('Bob')),


DataCell(Text('30')),


DataCell(Text('Los Angeles')),


]),


DataRow(cells: [


DataCell(Text('Charlie')),


DataCell(Text('35')),


DataCell(Text('Chicago')),


]),


],


);


}


}


在这个例子中,我们创建了一个包含三列(Name、Age、City)的表格,并添加了三行数据。

高级表格布局

1. 可滚动表格

当表格数据量较大时,我们可以使用 `SingleChildScrollView` 来包裹 `DataTable`,使其成为可滚动的表格。

dart

class MyTable extends StatelessWidget {


@override


Widget build(BuildContext context) {


return SingleChildScrollView(


child: DataTable(


columns: [


DataColumn(label: Text('Name')),


DataColumn(label: Text('Age')),


DataColumn(label: Text('City')),


],


rows: List.generate(100, (index) => DataRow(cells: [


DataCell(Text('Name $index')),


DataCell(Text('${index + 20}')),


DataCell(Text('City $index')),


])),


),


);


}


}


2. 表格样式

我们可以通过 `border` 属性来设置表格的边框样式,通过 `showBottomBorder` 属性来显示底部边框。

dart

class MyTable extends StatelessWidget {


@override


Widget build(BuildContext context) {


return DataTable(


columns: [


DataColumn(label: Text('Name')),


DataColumn(label: Text('Age')),


DataColumn(label: Text('City')),


],


rows: List.generate(100, (index) => DataRow(cells: [


DataCell(Text('Name $index')),


DataCell(Text('${index + 20}')),


DataCell(Text('City $index')),


])),


showBottomBorder: true,


border: TableBorder.all(),


);


}


}


3. 表格排序

我们可以通过 `onSort` 属性来为表格列添加排序功能。

dart

class MyTable extends StatefulWidget {


@override


_MyTableState createState() => _MyTableState();


}

class _MyTableState extends State<MyTable> {


int sortColumnIndex;


bool isAscending = true;

void _sortColumnIndex(int columnIndex, bool ascending) {


setState(() {


sortColumnIndex = columnIndex;


isAscending = ascending;


});


}

@override


Widget build(BuildContext context) {


return DataTable(


columns: [


DataColumn(


label: Text('Name'),


onSort: (columnIndex, ascending) {


_sortColumnIndex(columnIndex, ascending);


},


),


DataColumn(


label: Text('Age'),


onSort: (columnIndex, ascending) {


_sortColumnIndex(columnIndex, ascending);


},


),


DataColumn(


label: Text('City'),


onSort: (columnIndex, ascending) {


_sortColumnIndex(columnIndex, ascending);


},


),


],


rows: List.generate(100, (index) => DataRow(cells: [


DataCell(Text('Name $index')),


DataCell(Text('${index + 20}')),


DataCell(Text('City $index')),


])),


sortColumnIndex: sortColumnIndex,


sortAscending: isAscending,


);


}


}


在这个例子中,我们为表格的每一列都添加了排序功能。

实用示例

1. 表格与列表结合

在实际应用中,我们经常需要将表格与列表结合使用。以下是一个将表格与 `ListView` 结合的示例:

dart

class MyTable extends StatelessWidget {


@override


Widget build(BuildContext context) {


return ListView(


children: List.generate(100, (index) => ListTile(


title: DataTable(


columns: [


DataColumn(label: Text('Name')),


DataColumn(label: Text('Age')),


DataColumn(label: Text('City')),


],


rows: [


DataRow(cells: [


DataCell(Text('Name $index')),


DataCell(Text('${index + 20}')),


DataCell(Text('City $index')),


]),


],


),


)),


);


}


}


2. 表格与搜索框结合

在表格数据量较大时,我们可以添加一个搜索框来过滤数据。以下是一个将表格与搜索框结合的示例:

dart

class MyTable extends StatefulWidget {


@override


_MyTableState createState() => _MyTableState();


}

class _MyTableState extends State<MyTable> {


List<String> _data = [


'Alice',


'Bob',


'Charlie',


'David',


'Eve',


];


String _searchText = '';

void _onSearchTextChanged(String value) {


setState(() {


_searchText = value;


});


}

@override


Widget build(BuildContext context) {


return Column(


children: [


TextField(


decoration: InputDecoration(


labelText: 'Search',


suffixIcon: Icon(Icons.search),


),


onChanged: _onSearchTextChanged,


),


Expanded(


child: DataTable(


columns: [


DataColumn(label: Text('Name')),


],


rows: _data


.where((name) => name.toLowerCase().contains(_searchText.toLowerCase()))


.map((name) => DataRow(cells: [


DataCell(Text(name)),


]))


.toList(),


),


),


],


);


}


}


在这个例子中,我们使用 `TextField` 组件创建了一个搜索框,并通过 `_onSearchTextChanged` 方法来过滤表格数据。

总结

本文详细介绍了 Dart 语言中的 Table 表格布局,包括基本用法、高级特性以及一些实用的示例。通过学习本文,读者可以掌握 Dart 语言中表格布局的技巧,并将其应用到实际项目中。希望本文对您有所帮助!