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

75
Duration timeBase;
76 77
RenderTransform transformBox;

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

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

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

  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
102 103
  WidgetFlutterBinding.instance.renderView.child = root;
  WidgetFlutterBinding.instance.addPersistentFrameCallback(rotate);
104
}