有没有一种方法可以根据 Flutter 中可用的 space 自动调整 Table 小部件的行高?
Is there a way to automatically size the row heights of a Table widget based on available space in Flutter?
我一直在尝试使用 Table 小部件在 Flutter 中创建一个 table 并将其扩展到屏幕底部,而无需滚动查看整个 table.但是,文本小部件似乎强制行具有特定高度,导致最后一行溢出。
一个有趣的旁注是 Table 所在的扩展小部件不会溢出屏幕,因此即使 Table 溢出扩展小部件也不会显示错误消息。相反,似乎 Table 在 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,
),
),
]
);
}),
),
),
)
],
),
),
);
}
}
并且输出:
Screenshot of example app with overflowing table
希望对您有所帮助
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: Container(
child: ListView(
shrinkWrap:true,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(4.0),
child:
Image.network('https://imgs.xkcd.com/comics/minifigs.png'),
),
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(20, (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,
),
),
]);
}),
),
),
],
),
),
),
);
}
}
我一直在尝试使用 Table 小部件在 Flutter 中创建一个 table 并将其扩展到屏幕底部,而无需滚动查看整个 table.但是,文本小部件似乎强制行具有特定高度,导致最后一行溢出。
一个有趣的旁注是 Table 所在的扩展小部件不会溢出屏幕,因此即使 Table 溢出扩展小部件也不会显示错误消息。相反,似乎 Table 在 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,
),
),
]
);
}),
),
),
)
],
),
),
);
}
}
并且输出:
Screenshot of example app with overflowing table
希望对您有所帮助
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: Container(
child: ListView(
shrinkWrap:true,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(4.0),
child:
Image.network('https://imgs.xkcd.com/comics/minifigs.png'),
),
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(20, (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,
),
),
]);
}),
),
),
],
),
),
),
);
}
}