flutter - Flutter DataTable - how set column width?
问题描述
I use DataTable in my Flutter app. This is simple example of my code:
DataTable(
columns: [
DataColumn(label: Text('Column1')),
DataColumn(label: Text('Column2')),
DataColumn(label: Text('Column3')),
],
rows: [
DataRow(cells: [
DataCell(Text('1')),
DataCell(Text('2')),
DataCell(Text('6')),
]),
DataRow(cells: [
DataCell(Text('12')),
DataCell(Text('John')),
DataCell(Text('9')),
]),
],
)
I need to:
-column1 to be 20% of the available width
-column1 to be 40% of the available width
-column1 to be 40% of the available width
How can I do this? Is it possible for DataTable?
解决方案
You can try something like this :
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(home: MyApp()));
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final double width = MediaQuery.of(context).size.width;
return Scaffold(
appBar: AppBar(
title: const Text("Flutter Demo"),
),
body: Container(
width: width,
child: DataTable(
columnSpacing: 0,
horizontalMargin: 0,
columns: [
DataColumn(
label: Container(
width: width * .2,
child: Text('Column1'),
),
),
DataColumn(
label: Container(
width: width * .4,
child: Text('Column2'),
),
),
DataColumn(
label: Container(
width: width * .4,
child: Text('Column3'),
),
),
],
rows: [
DataRow(cells: [
DataCell(Text('1')),
DataCell(Text('2')),
DataCell(Text('6')),
]),
DataRow(cells: [
DataCell(Text('12')),
DataCell(Text('John')),
DataCell(Text('9')),
]),
],
),
),
);
}
}
推荐阅读
- soapui - 如何将 groovy 脚本的输出传递给soap请求
- bixby - 使用词汇类型标记训练数据时出现“非法计划”消息
- python-3.x - 选择包含混合单引号和双引号的元组的查询
- javascript - 从特定的 onclick 函数获取输入参数
- html - 左对齐和居中对齐 div
- javascript - 将返回对象的函数转换为该对象的映射
- r - 在R中的条件下重新开始计数
- ios - 迁移运行后立即遍历 Realm 中的所有记录 - Swift / iOS
- oracle - 我可以使用默认/最大值分区创建一个不断增长的间隔分区表吗?
- java - 非单词字符分隔符“\W”将“逗号+空格”计为一个字符