blob: 6d3223c7b0065bdbd2a557a70a1ab7e06ace4321 [file] [log] [blame]
Ian Hickson449f4a62019-11-27 15:04:02 -08001// Copyright 2014 The Flutter Authors. All rights reserved.
Hixiebd9036c2015-10-21 11:17:03 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Hixiebd9036c2015-10-21 11:17:03 -07005import 'package:flutter/material.dart';
6import 'package:flutter/rendering.dart';
Hixiebd9036c2015-10-21 11:17:03 -07007
Adam Barth948ae152016-02-13 00:52:56 -08008import '../rendering/src/solid_color_box.dart';
Hixiebd9036c2015-10-21 11:17:03 -07009
Chris Bracken156b4222019-05-24 19:13:02 -070010// Solid color, RenderObject version
Alexandre Ardhuin09276be2018-06-05 08:50:40 +020011void addFlexChildSolidColor(RenderFlex parent, Color backgroundColor, { int flex = 0 }) {
Alexandre Ardhuind927c932018-09-12 08:29:29 +020012 final RenderSolidColorBox child = RenderSolidColorBox(backgroundColor);
Hixiebd9036c2015-10-21 11:17:03 -070013 parent.add(child);
Michael Goderbauerdd2ea7c2020-10-22 13:33:07 -070014 final FlexParentData childParentData = child.parentData! as FlexParentData;
Hixiebd9036c2015-10-21 11:17:03 -070015 childParentData.flex = flex;
16}
17
Chris Bracken156b4222019-05-24 19:13:02 -070018// Solid color, Widget version
Adam Barth95fc5ae2016-03-12 12:13:16 -080019class Rectangle extends StatelessWidget {
Michael Goderbauer91bcf422022-03-29 12:53:08 -070020 const Rectangle(this.color, { super.key });
Hixie797e27e2016-03-14 13:31:43 -070021
Hixiebd9036c2015-10-21 11:17:03 -070022 final Color color;
Hixie797e27e2016-03-14 13:31:43 -070023
24 @override
Hixiebd9036c2015-10-21 11:17:03 -070025 Widget build(BuildContext context) {
Alexandre Ardhuind927c932018-09-12 08:29:29 +020026 return Expanded(
27 child: Container(
Ian Hickson36052e62017-04-27 14:19:01 -070028 color: color,
Alexandre Ardhuin387f8852019-03-01 08:17:55 +010029 ),
Hixiebd9036c2015-10-21 11:17:03 -070030 );
31 }
32}
33
Michael Goderbauerdd2ea7c2020-10-22 13:33:07 -070034double? value;
35RenderObjectToWidgetElement<RenderBox>? element;
Hixiebd9036c2015-10-21 11:17:03 -070036void attachWidgetTreeToRenderTree(RenderProxyBox container) {
Alexandre Ardhuind927c932018-09-12 08:29:29 +020037 element = RenderObjectToWidgetAdapter<RenderBox>(
Hixiebd9036c2015-10-21 11:17:03 -070038 container: container,
Alexandre Ardhuind927c932018-09-12 08:29:29 +020039 child: Directionality(
Ian Hicksonca7d2d22017-09-07 16:57:38 -070040 textDirection: TextDirection.ltr,
Greg Spencerc6ef4c72021-03-04 01:29:02 +000041 child: SizedBox(
Ian Hicksonca7d2d22017-09-07 16:57:38 -070042 height: 300.0,
Alexandre Ardhuind927c932018-09-12 08:29:29 +020043 child: Column(
Alexandre Ardhuin157bc922021-05-13 22:39:04 +020044 mainAxisAlignment: MainAxisAlignment.spaceBetween,
Ian Hicksonca7d2d22017-09-07 16:57:38 -070045 children: <Widget>[
Alexandre Ardhuineda03e22018-08-02 12:02:32 +020046 const Rectangle(Color(0xFF00FFFF)),
Alexandre Ardhuind927c932018-09-12 08:29:29 +020047 Material(
48 child: Container(
Ian Hicksonca7d2d22017-09-07 16:57:38 -070049 padding: const EdgeInsets.all(10.0),
50 margin: const EdgeInsets.all(10.0),
Alexandre Ardhuind927c932018-09-12 08:29:29 +020051 child: Row(
Alexandre Ardhuin157bc922021-05-13 22:39:04 +020052 mainAxisAlignment: MainAxisAlignment.spaceAround,
Ian Hicksonca7d2d22017-09-07 16:57:38 -070053 children: <Widget>[
Hans Mullerb2b5f262020-09-16 08:02:06 -070054 ElevatedButton(
Michael Goderbauer6f090642023-07-17 09:14:08 -070055 child: const Row(
Ian Hicksonca7d2d22017-09-07 16:57:38 -070056 children: <Widget>[
Michael Goderbauer6f090642023-07-17 09:14:08 -070057 FlutterLogo(),
58 Text('PRESS ME'),
Ian Hicksonca7d2d22017-09-07 16:57:38 -070059 ],
60 ),
61 onPressed: () {
Michael Goderbauerdd2ea7c2020-10-22 13:33:07 -070062 value = value == null ? 0.1 : (value! + 0.1) % 1.0;
Ian Hicksonca7d2d22017-09-07 16:57:38 -070063 attachWidgetTreeToRenderTree(container);
64 },
Hixiea309cea2016-01-15 16:38:10 -080065 ),
Alexandre Ardhuind927c932018-09-12 08:29:29 +020066 CircularProgressIndicator(value: value),
Ian Hicksonca7d2d22017-09-07 16:57:38 -070067 ],
Ian Hicksonca7d2d22017-09-07 16:57:38 -070068 ),
69 ),
70 ),
Alexandre Ardhuineda03e22018-08-02 12:02:32 +020071 const Rectangle(Color(0xFFFFFF00)),
Ian Hicksonca7d2d22017-09-07 16:57:38 -070072 ],
Ian Hicksonca7d2d22017-09-07 16:57:38 -070073 ),
74 ),
75 ),
Ian Hicksonab89ce22022-02-03 14:55:15 -080076 ).attachToRenderTree(WidgetsBinding.instance.buildOwner!, element);
Hixiebd9036c2015-10-21 11:17:03 -070077}
78
Michael Goderbauerdd2ea7c2020-10-22 13:33:07 -070079Duration? timeBase;
80late RenderTransform transformBox;
Hixiebd9036c2015-10-21 11:17:03 -070081
82void rotate(Duration timeStamp) {
Alexandre Ardhuin8bcf3022017-04-07 21:41:17 +020083 timeBase ??= timeStamp;
Michael Goderbauerdd2ea7c2020-10-22 13:33:07 -070084 final double delta = (timeStamp - timeBase!).inMicroseconds.toDouble() / Duration.microsecondsPerSecond; // radians
Hixiebd9036c2015-10-21 11:17:03 -070085
86 transformBox.setIdentity();
Hixiebd9036c2015-10-21 11:17:03 -070087 transformBox.rotateZ(delta);
Ian Hicksone01592a2016-09-28 01:06:32 -070088
Ian Hicksonab89ce22022-02-03 14:55:15 -080089 WidgetsBinding.instance.buildOwner!.buildScope(element!);
Hixiebd9036c2015-10-21 11:17:03 -070090}
91
92void main() {
Chris Bracken4c8c4202017-03-03 18:04:27 -080093 final WidgetsBinding binding = WidgetsFlutterBinding.ensureInitialized();
Alexandre Ardhuind927c932018-09-12 08:29:29 +020094 final RenderProxyBox proxy = RenderProxyBox();
Hixiebd9036c2015-10-21 11:17:03 -070095 attachWidgetTreeToRenderTree(proxy);
96
Alexandre Ardhuind927c932018-09-12 08:29:29 +020097 final RenderFlex flexRoot = RenderFlex(direction: Axis.vertical);
Ian Hicksona94999b2016-02-10 18:11:03 -080098 addFlexChildSolidColor(flexRoot, const Color(0xFFFF00FF), flex: 1);
Hixiebd9036c2015-10-21 11:17:03 -070099 flexRoot.add(proxy);
Ian Hicksona94999b2016-02-10 18:11:03 -0800100 addFlexChildSolidColor(flexRoot, const Color(0xFF0000FF), flex: 1);
Hixiebd9036c2015-10-21 11:17:03 -0700101
Alexandre Ardhuind927c932018-09-12 08:29:29 +0200102 transformBox = RenderTransform(child: flexRoot, transform: Matrix4.identity(), alignment: Alignment.center);
103 final RenderPadding root = RenderPadding(padding: const EdgeInsets.all(80.0), child: transformBox);
Hixiebd9036c2015-10-21 11:17:03 -0700104
Michael Goderbauer6f090642023-07-17 09:14:08 -0700105 // 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 Hickson0e11b0e2016-04-21 16:06:51 -0700116 binding.addPersistentFrameCallback(rotate);
Hixiebd9036c2015-10-21 11:17:03 -0700117}