flex_layout.dart 3.87 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6 7
// This example shows how to use flex layout directly in the underlying render
// tree.

8
import 'package:flutter/rendering.dart';
9

10
import 'src/solid_color_box.dart';
11 12

void main() {
13
  final RenderFlex table = RenderFlex(direction: Axis.vertical, textDirection: TextDirection.ltr);
14

15
  void addAlignmentRow(CrossAxisAlignment crossAxisAlignment) {
16
    TextStyle style = const TextStyle(color: Color(0xFF000000));
17 18
    final RenderParagraph paragraph = RenderParagraph(
      TextSpan(style: style, text: '$crossAxisAlignment'),
Ian Hickson's avatar
Ian Hickson committed
19 20
      textDirection: TextDirection.ltr,
    );
21 22
    table.add(RenderPadding(child: paragraph, padding: const EdgeInsets.only(top: 20.0)));
    final RenderFlex row = RenderFlex(crossAxisAlignment: crossAxisAlignment, textBaseline: TextBaseline.alphabetic, textDirection: TextDirection.ltr);
23
    style = const TextStyle(fontSize: 15.0, color: Color(0xFF000000));
24
    row.add(RenderDecoratedBox(
25
      decoration: const BoxDecoration(color: Color(0x7FFFCCCC)),
26 27
      child: RenderParagraph(
        TextSpan(style: style, text: 'foo foo foo'),
Ian Hickson's avatar
Ian Hickson committed
28 29
        textDirection: TextDirection.ltr,
      ),
30
    ));
31
    style = const TextStyle(fontSize: 10.0, color: Color(0xFF000000));
32
    row.add(RenderDecoratedBox(
33
      decoration: const BoxDecoration(color: Color(0x7FCCFFCC)),
34 35
      child: RenderParagraph(
        TextSpan(style: style, text: 'foo foo foo'),
Ian Hickson's avatar
Ian Hickson committed
36 37
        textDirection: TextDirection.ltr,
      ),
38
    ));
39
    final RenderFlex subrow = RenderFlex(crossAxisAlignment: crossAxisAlignment, textBaseline: TextBaseline.alphabetic, textDirection: TextDirection.ltr);
40
    style = const TextStyle(fontSize: 25.0, color: Color(0xFF000000));
41
    subrow.add(RenderDecoratedBox(
42
      decoration: const BoxDecoration(color: Color(0x7FCCCCFF)),
43 44
      child: RenderParagraph(
        TextSpan(style: style, text: 'foo foo foo foo'),
Ian Hickson's avatar
Ian Hickson committed
45 46
        textDirection: TextDirection.ltr,
      ),
47
    ));
48
    subrow.add(RenderSolidColorBox(const Color(0x7FCCFFFF), desiredSize: const Size(30.0, 40.0)));
Collin Jackson's avatar
Collin Jackson committed
49
    row.add(subrow);
50
    table.add(row);
51
    final FlexParentData rowParentData = row.parentData! as FlexParentData;
Hixie's avatar
Hixie committed
52
    rowParentData.flex = 1;
53 54
  }

55 56 57 58 59
  addAlignmentRow(CrossAxisAlignment.start);
  addAlignmentRow(CrossAxisAlignment.end);
  addAlignmentRow(CrossAxisAlignment.center);
  addAlignmentRow(CrossAxisAlignment.stretch);
  addAlignmentRow(CrossAxisAlignment.baseline);
60

61
  void addJustificationRow(MainAxisAlignment justify) {
62
    const TextStyle style = TextStyle(color: Color(0xFF000000));
63 64
    final RenderParagraph paragraph = RenderParagraph(
      TextSpan(style: style, text: '$justify'),
Ian Hickson's avatar
Ian Hickson committed
65 66
      textDirection: TextDirection.ltr,
    );
67
    table.add(RenderPadding(child: paragraph, padding: const EdgeInsets.only(top: 20.0)));
68
    final RenderFlex row = RenderFlex(textDirection: TextDirection.ltr);
69 70 71
    row.add(RenderSolidColorBox(const Color(0xFFFFCCCC), desiredSize: const Size(80.0, 60.0)));
    row.add(RenderSolidColorBox(const Color(0xFFCCFFCC), desiredSize: const Size(64.0, 60.0)));
    row.add(RenderSolidColorBox(const Color(0xFFCCCCFF), desiredSize: const Size(160.0, 60.0)));
72
    row.mainAxisAlignment = justify;
73
    table.add(row);
74
    final FlexParentData rowParentData = row.parentData! as FlexParentData;
75 76 77
    rowParentData.flex = 1;
  }

78 79 80 81 82
  addJustificationRow(MainAxisAlignment.start);
  addJustificationRow(MainAxisAlignment.end);
  addJustificationRow(MainAxisAlignment.center);
  addJustificationRow(MainAxisAlignment.spaceBetween);
  addJustificationRow(MainAxisAlignment.spaceAround);
83

84
  final RenderDecoratedBox root = RenderDecoratedBox(
85
    decoration: const BoxDecoration(color: Color(0xFFFFFFFF)),
86
    child: RenderPadding(child: table, padding: const EdgeInsets.symmetric(vertical: 50.0)),
87 88
  );

89
  RenderingFlutterBinding(root: root).scheduleFrame();
90
}