Row and Column

MainAxisAlignment

MainAxisAlignment.start

new Row /*or Column*/( 
  mainAxisAlignment: MainAxisAlignment.start,
  children: <Widget>[
    new Icon(Icons.star, size: 50.0),
    new Icon(Icons.star, size: 50.0),
    new Icon(Icons.star, size: 50.0),
  ],
),
1
2
3
4
5
6
7
8

MainAxisAlignment MainAxisAlignment

MainAxisAlignment.center

new Row /*or Column*/( 
  mainAxisAlignment: MainAxisAlignment.center,
  children: <Widget>[
    new Icon(Icons.star, size: 50.0),
    new Icon(Icons.star, size: 50.0),
    new Icon(Icons.star, size: 50.0),
  ],
),
1
2
3
4
5
6
7
8

MainAxisAlignment MainAxisAlignment

MainAxisAlignment.end

new Row /*or Column*/( 
  mainAxisAlignment: MainAxisAlignment.end,
  children: <Widget>[
    new Icon(Icons.star, size: 50.0),
    new Icon(Icons.star, size: 50.0),
    new Icon(Icons.star, size: 50.0),
  ],
),
1
2
3
4
5
6
7
8

MainAxisAlignment MainAxisAlignment

MainAxisAlignment.spaceEvenly

new Row /*or Column*/( 
  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  children: <Widget>[
    new Icon(Icons.star, size: 50.0),
    new Icon(Icons.star, size: 50.0),
    new Icon(Icons.star, size: 50.0),
  ],
),
1
2
3
4
5
6
7
8

MainAxisAlignment MainAxisAlignment

MainAxisAlignment.spaceBetween

new Row /*or Column*/( 
  mainAxisAlignment: MainAxisAlignment.spaceBetween,
  children: <Widget>[
    new Icon(Icons.star, size: 50.0),
    new Icon(Icons.star, size: 50.0),
    new Icon(Icons.star, size: 50.0),
  ],
),
1
2
3
4
5
6
7
8

MainAxisAlignment MainAxisAlignment

MainAxisAlignment.spaceAround

new Row /*or Column*/( 
  mainAxisAlignment: MainAxisAlignment.spaceAround,
  children: <Widget>[
    new Icon(Icons.star, size: 50.0),
    new Icon(Icons.star, size: 50.0),
    new Icon(Icons.star, size: 50.0),
  ],
),
1
2
3
4
5
6
7
8

MainAxisAlignment MainAxisAlignment

CrossAxisAlignment

CrossAxisAlignment.baseline

new Row(
  crossAxisAlignment: CrossAxisAlignment.baseline,
  textBaseline: TextBaseline.alphabetic,
  children: <Widget>[
    new Text(
      'Baseline',
      style: Theme.of(context).textTheme.display3,
    ),
    new Text(
      'Baseline',
      style: Theme.of(context).textTheme.body1,
    ),
  ],
),
1
2
3
4
5
6
7
8
9
10
11
12
13
14

CrossAxisAlignment

CrossAxisAlignment.start

new Row /*or Column*/( 
  crossAxisAlignment: CrossAxisAlignment.start,
  children: <Widget>[
    new Icon(Icons.star, size: 50.0),
    new Icon(Icons.star, size: 200.0),
    new Icon(Icons.star, size: 50.0),
  ],
),
1
2
3
4
5
6
7
8

CrossAxisAlignment CrossAxisAlignment

CrossAxisAlignment.center

new Row /*or Column*/( 
  crossAxisAlignment: CrossAxisAlignment.center,
  children: <Widget>[
    new Icon(Icons.star, size: 50.0),
    new Icon(Icons.star, size: 200.0),
    new Icon(Icons.star, size: 50.0),
  ],
),
1
2
3
4
5
6
7
8

CrossAxisAlignment CrossAxisAlignment

CrossAxisAlignment.end

new Row /*or Column*/( 
  crossAxisAlignment: CrossAxisAlignment.end,
  children: <Widget>[
    new Icon(Icons.star, size: 50.0),
    new Icon(Icons.star, size: 200.0),
    new Icon(Icons.star, size: 50.0),
  ],
),
1
2
3
4
5
6
7
8

