Elastic layout (Flex)
Flexible layout allows the subassembly to allocate a certain percentage of the parent container space. Concept of an elastomeric layout UI there are also other systems, such as the layout of the resilient cartridge in H5, Android the FlexboxLayout
like. Flutter primarily by the elastic layout Flex
and Expanded
to fit achieved.
Flex
Flex
Assembly may be arranged along a horizontal or vertical direction sub-assembly, if you know the direction of the spindle, to use Row
or Column
may be convenient since Row
, and Column
are inherited from Flex
, substantially the same parameters, it can be used where Flex basically be used Row
or Column
. Flex
Function itself is very powerful, and it can also be Expanded
components with the realization of elastic layout. Next we discuss only Flex
and elastic layout-related properties (other attributes have been introduced Row
and Column
introduced over time).
The Flex ({ ... @required this.direction, // the layout direction of the elastic, Row default horizontal direction, Column default vertical direction List < the Widget > Children = const < the Widget > [], })
Flex
Inherited from MultiChildRenderObjectWidget
the corresponding RenderObject
is RenderFlex
, RenderFlex
to achieve its layout algorithm.
Expanded
It can be scaled "expanding stretch" Row
, Column
and Flex
the space occupied by the subassembly.
const Expanded({
int flex = 1,
@required Widget child,
})
flex
Parameter is the spring constant, or if it is 0 null
, it child
is not resilient, i.e., space is not occupied by the expanded stretched. If more than 0, all Expanded
in proportion to the divided spindle which flex the entire free space. Let's look at an example:
class FlexLayoutTestRoute extends StatelessWidget { @override Widget build(BuildContext context) { return Column( children: <Widget>[ //Flex的两个子widget按1:2来占据水平空间 Flex( direction: Axis.horizontal, children: <Widget>[ Expanded( flex: 1, child: Container( height: 30.0, color: Colors.red, ), ), Expanded( flex: 2 , Child: Container ( height: 30.0 , Color: Colors.green, ), ), ], ), the Padding ( padding: const EdgeInsets.only (Top: 20.0 ), Child: SizedBox ( height: 100.0 , // the Flex the three sub-widget, in the vertical direction by 2: 1: 1 to 100 pixels space child: the Flex ( direction: Axis.vertical, Children: <the Widget> [ Expanded ( Flex: 2, child: Container( height: 30.0, color: Colors.red, ), ), Spacer( flex: 1, ), Expanded( flex: 1, child: Container( height: 30.0, color: Colors.green, ), ), ], ), ), ), ], ); } }
Operating results as shown:
The exemplary Spacer
function is specified space ratio, it is actually just Expanded
a wrapper class Spacer
source code as follows:
class Spacer extends StatelessWidget { const Spacer({Key key, this.flex = 1}) : assert(flex != null), assert(flex > 0), super(key: key); final int flex; @override Widget build(BuildContext context) { return Expanded( flex: flex, child: const SizedBox.shrink(), ); } }
summary
Elastic layout is relatively simple, the only caveat is that Row
, Column
as well as Flex
relationships.