spinning_mixed.dart 3.49 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 6
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
7

8
import 'package:flutter_rendering_examples/solid_color_box.dart';
9 10

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

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

31 32 33 34 35 36 37
double value;
RenderObjectToWidgetElement<RenderBox> element;
void attachWidgetTreeToRenderTree(RenderProxyBox container) {
  element = new RenderObjectToWidgetAdapter<RenderBox>(
    container: container,
    child: new Container(
      height: 300.0,
38 39
      child: new Column(
        children: <Widget>[
40
          new Rectangle(const Color(0xFF00FFFF)),
Hixie's avatar
Hixie committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
          new Material(
            child: new Container(
              padding: new EdgeDims.all(10.0),
              margin: new EdgeDims.all(10.0),
              child: new Row(
                children: <Widget>[
                  new RaisedButton(
                    child: new Row(
                      children: <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);
                    }
58
                  ),
Hixie's avatar
Hixie committed
59 60 61 62
                  new CircularProgressIndicator(value: value),
                ],
                justifyContent: FlexJustifyContent.spaceAround
              )
63 64 65 66 67 68
            )
          ),
          new Rectangle(const Color(0xFFFFFF00)),
        ],
        justifyContent: FlexJustifyContent.spaceBetween
      )
69
    )
70
  ).attachToRenderTree(element);
71 72
}

73
Duration timeBase;
74 75
RenderTransform transformBox;

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

  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() {
Hixie's avatar
Hixie committed
88
  WidgetFlutterBinding.ensureInitialized();
89
  RenderProxyBox proxy = new RenderProxyBox();
90
  attachWidgetTreeToRenderTree(proxy);
91

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

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

Hixie's avatar
Hixie committed
100 101
  WidgetFlutterBinding.instance.renderView.child = root;
  WidgetFlutterBinding.instance.addPersistentFrameCallback(rotate);
102
}