我一直在尝试使用 Table 小部件在 Flutter 中创建一个表格,并将其展开到屏幕底部,而无需滚动查看整个表格。但是,似乎 Text 小部件强制行具有特定高度,导致最后一行溢出。
一个有趣的旁注是,表格所在的扩展小部件不会溢出屏幕,因此即使表格溢出扩展小部件,也不会显示错误消息。相反,看起来表格在 Expanded 小部件中溢出。
示例代码:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter table example',
home: Scaffold(
appBar: AppBar(
title: Text('Flutter table example'),
),
body: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(4.0),
child: Image.network('https://imgs.xkcd.com/comics/minifigs.png'),
),
Expanded(
child: Container(
padding: const EdgeInsets.all(6.0),
child: Table(
columnWidths: {
0: FlexColumnWidth(1),
1: FlexColumnWidth(2),
2: FlexColumnWidth(2),
},
border: TableBorder(
horizontalInside: new BorderSide(color: Colors.grey[300], width: 1)
),
defaultVerticalAlignment: TableCellVerticalAlignment.middle,
children:
List<TableRow>.generate(10, (int i){
return TableRow(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
(1990 + i).toString(),
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 14,
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Normal people',
textAlign: TextAlign.center,
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Lego people',
textAlign: TextAlign.center,
),
),
]
);
}),
),
),
)
],
),
),
);
}
}
和输出: