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 '../rendering/src/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 StatelessWidget {
20
  Rectangle(this.color, { Key key }) : super(key: key);
21

22
  final Color color;
23 24

  @override
25
  Widget build(BuildContext context) {
26 27 28 29 30 31 32 33
    return new Flexible(
      child: new Container(
        decoration: new BoxDecoration(backgroundColor: color)
      )
    );
  }
}

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

76
Duration timeBase;
77 78
RenderTransform transformBox;

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

  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
91
  WidgetFlutterBinding.ensureInitialized();
92
  RenderProxyBox proxy = new RenderProxyBox();
93
  attachWidgetTreeToRenderTree(proxy);
94

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

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

Hixie's avatar
Hixie committed
103 104
  WidgetFlutterBinding.instance.renderView.child = root;
  WidgetFlutterBinding.instance.addPersistentFrameCallback(rotate);
105
}