【发布时间】:2020-08-25 03:56:18
【问题描述】:
我正在尝试在Flutter 中构建一个全宽DataTable,左侧有一个固定宽度的列,另外两列应该将其余列分开。
但是,即使左侧标题文本被截断,中间和右侧列也不会占用剩余宽度,如下所示:
当文本太宽而无法在单行中显示时,我还想将文本换行,但 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 的回答,但在您的示例中,表格没有占据整个设备的宽度,它仍然在中间有一个大屏幕,这不是我想要的。
另外,行高是恒定的,如果内容需要更高的高度,它不会增加。
有什么可以做的吗?
【问题讨论】:
标签: flutter datatable width flutter-desktop