spinning_mixed.dart 3.39 KB
Newer Older
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:ui' as ui;
6

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

10
import 'package:flutter_rendering_examples/solid_color_box.dart';
11 12

// Solid colour, RenderObject version
13
void addFlexChildSolidColor(RenderFlex parent, ui.Color backgroundColor, { int flex: 0 }) {
14 15
  RenderSolidColorBox child = new RenderSolidColorBox(backgroundColor);
  parent.add(child);
16 17
  FlexParentData childParentData = child.parentData;
  childParentData.flex = flex;
18 19 20
}

// Solid colour, Widget version
21
class Rectangle extends StatelessComponent {
22
  Rectangle(this.color, { Key key }) : super(key: key);
23
  final Color color;
24
  Widget build(BuildContext context) {
25 26 27 28 29 30 31 32
    return new Flexible(
      child: new Container(
        decoration: new BoxDecoration(backgroundColor: color)
      )
    );
  }
}

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
double value;
RenderObjectToWidgetElement<RenderBox> element;
void attachWidgetTreeToRenderTree(RenderProxyBox container) {
  element = new RenderObjectToWidgetAdapter<RenderBox>(
    container: container,
    child: new Container(
      height: 300.0,
      child: new Column(<Widget>[
          new Rectangle(const Color(0xFF00FFFF)),
          new Container(
            padding: new EdgeDims.all(10.0),
            margin: new EdgeDims.all(10.0),
            decoration: new BoxDecoration(backgroundColor: const Color(0xFFCCCCCC)),
            child: new Row(<Widget>[
                new RaisedButton(
                  child: new Row(<Widget>[
                      new NetworkImage(src: "http://flutter.io/favicon.ico"),
                      new Text('PRESS ME'),
                    ]
                  ),
                  onPressed: () {
                    value = value == null ? 0.1 : (value + 0.1) % 1.0;
                    attachWidgetTreeToRenderTree(container);
                  }
                ),
                new CircularProgressIndicator(value: value),
              ],
              justifyContent: FlexJustifyContent.spaceAround
            )
          ),
          new Rectangle(const Color(0xFFFFFF00)),
        ],
        justifyContent: FlexJustifyContent.spaceBetween
      )
67
    )
68
  ).attachToRenderTree(element);
69 70
}

71
Duration timeBase;
72 73
RenderTransform transformBox;

74
void rotate(Duration timeStamp) {
75 76
  if (timeBase == null)
    timeBase = timeStamp;
77
  double delta = (timeStamp - timeBase).inMicroseconds.toDouble() / Duration.MICROSECONDS_PER_SECOND; // radians
78 79 80 81 82 83 84 85 86

  transformBox.setIdentity();
  transformBox.translate(transformBox.size.width / 2.0, transformBox.size.height / 2.0);
  transformBox.rotateZ(delta);
  transformBox.translate(-transformBox.size.width / 2.0, -transformBox.size.height / 2.0);
}

void main() {
  RenderProxyBox proxy = new RenderProxyBox();
87
  attachWidgetTreeToRenderTree(proxy);
88

89
  RenderFlex flexRoot = new RenderFlex(direction: FlexDirection.vertical);
90
  addFlexChildSolidColor(flexRoot, const ui.Color(0xFFFF00FF), flex: 1);
91
  flexRoot.add(proxy);
92
  addFlexChildSolidColor(flexRoot, const ui.Color(0xFF0000FF), flex: 1);
93 94 95 96

  transformBox = new RenderTransform(child: flexRoot, transform: new Matrix4.identity());
  RenderPadding root = new RenderPadding(padding: new EdgeDims.all(80.0), child: transformBox);

Ian Hickson's avatar
Ian Hickson committed
97 98 99
  WidgetFlutterBinding.ensureInitialized()
   ..renderView.child = root
   ..addPersistentFrameCallback(rotate);
100
}