Flutter Layout Row / Column - 共享宽度,展开高度
Flutter Layout Row / Column - share width, expand height
我在 Flutter 中的布局仍然有点问题。
现在我想在象限布局中的 3 个小部件之间共享可用的 space。
宽度是均匀共享的(这可以通过连续 2 Expanded
个小部件正常工作),但现在我也希望高度自动调整,所以 widget3.height == widget1.height + widget2.height
。
如果widget3
的内容比较大,我想让widget1
和widget2
调整高度,反之亦然。
这在 Flutter 中是否可行?
看看IntrinsicHeight
;包装根行应该提供您正在寻找的效果:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(title: Text('Rows & Columns')),
body: RowsAndColumns(),
),
);
}
}
class RowsAndColumns extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(top: 100.0),
child: IntrinsicHeight(
child: Row(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
Expanded(
child: Column(children: [
Container(height: 120.0, color: Colors.yellow),
Container(height: 100.0, color: Colors.cyan),
]),
),
Expanded(child: Container(color: Colors.amber)),
]),
),
);
}
}
调整列中容器的高度会导致右侧容器调整大小以匹配:
https://gist.github.com/mjohnsullivan/c5b661d7b3b4ca00599e8ef87ff6ac61
我是 Flutter 新手。如果我在这里做错了什么,请纠正我。我认为不使用 IntrinsicHeight 也可以使用 setState((){}) 来实现。
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}`enter code here`
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return MyAppState();
}
}
class MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
double h1 = 100;
double h2 = 200;
double h3;
setState(() {
h3 = h1 + h2;
});
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Padding(
padding: const EdgeInsets.only(top: 100.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child: Column(
children: [
Container(
color: Colors.green,
height: h1,
),
Container(
color: Colors.orange,
height: h2,
)
],
),
),
Expanded(
child: Container(
color: Colors.yellow,
height: h3,
),
)
],
),
),
),
),
);
}
}
Find image here
代码也可以在这里找到:https://gist.github.com/Amrit0786/9d228017c2df6cf277fbbaa4a6b20e83
我认为你可以设置行的高度,然后你只需要设置列容器的高度,并使用 crossAxisAlignment: CrossAxisAlignment.stretch 第二行会展开占据他父级的高度:
SizedBox(
height: 100,
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
child: Column(
children: [
Expanded( //If you dont have the height you can expanded with flex
flex: 1,
child: Container(
height: 50,
color: Colors.blue,
),
),
Expanded(
flex: 1,
child: Container(
height: 50,
color: Colors.red,
),
)
],
),
),
Expanded(
child: Container(
color: Colors.yellow,
),
)
],
),
),
既然IntrinsicHeight
被认为是relatively expensive,最好还是避免。您可以使用 Table
with verticalAlignment: TableCellVerticalAlignment.fill 在最大的 TableCell
.
请注意,如果您在所有单元格中使用 .fill
,则 TableRow
将具有 zero height。
Table(children: [
TableRow(children: [
Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
alignment: Alignment.center,
color: Colors.blue,
child: Text('Widget 1'),
),
Container(
alignment: Alignment.center,
color: Colors.green,
child: Text('Widget 2'),
),
],
),
TableCell(
verticalAlignment: TableCellVerticalAlignment.fill,
child: Container(
alignment: Alignment.center,
color: Colors.orange,
child: Text('Widget 3'),
)),
]),
]),
我在 Flutter 中的布局仍然有点问题。
现在我想在象限布局中的 3 个小部件之间共享可用的 space。
宽度是均匀共享的(这可以通过连续 2 Expanded
个小部件正常工作),但现在我也希望高度自动调整,所以 widget3.height == widget1.height + widget2.height
。
widget3
的内容比较大,我想让widget1
和widget2
调整高度,反之亦然。
这在 Flutter 中是否可行?
看看IntrinsicHeight
;包装根行应该提供您正在寻找的效果:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(title: Text('Rows & Columns')),
body: RowsAndColumns(),
),
);
}
}
class RowsAndColumns extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(top: 100.0),
child: IntrinsicHeight(
child: Row(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
Expanded(
child: Column(children: [
Container(height: 120.0, color: Colors.yellow),
Container(height: 100.0, color: Colors.cyan),
]),
),
Expanded(child: Container(color: Colors.amber)),
]),
),
);
}
}
调整列中容器的高度会导致右侧容器调整大小以匹配:
https://gist.github.com/mjohnsullivan/c5b661d7b3b4ca00599e8ef87ff6ac61
我是 Flutter 新手。如果我在这里做错了什么,请纠正我。我认为不使用 IntrinsicHeight 也可以使用 setState((){}) 来实现。
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}`enter code here`
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return MyAppState();
}
}
class MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
double h1 = 100;
double h2 = 200;
double h3;
setState(() {
h3 = h1 + h2;
});
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Padding(
padding: const EdgeInsets.only(top: 100.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child: Column(
children: [
Container(
color: Colors.green,
height: h1,
),
Container(
color: Colors.orange,
height: h2,
)
],
),
),
Expanded(
child: Container(
color: Colors.yellow,
height: h3,
),
)
],
),
),
),
),
);
}
}
Find image here
代码也可以在这里找到:https://gist.github.com/Amrit0786/9d228017c2df6cf277fbbaa4a6b20e83
我认为你可以设置行的高度,然后你只需要设置列容器的高度,并使用 crossAxisAlignment: CrossAxisAlignment.stretch 第二行会展开占据他父级的高度:
SizedBox(
height: 100,
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
child: Column(
children: [
Expanded( //If you dont have the height you can expanded with flex
flex: 1,
child: Container(
height: 50,
color: Colors.blue,
),
),
Expanded(
flex: 1,
child: Container(
height: 50,
color: Colors.red,
),
)
],
),
),
Expanded(
child: Container(
color: Colors.yellow,
),
)
],
),
),
既然IntrinsicHeight
被认为是relatively expensive,最好还是避免。您可以使用 Table
with verticalAlignment: TableCellVerticalAlignment.fill 在最大的 TableCell
.
请注意,如果您在所有单元格中使用 .fill
,则 TableRow
将具有 zero height。
Table(children: [
TableRow(children: [
Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
alignment: Alignment.center,
color: Colors.blue,
child: Text('Widget 1'),
),
Container(
alignment: Alignment.center,
color: Colors.green,
child: Text('Widget 2'),
),
],
),
TableCell(
verticalAlignment: TableCellVerticalAlignment.fill,
child: Container(
alignment: Alignment.center,
color: Colors.orange,
child: Text('Widget 3'),
)),
]),
]),