CrossAxisAlignment CrossAxisAlignment

CrossAxisAlignment.stretch

new Row /*or Column*/( 
  crossAxisAlignment: CrossAxisAlignment.stretch,
  children: <Widget>[
    new Icon(Icons.star, size: 50.0),
    new Icon(Icons.star, size: 200.0),
    new Icon(Icons.star, size: 50.0),
  ],
),
1
2
3
4
5
6
7
8

CrossAxisAlignment CrossAxisAlignment

MainAxisSize

MainAxisSize.max

new Row /*or Column*/( 
  mainAxisSize: MainAxisSize.max,
  children: <Widget>[
    new Icon(Icons.star, size: 50.0),
    new Icon(Icons.star, size: 50.0),
    new Icon(Icons.star, size: 50.0),
  ],
),
1
2
3
4
5
6
7
8

CrossAxisAlignment CrossAxisAlignment

MainAxisSize.min

new Row /*or Column*/( 
  mainAxisSize: MainAxisSize.min,
  children: <Widget>[
    new Icon(Icons.star, size: 50.0),
    new Icon(Icons.star, size: 50.0),
    new Icon(Icons.star, size: 50.0),
  ],
),
1
2
3
4
5
6
7
8

CrossAxisAlignment CrossAxisAlignment

IntrinsicWidth and IntrinsicHeight

让一行或者一列中的组件同高或者同宽

Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(title: Text('IntrinsicWidth')),
    body: Center(
      child: Column(
        children: <Widget>[
          RaisedButton(
            onPressed: () {},
            child: Text('Short'),
          ),
          RaisedButton(
            onPressed: () {},
            child: Text('A bit Longer'),
          ),
          RaisedButton(
            onPressed: () {},
            child: Text('The Longest text button'),
          ),
        ],
      ),
    ),
  );
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

IntrinsicWidth

Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(title: Text('IntrinsicWidth')),
    body: Center(
      child: IntrinsicHeight(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[
            RaisedButton(
              onPressed: () {},
              child: Text('Short'),
            ),
            RaisedButton(
              onPressed: () {},
              child: Text('A bit Longer'),
            ),
            RaisedButton(
              onPressed: () {},
              child: Text('The Longest text button'),
            ),
          ],
        ),
      ),
    ),
  );
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

IntrinsicHeight

Stack


Widget build(BuildContext context) {
  Widget main = Scaffold(
    appBar: AppBar(title: Text('Stack')),
  );

  return Stack(
    fit: StackFit.expand,
    children: <Widget>[
      main,
      Banner(
        message: "Top Start",
        location: BannerLocation.topStart,
      ),
      Banner(
        message: "Top End",
        location: BannerLocation.topEnd,
      ),
      Banner(
        message: "Bottom Start",
        location: BannerLocation.bottomStart,
      ),
      Banner(
        message: "Bottom End",
        location: BannerLocation.bottomEnd,
      ),
    ],
  );
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

Stack

Widget build(BuildContext context) {
  return new Scaffold(
    appBar: AppBar(title: Text('Stack')),
    body: Stack(
      fit: StackFit.expand,
      children: <Widget>[
        Material(color: Colors.yellowAccent),
        Positioned(
          top: 0.0,
          left: 0.0,
          child: Icon(Icons.star, size: 50.0),
        ),
        Positioned(
          top: 340.0,
          left: 250.0,
          child: Icon(Icons.call, size: 50.0),
        ),
      ],
    ),
  );
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

Stack

Widget build(BuildContext context) {
  const iconSize = 50.0;
  return Scaffold(
    appBar: AppBar(title: Text('Stack with LayoutBuilder')),
    body: LayoutBuilder(
      builder: (context, constraints) =>
        Stack(
          fit: StackFit.expand,
          children: <Widget>[
            Material(color: Colors.yellowAccent),
            Positioned(
              top: 0.0,
              child: Icon(Icons.star, size: iconSize),
            ),
            Positioned(
              top: constraints.maxHeight - iconSize,
              left: constraints.maxWidth - iconSize,
              child: Icon(Icons.call, size: iconSize),
            ),
          ],
        ),
    ),
  );
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

Stack

Expanded

new Row(
  children: <Widget>[
    new Expanded(
      child: new Container(
        decoration: const BoxDecoration(color: Colors.red),
      ),
      flex: 3,
    ),
    new Expanded(
      child: new Container(
        decoration: const BoxDecoration(color: Colors.green),
      ),
      flex: 2,
    ),
    new Expanded(
      child: new Container(
        decoration: const BoxDecoration(color: Colors.blue),
      ),
      flex: 1,
    ),
  ],
),
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

Expanded

ConstrainedBox

new Card(child: const Text('Hello World!'), color: Colors.yellow)

1
2

ConstrainedBox

new ConstrainedBox( 
  constraints: new BoxConstraints.expand(),
  child: const Card(
    child: const Text('Hello World!'), 
    color: Colors.yellow,
  ), 
),
1
2
3
4
5
6
7

ConstrainedBox

new ConstrainedBox(
  constraints: new BoxConstraints.expand(height: 300.0),
  child: const Card(
    child: const Text('Hello World!'), 
    color: Colors.yellow,
  ),
),
/// same as -------
new ConstrainedBox(
  constraints: new BoxConstraints(
    minWidth: double.infinity,
    maxWidth: double.infinity,
    minHeight: 300.0,
    maxHeight: 300.0,
  ),
  child: const Card(
    child: const Text('Hello World!'), 
    color: Colors.yellow,
  ),
),

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

Container

Container as a layout tool

Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(title: Text('Container as a layout')),
    body: Container(
      color: Colors.yellowAccent,
      child: Text("Hi"),
    ),
  );
}
1
2
3
4
5
6
7
8
9

Container

Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(title: Text('Container as a layout')),
    body: Container(
      height: double.infinity,
      width: double.infinity,
      color: Colors.yellowAccent,
      child: Text("Hi"),
    ),
  );
}
1
2
3
4
5
6
7
8
9
10
11

