DataTable 列宽问题
DataTable column width issues
我正在尝试在 Flutter
中构建全宽 DataTable
,左侧有一个固定宽度的列,另外两列应将剩余的列与其他列分开。
然而,即使左侧 header 文本被截断,中间和右侧的列也不会占用剩余的宽度,如下所示:
当文本太宽而无法在一行中显示时,我也想将文本换行,但 Wrap
没有按预期工作。
我该如何解决我的问题?
代码如下:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Column(children: [
Expanded(
child: Container(
constraints: BoxConstraints.expand(width: double.infinity),
child: SingleChildScrollView(
child: DataTable(
headingRowHeight: 32,
dataRowHeight: 24,
columns: [
DataColumn(
label: ConstrainedBox(
constraints: BoxConstraints(
maxWidth: 20,
minWidth: 20,
),
child: Text('Short column'),
),
),
DataColumn(label: Text('Long column')),
DataColumn(label: Text('Long column')),
],
rows: [
DataRow(
cells: [
DataCell(
ConstrainedBox(
constraints: BoxConstraints(
maxWidth: 20,
minWidth: 20,
),
child: Text('1'),
),
),
DataCell(
Wrap(
children: [
Text(
"""Some long content i would like to be wrapped when column width is not
enought to fully display it"""),
],
),
),
DataCell(Text('Some more text')),
],
),
DataRow(
cells: [
DataCell(Container(
color: Colors.pink,
child: ConstrainedBox(
constraints: BoxConstraints(
maxWidth: 20,
minWidth: 20,
),
child: Text('2'),
),
)),
DataCell(
Wrap(
children: [
Container(
color: Colors.yellow,
child: Text(
"""Some long content i would like to be wrapped when column width is not
enought to fully display it""")),
],
),
),
DataCell(Text('Some more text')),
],
)
]),
),
),
),
]),
),
);
}
}
编辑
感谢@awaik 的回答,但在您的示例中,table 没有占据整个设备宽度,它保持在大屏幕的中间,这不是我想要的。
此外,行高是恒定的,如果内容需要更高,行高不会增加。
有什么可以做的吗?
DataTable 有一些默认值:
DataTable({
Key key,
@required this.columns,
this.sortColumnIndex,
this.sortAscending = true,
this.onSelectAll,
this.dataRowHeight = kMinInteractiveDimension,
this.headingRowHeight = 56.0,
this.horizontalMargin = 24.0,
this.columnSpacing = 56.0,
下面是删除了一些小部件的固定示例。
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: SafeArea(
child: DataTable(
horizontalMargin: 6.0,
columnSpacing: 6.0,
headingRowHeight: 32.0,
dataRowHeight: 100.0,
columns: [
DataColumn(
label: ConstrainedBox(
constraints: BoxConstraints(
maxWidth: 20,
minWidth: 20,
),
child: Text('Short column'),
),
),
DataColumn(label: Text('Long column')),
DataColumn(label: Text('Three')),
],
rows: [
DataRow(
cells: [
DataCell(
Text('1'),
),
DataCell(
Container(
child: Text(
'Some long content i would like to be wrapped ',
),
),
),
DataCell(Text('Some more text')),
],
),
DataRow(
cells: [
DataCell(Container(
color: Colors.pink,
child: Text('2'),
)),
DataCell(
Container(
height: 500,
color: Colors.yellow,
child: Text(
'Some long content i would like to be wrapped when column width is not enought to fully display itSome long content i would like to be wrapped when column width is not display it Some long content i would like to be wrapped when column width is not enought to fully display itSome long content i would like to be wrapped when column width is not display it Some long content i would like to be wrapped when column width is not enought to fully display itSome long content i would like to be wrapped when column width is not display it 555',
),
),
placeholder: false),
DataCell(Text('Some more text')),
],
),
],
),
),
),
);
}
}
编辑
- 代码向上更新了
- table 没有全宽,因为我们使用了带有树“”的多行字符串。如果我们使用常规字符串,行为是正常的。
行高在构造函数中设置,无法动态更改。
child: 数据表(
水平边距:6.0,
列间距:6.0,
headingRowHeight: 32.0,
dataRowHeight: 100.0,
最后,我个人的观点 - 创建自己的小部件比使用这个更容易 DataTable
我发现正常的 Table
允许我做我想做的事:我可以对某一列使用 FixedColumnWidth
,对其他列使用 FlexColumnWidth
,以便获取剩余 space.
此外,文本已正确换行并增加了行高以适合内容,如下图的小屏幕宽度和大屏幕宽度所示:
这是代码:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Column(children: [
Expanded(
child: Container(
child: SingleChildScrollView(
child: Table(
border: TableBorder.all(width: 1),
columnWidths: {
0: FixedColumnWidth(20),
},
defaultColumnWidth: FlexColumnWidth(),
children: [
TableRow(children: [
Text('Short column'),
Text('Long column'),
Text('Long column')
]),
TableRow(
children: [
Text('1'),
Text(
'Some long content i would like to be wrapped when column width is not enought to fully display it'),
Container(
child: Text('Some more text'),
)
],
)
],
),
),
),
),
]),
),
);
}
}
我正在尝试在 Flutter
中构建全宽 DataTable
,左侧有一个固定宽度的列,另外两列应将剩余的列与其他列分开。
然而,即使左侧 header 文本被截断,中间和右侧的列也不会占用剩余的宽度,如下所示:
当文本太宽而无法在一行中显示时,我也想将文本换行,但 Wrap
没有按预期工作。
我该如何解决我的问题?
代码如下:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Column(children: [
Expanded(
child: Container(
constraints: BoxConstraints.expand(width: double.infinity),
child: SingleChildScrollView(
child: DataTable(
headingRowHeight: 32,
dataRowHeight: 24,
columns: [
DataColumn(
label: ConstrainedBox(
constraints: BoxConstraints(
maxWidth: 20,
minWidth: 20,
),
child: Text('Short column'),
),
),
DataColumn(label: Text('Long column')),
DataColumn(label: Text('Long column')),
],
rows: [
DataRow(
cells: [
DataCell(
ConstrainedBox(
constraints: BoxConstraints(
maxWidth: 20,
minWidth: 20,
),
child: Text('1'),
),
),
DataCell(
Wrap(
children: [
Text(
"""Some long content i would like to be wrapped when column width is not
enought to fully display it"""),
],
),
),
DataCell(Text('Some more text')),
],
),
DataRow(
cells: [
DataCell(Container(
color: Colors.pink,
child: ConstrainedBox(
constraints: BoxConstraints(
maxWidth: 20,
minWidth: 20,
),
child: Text('2'),
),
)),
DataCell(
Wrap(
children: [
Container(
color: Colors.yellow,
child: Text(
"""Some long content i would like to be wrapped when column width is not
enought to fully display it""")),
],
),
),
DataCell(Text('Some more text')),
],
)
]),
),
),
),
]),
),
);
}
}
编辑
感谢@awaik 的回答,但在您的示例中,table 没有占据整个设备宽度,它保持在大屏幕的中间,这不是我想要的。
此外,行高是恒定的,如果内容需要更高,行高不会增加。
有什么可以做的吗?
DataTable 有一些默认值:
DataTable({
Key key,
@required this.columns,
this.sortColumnIndex,
this.sortAscending = true,
this.onSelectAll,
this.dataRowHeight = kMinInteractiveDimension,
this.headingRowHeight = 56.0,
this.horizontalMargin = 24.0,
this.columnSpacing = 56.0,
下面是删除了一些小部件的固定示例。
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: SafeArea(
child: DataTable(
horizontalMargin: 6.0,
columnSpacing: 6.0,
headingRowHeight: 32.0,
dataRowHeight: 100.0,
columns: [
DataColumn(
label: ConstrainedBox(
constraints: BoxConstraints(
maxWidth: 20,
minWidth: 20,
),
child: Text('Short column'),
),
),
DataColumn(label: Text('Long column')),
DataColumn(label: Text('Three')),
],
rows: [
DataRow(
cells: [
DataCell(
Text('1'),
),
DataCell(
Container(
child: Text(
'Some long content i would like to be wrapped ',
),
),
),
DataCell(Text('Some more text')),
],
),
DataRow(
cells: [
DataCell(Container(
color: Colors.pink,
child: Text('2'),
)),
DataCell(
Container(
height: 500,
color: Colors.yellow,
child: Text(
'Some long content i would like to be wrapped when column width is not enought to fully display itSome long content i would like to be wrapped when column width is not display it Some long content i would like to be wrapped when column width is not enought to fully display itSome long content i would like to be wrapped when column width is not display it Some long content i would like to be wrapped when column width is not enought to fully display itSome long content i would like to be wrapped when column width is not display it 555',
),
),
placeholder: false),
DataCell(Text('Some more text')),
],
),
],
),
),
),
);
}
}
编辑
- 代码向上更新了
- table 没有全宽,因为我们使用了带有树“”的多行字符串。如果我们使用常规字符串,行为是正常的。
行高在构造函数中设置,无法动态更改。
child: 数据表( 水平边距:6.0, 列间距:6.0, headingRowHeight: 32.0, dataRowHeight: 100.0,
最后,我个人的观点 - 创建自己的小部件比使用这个更容易 DataTable
我发现正常的 Table
允许我做我想做的事:我可以对某一列使用 FixedColumnWidth
,对其他列使用 FlexColumnWidth
,以便获取剩余 space.
此外,文本已正确换行并增加了行高以适合内容,如下图的小屏幕宽度和大屏幕宽度所示:
这是代码:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Column(children: [
Expanded(
child: Container(
child: SingleChildScrollView(
child: Table(
border: TableBorder.all(width: 1),
columnWidths: {
0: FixedColumnWidth(20),
},
defaultColumnWidth: FlexColumnWidth(),
children: [
TableRow(children: [
Text('Short column'),
Text('Long column'),
Text('Long column')
]),
TableRow(
children: [
Text('1'),
Text(
'Some long content i would like to be wrapped when column width is not enought to fully display it'),
Container(
child: Text('Some more text'),
)
],
)
],
),
),
),
),
]),
),
);
}
}