我需要使用Flutter创建全屏表格。由于我无法使用Table
/DataTable
这样做,所以我尝试使用Flex
/Expanded
。
Everithing可以按预期工作,但是我无法设置Expanded
的child
的样式,因此例如看来我无法设置填充。
到目前为止,这是我的代码。main.dart
:
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
home: Calendar2(),
);
}
}
calendar2.dart
:import 'package:flutter/material.dart';
class Calendar2 extends StatefulWidget {
@override
State<StatefulWidget> createState() => _Calendar2State();
}
class _Calendar2State extends State<Calendar2> {
@override
Widget build(BuildContext context) {
List<CalendarRow> tableRows = buildTable();
print(tableRows);
return Scaffold(
body: Flex(
direction: Axis.vertical,
children: tableRows,
)
);
}
List<CalendarRow> buildTable() {
List<CalendarRow> rows = <CalendarRow>[];
for(int i=0; i<4; i++) {
List<Widget> children = [];
for(int j=0; j<7; j++) {
children.add(
CalendarCell(
child: Text((i * 7 + j + 1).toString())
)
);
}
rows.add(
CalendarRow(
children: children
)
);
}
return rows;
}
}
class CalendarCell extends StatelessWidget {
Widget child;
CalendarCell({this.child}) {}
@override
Widget build(BuildContext context) {
return Expanded (
flex: 1,
child: Padding(
child: child,
padding: EdgeInsets.all(0.0), // this doesn't work for top and bottom
)
);
}
}
class CalendarRow extends StatelessWidget {
List<Widget> children = <Widget>[];
CalendarRow({this.children}) {}
@override
Widget build(BuildContext context) {
return Expanded(
flex: 1,
child: Flex(
direction: Axis.horizontal,
children: children
)
);
}
}
如何在
child
中设置Expanded
的样式? 最佳答案
这是在flutter中用Expanded小部件完成填充的方式
class DicePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Row(
children: <Widget>[
Expanded(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Image.asset('images/dice1.png'),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Image.asset('images/dice1.png'),
),
),
],
),
);
关于android - 内部填充展开,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52642665/