1// Copyright 2015 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5import 'package:flutter/material.dart'; 6import 'package:flutter/rendering.dart'; 7 8import '../rendering/src/solid_color_box.dart'; 9 10// Solid color, RenderObject version 11void addFlexChildSolidColor(RenderFlex parent, Color backgroundColor, { int flex = 0 }) { 12 final RenderSolidColorBox child = RenderSolidColorBox(backgroundColor); 13 parent.add(child); 14 final FlexParentData childParentData = child.parentData; 15 childParentData.flex = flex; 16} 17 18// Solid color, Widget version 19class Rectangle extends StatelessWidget { 20 const Rectangle(this.color, { Key key }) : super(key: key); 21 22 final Color color; 23 24 @override 25 Widget build(BuildContext context) { 26 return Expanded( 27 child: Container( 28 color: color, 29 ), 30 ); 31 } 32} 33 34double value; 35RenderObjectToWidgetElement<RenderBox> element; 36BuildOwner owner = BuildOwner(); 37void attachWidgetTreeToRenderTree(RenderProxyBox container) { 38 element = RenderObjectToWidgetAdapter<RenderBox>( 39 container: container, 40 child: Directionality( 41 textDirection: TextDirection.ltr, 42 child: Container( 43 height: 300.0, 44 child: Column( 45 children: <Widget>[ 46 const Rectangle(Color(0xFF00FFFF)), 47 Material( 48 child: Container( 49 padding: const EdgeInsets.all(10.0), 50 margin: const EdgeInsets.all(10.0), 51 child: Row( 52 children: <Widget>[ 53 RaisedButton( 54 child: Row( 55 children: <Widget>[ 56 Image.network('https://flutter.dev/images/favicon.png'), 57 const Text('PRESS ME'), 58 ], 59 ), 60 onPressed: () { 61 value = value == null ? 0.1 : (value + 0.1) % 1.0; 62 attachWidgetTreeToRenderTree(container); 63 }, 64 ), 65 CircularProgressIndicator(value: value), 66 ], 67 mainAxisAlignment: MainAxisAlignment.spaceAround, 68 ), 69 ), 70 ), 71 const Rectangle(Color(0xFFFFFF00)), 72 ], 73 mainAxisAlignment: MainAxisAlignment.spaceBetween, 74 ), 75 ), 76 ), 77 ).attachToRenderTree(owner, element); 78} 79 80Duration timeBase; 81RenderTransform transformBox; 82 83void rotate(Duration timeStamp) { 84 timeBase ??= timeStamp; 85 final double delta = (timeStamp - timeBase).inMicroseconds.toDouble() / Duration.microsecondsPerSecond; // radians 86 87 transformBox.setIdentity(); 88 transformBox.rotateZ(delta); 89 90 owner.buildScope(element); 91} 92 93void main() { 94 final WidgetsBinding binding = WidgetsFlutterBinding.ensureInitialized(); 95 final RenderProxyBox proxy = RenderProxyBox(); 96 attachWidgetTreeToRenderTree(proxy); 97 98 final RenderFlex flexRoot = RenderFlex(direction: Axis.vertical); 99 addFlexChildSolidColor(flexRoot, const Color(0xFFFF00FF), flex: 1); 100 flexRoot.add(proxy); 101 addFlexChildSolidColor(flexRoot, const Color(0xFF0000FF), flex: 1); 102 103 transformBox = RenderTransform(child: flexRoot, transform: Matrix4.identity(), alignment: Alignment.center); 104 final RenderPadding root = RenderPadding(padding: const EdgeInsets.all(80.0), child: transformBox); 105 106 binding.renderView.child = root; 107 binding.addPersistentFrameCallback(rotate); 108} 109