我在Flutter中的布局仍然有些麻烦。现在,我要以象限布局在3个小部件之间共享可用空间。宽度是平均分配的(通过Expanded行中的2 个小部件可以正常工作),但是现在我还希望高度自动调整为widget3.height == widget1.height + widget2.height。 如果含量较大,我想和以 反之亦然调整高度和副。 布局widget3widget1widget2
widget3.height == widget1.height + widget2.height
在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