Container

Container as decoration

Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(title: Text('Container.decoration')),
    body: Container(
      height: double.infinity,
      width: double.infinity,
      decoration: BoxDecoration(color: Colors.yellowAccent),
      child: Text("Hi"),
    ),
  );
}
1
2
3
4
5
6
7
8
9
10
11

Container

Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(title: Text('Container.foregroundDecoration')),
    body: Container(
      height: double.infinity,
      width: double.infinity,
      decoration: BoxDecoration(color: Colors.yellowAccent),
      foregroundDecoration: BoxDecoration(color: Colors.red.withOpacity(0.5)),
      child: Text("Hi"),
    ),
  );
}
1
2
3
4
5
6
7
8
9
10
11
12

Container

Container as Transform

Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(title: Text('Container.transform')),
    body: Container(
      height: 300.0,
      width: 300.0,
      transform: Matrix4.rotationZ(pi / 4),
      decoration: BoxDecoration(color: Colors.yellowAccent),
      child: Text(
        "Hi",
        textAlign: TextAlign.center,
      ),
    ),
  );
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Container

SizedBox

SizedBox as ConstrainedBox

SizedBox.expand(
  child: Card(
    child: Text('Hello World!'),
    color: Colors.yellowAccent,
  ),
),
1
2
3
4
5
6

SizedBox

SizedBox as padding

Column(
  children: <Widget>[
    Icon(Icons.star, size: 50.0),
    const SizedBox(height: 100.0,),
    Icon(Icons.star, size: 50.0),
    Icon(Icons.star, size: 50.0)
  ],
),
1
2
3
4
5
6
7
8

SizedBox

SizedBox as an Invisible Object

Widget build(BuildContext context) {
  bool isVisible = ...
  return Scaffold(
    appBar: AppBar(
      title: Text('isVisible = $isVisible'),
    ),
    body: isVisible 
      ? Icon(Icons.star, size: 150.0) 
      : const SizedBox(),
  );
}
1
2
3
4
5
6
7
8
9
10
11

SizedBox SizedBox

SafeArea

Widget build(BuildContext context) {
  return Material(
    color: Colors.blue,
    child: SafeArea(
      child: SizedBox.expand(
        child: Card(color: Colors.yellowAccent),
      ),
    ),
  );
}
1
2
3
4
5
6
7
8
9
10

SafeArea SafeArea