import 'package:flutter/material.dart';
// main method that runs the runApp.
void main() => runApp(SimpleDataTable());
class SimpleDataTable extends StatelessWidget{
@override
Widget build(BuildContext context) {
return MaterialApp(
// MaterialApp with debugShowCheckedModeBanner false and home
debugShowCheckedModeBanner: false,
theme: ThemeData.light(),
home: Scaffold(
// Scaffold with appbar ans body.
appBar: AppBar(
title: Text('Simple Data Table'),
),
body:
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: DataTable(
// Datatable widget that have the property columns and rows.
columns: [
// Set the name of the column
DataColumn(label: Text('ID'),),
DataColumn(label: Text('Name'),),
DataColumn(label: Text('LastName'),),
DataColumn(label: Text('Age'),),
],
rows:[
// Set the values to the columns
DataRow(cells: [
DataCell(Text("1")),
DataCell(Text("Alex")),
DataCell(Text("Anderson")),
DataCell(Text("18")),
]),
DataRow(cells: [
DataCell(Text("2")),
DataCell(Text("John")),
DataCell(Text("Anderson")),
DataCell(Text("24")),
]),
]
),
),
),
);
}
}
- Body Contain SingleChildScrollView 用于水平滚动并避免由于手机屏幕宽度过小而导致的溢出。
- SingleChildScrollView 采用 Single Child DataTable,后者采用列和行。
- DataColumn 中带有标签的列。
- DataRow 中表示的行采用 Cells,然后是 DataCell,后者采用表示行的每个列值的任何 Widget。