Ian Hickson | 449f4a6 | 2019-11-27 15:04:02 -0800 | [diff] [blame] | 1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
Hixie | bd9036c | 2015-10-21 11:17:03 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Hixie | bd9036c | 2015-10-21 11:17:03 -0700 | [diff] [blame] | 5 | import 'package:flutter/material.dart'; |
| 6 | import 'package:flutter/rendering.dart'; |
Hixie | bd9036c | 2015-10-21 11:17:03 -0700 | [diff] [blame] | 7 | |
Adam Barth | 948ae15 | 2016-02-13 00:52:56 -0800 | [diff] [blame] | 8 | import '../rendering/src/solid_color_box.dart'; |
Hixie | bd9036c | 2015-10-21 11:17:03 -0700 | [diff] [blame] | 9 | |
Chris Bracken | 156b422 | 2019-05-24 19:13:02 -0700 | [diff] [blame] | 10 | // Solid color, RenderObject version |
Alexandre Ardhuin | 09276be | 2018-06-05 08:50:40 +0200 | [diff] [blame] | 11 | void addFlexChildSolidColor(RenderFlex parent, Color backgroundColor, { int flex = 0 }) { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 12 | final RenderSolidColorBox child = RenderSolidColorBox(backgroundColor); |
Hixie | bd9036c | 2015-10-21 11:17:03 -0700 | [diff] [blame] | 13 | parent.add(child); |
Michael Goderbauer | dd2ea7c | 2020-10-22 13:33:07 -0700 | [diff] [blame] | 14 | final FlexParentData childParentData = child.parentData! as FlexParentData; |
Hixie | bd9036c | 2015-10-21 11:17:03 -0700 | [diff] [blame] | 15 | childParentData.flex = flex; |
| 16 | } |
| 17 | |
Chris Bracken | 156b422 | 2019-05-24 19:13:02 -0700 | [diff] [blame] | 18 | // Solid color, Widget version |
Adam Barth | 95fc5ae | 2016-03-12 12:13:16 -0800 | [diff] [blame] | 19 | class Rectangle extends StatelessWidget { |
Michael Goderbauer | 91bcf42 | 2022-03-29 12:53:08 -0700 | [diff] [blame] | 20 | const Rectangle(this.color, { super.key }); |
Hixie | 797e27e | 2016-03-14 13:31:43 -0700 | [diff] [blame] | 21 | |
Hixie | bd9036c | 2015-10-21 11:17:03 -0700 | [diff] [blame] | 22 | final Color color; |
Hixie | 797e27e | 2016-03-14 13:31:43 -0700 | [diff] [blame] | 23 | |
| 24 | @override |
Hixie | bd9036c | 2015-10-21 11:17:03 -0700 | [diff] [blame] | 25 | Widget build(BuildContext context) { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 26 | return Expanded( |
| 27 | child: Container( |
Ian Hickson | 36052e6 | 2017-04-27 14:19:01 -0700 | [diff] [blame] | 28 | color: color, |
Alexandre Ardhuin | 387f885 | 2019-03-01 08:17:55 +0100 | [diff] [blame] | 29 | ), |
Hixie | bd9036c | 2015-10-21 11:17:03 -0700 | [diff] [blame] | 30 | ); |
| 31 | } |
| 32 | } |
| 33 | |
Michael Goderbauer | dd2ea7c | 2020-10-22 13:33:07 -0700 | [diff] [blame] | 34 | double? value; |
| 35 | RenderObjectToWidgetElement<RenderBox>? element; |
Hixie | bd9036c | 2015-10-21 11:17:03 -0700 | [diff] [blame] | 36 | void attachWidgetTreeToRenderTree(RenderProxyBox container) { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 37 | element = RenderObjectToWidgetAdapter<RenderBox>( |
Hixie | bd9036c | 2015-10-21 11:17:03 -0700 | [diff] [blame] | 38 | container: container, |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 39 | child: Directionality( |
Ian Hickson | ca7d2d2 | 2017-09-07 16:57:38 -0700 | [diff] [blame] | 40 | textDirection: TextDirection.ltr, |
Greg Spencer | c6ef4c7 | 2021-03-04 01:29:02 +0000 | [diff] [blame] | 41 | child: SizedBox( |
Ian Hickson | ca7d2d2 | 2017-09-07 16:57:38 -0700 | [diff] [blame] | 42 | height: 300.0, |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 43 | child: Column( |
Alexandre Ardhuin | 157bc92 | 2021-05-13 22:39:04 +0200 | [diff] [blame] | 44 | mainAxisAlignment: MainAxisAlignment.spaceBetween, |
Ian Hickson | ca7d2d2 | 2017-09-07 16:57:38 -0700 | [diff] [blame] | 45 | children: <Widget>[ |
Alexandre Ardhuin | eda03e2 | 2018-08-02 12:02:32 +0200 | [diff] [blame] | 46 | const Rectangle(Color(0xFF00FFFF)), |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 47 | Material( |
| 48 | child: Container( |
Ian Hickson | ca7d2d2 | 2017-09-07 16:57:38 -0700 | [diff] [blame] | 49 | padding: const EdgeInsets.all(10.0), |
| 50 | margin: const EdgeInsets.all(10.0), |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 51 | child: Row( |
Alexandre Ardhuin | 157bc92 | 2021-05-13 22:39:04 +0200 | [diff] [blame] | 52 | mainAxisAlignment: MainAxisAlignment.spaceAround, |
Ian Hickson | ca7d2d2 | 2017-09-07 16:57:38 -0700 | [diff] [blame] | 53 | children: <Widget>[ |
Hans Muller | b2b5f26 | 2020-09-16 08:02:06 -0700 | [diff] [blame] | 54 | ElevatedButton( |
Michael Goderbauer | 6f09064 | 2023-07-17 09:14:08 -0700 | [diff] [blame] | 55 | child: const Row( |
Ian Hickson | ca7d2d2 | 2017-09-07 16:57:38 -0700 | [diff] [blame] | 56 | children: <Widget>[ |
Michael Goderbauer | 6f09064 | 2023-07-17 09:14:08 -0700 | [diff] [blame] | 57 | FlutterLogo(), |
| 58 | Text('PRESS ME'), |
Ian Hickson | ca7d2d2 | 2017-09-07 16:57:38 -0700 | [diff] [blame] | 59 | ], |
| 60 | ), |
| 61 | onPressed: () { |
Michael Goderbauer | dd2ea7c | 2020-10-22 13:33:07 -0700 | [diff] [blame] | 62 | value = value == null ? 0.1 : (value! + 0.1) % 1.0; |
Ian Hickson | ca7d2d2 | 2017-09-07 16:57:38 -0700 | [diff] [blame] | 63 | attachWidgetTreeToRenderTree(container); |
| 64 | }, |
Hixie | a309cea | 2016-01-15 16:38:10 -0800 | [diff] [blame] | 65 | ), |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 66 | CircularProgressIndicator(value: value), |
Ian Hickson | ca7d2d2 | 2017-09-07 16:57:38 -0700 | [diff] [blame] | 67 | ], |
Ian Hickson | ca7d2d2 | 2017-09-07 16:57:38 -0700 | [diff] [blame] | 68 | ), |
| 69 | ), |
| 70 | ), |
Alexandre Ardhuin | eda03e2 | 2018-08-02 12:02:32 +0200 | [diff] [blame] | 71 | const Rectangle(Color(0xFFFFFF00)), |
Ian Hickson | ca7d2d2 | 2017-09-07 16:57:38 -0700 | [diff] [blame] | 72 | ], |
Ian Hickson | ca7d2d2 | 2017-09-07 16:57:38 -0700 | [diff] [blame] | 73 | ), |
| 74 | ), |
| 75 | ), |
Ian Hickson | ab89ce2 | 2022-02-03 14:55:15 -0800 | [diff] [blame] | 76 | ).attachToRenderTree(WidgetsBinding.instance.buildOwner!, element); |
Hixie | bd9036c | 2015-10-21 11:17:03 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Michael Goderbauer | dd2ea7c | 2020-10-22 13:33:07 -0700 | [diff] [blame] | 79 | Duration? timeBase; |
| 80 | late RenderTransform transformBox; |
Hixie | bd9036c | 2015-10-21 11:17:03 -0700 | [diff] [blame] | 81 | |
| 82 | void rotate(Duration timeStamp) { |
Alexandre Ardhuin | 8bcf302 | 2017-04-07 21:41:17 +0200 | [diff] [blame] | 83 | timeBase ??= timeStamp; |
Michael Goderbauer | dd2ea7c | 2020-10-22 13:33:07 -0700 | [diff] [blame] | 84 | final double delta = (timeStamp - timeBase!).inMicroseconds.toDouble() / Duration.microsecondsPerSecond; // radians |
Hixie | bd9036c | 2015-10-21 11:17:03 -0700 | [diff] [blame] | 85 | |
| 86 | transformBox.setIdentity(); |
Hixie | bd9036c | 2015-10-21 11:17:03 -0700 | [diff] [blame] | 87 | transformBox.rotateZ(delta); |
Ian Hickson | e01592a | 2016-09-28 01:06:32 -0700 | [diff] [blame] | 88 | |
Ian Hickson | ab89ce2 | 2022-02-03 14:55:15 -0800 | [diff] [blame] | 89 | WidgetsBinding.instance.buildOwner!.buildScope(element!); |
Hixie | bd9036c | 2015-10-21 11:17:03 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void main() { |
Chris Bracken | 4c8c420 | 2017-03-03 18:04:27 -0800 | [diff] [blame] | 93 | final WidgetsBinding binding = WidgetsFlutterBinding.ensureInitialized(); |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 94 | final RenderProxyBox proxy = RenderProxyBox(); |
Hixie | bd9036c | 2015-10-21 11:17:03 -0700 | [diff] [blame] | 95 | attachWidgetTreeToRenderTree(proxy); |
| 96 | |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 97 | final RenderFlex flexRoot = RenderFlex(direction: Axis.vertical); |
Ian Hickson | a94999b | 2016-02-10 18:11:03 -0800 | [diff] [blame] | 98 | addFlexChildSolidColor(flexRoot, const Color(0xFFFF00FF), flex: 1); |
Hixie | bd9036c | 2015-10-21 11:17:03 -0700 | [diff] [blame] | 99 | flexRoot.add(proxy); |
Ian Hickson | a94999b | 2016-02-10 18:11:03 -0800 | [diff] [blame] | 100 | addFlexChildSolidColor(flexRoot, const Color(0xFF0000FF), flex: 1); |
Hixie | bd9036c | 2015-10-21 11:17:03 -0700 | [diff] [blame] | 101 | |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 102 | transformBox = RenderTransform(child: flexRoot, transform: Matrix4.identity(), alignment: Alignment.center); |
| 103 | final RenderPadding root = RenderPadding(padding: const EdgeInsets.all(80.0), child: transformBox); |
Hixie | bd9036c | 2015-10-21 11:17:03 -0700 | [diff] [blame] | 104 | |
Michael Goderbauer | 6f09064 | 2023-07-17 09:14:08 -0700 | [diff] [blame] | 105 | // TODO(goderbauer): Create a window if embedder doesn't provide an implicit view to draw into. |
| 106 | assert(binding.platformDispatcher.implicitView != null); |
| 107 | final RenderView view = RenderView( |
| 108 | view: binding.platformDispatcher.implicitView!, |
| 109 | child: root, |
| 110 | ); |
| 111 | final PipelineOwner pipelineOwner = PipelineOwner()..rootNode = view; |
| 112 | binding.rootPipelineOwner.adoptChild(pipelineOwner); |
| 113 | binding.addRenderView(view); |
| 114 | view.prepareInitialFrame(); |
| 115 | |
Ian Hickson | 0e11b0e | 2016-04-21 16:06:51 -0700 | [diff] [blame] | 116 | binding.addPersistentFrameCallback(rotate); |
Hixie | bd9036c | 2015-10-21 11:17:03 -0700 | [diff] [blame] | 117 | } |