spinning_mixed.dart 3.86 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// 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 color, RenderObject version
11
void addFlexChildSolidColor(RenderFlex parent, Color backgroundColor, { int flex = 0 }) {
12
  final RenderSolidColorBox child = RenderSolidColorBox(backgroundColor);
13
  parent.add(child);
14
  final FlexParentData childParentData = child.parentData! as FlexParentData;
15
  childParentData.flex = flex;
16 17
}

18
// Solid color, Widget version
19
class Rectangle extends StatelessWidget {
20
  const Rectangle(this.color, { super.key });
21

22
  final Color color;
23 24

  @override
25
  Widget build(BuildContext context) {
26 27
    return Expanded(
      child: Container(
28
        color: color,
29
      ),
30 31 32 33
    );
  }
}

34 35
double? value;
RenderObjectToWidgetElement<RenderBox>? element;
36
void attachWidgetTreeToRenderTree(RenderProxyBox container) {
37
  element = RenderObjectToWidgetAdapter<RenderBox>(
38
    container: container,
39
    child: Directionality(
Ian Hickson's avatar
Ian Hickson committed
40
      textDirection: TextDirection.ltr,
41
      child: SizedBox(
Ian Hickson's avatar
Ian Hickson committed
42
        height: 300.0,
43
        child: Column(
44
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
Ian Hickson's avatar
Ian Hickson committed
45
          children: <Widget>[
46
            const Rectangle(Color(0xFF00FFFF)),
47 48
            Material(
              child: Container(
Ian Hickson's avatar
Ian Hickson committed
49 50
                padding: const EdgeInsets.all(10.0),
                margin: const EdgeInsets.all(10.0),
51
                child: Row(
52
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
Ian Hickson's avatar
Ian Hickson committed
53
                  children: <Widget>[
54
                    ElevatedButton(
55
                      child: const Row(
Ian Hickson's avatar
Ian Hickson committed
56
                        children: <Widget>[
57 58
                          FlutterLogo(),
                          Text('PRESS ME'),
Ian Hickson's avatar
Ian Hickson committed
59 60 61
                        ],
                      ),
                      onPressed: () {
62
                        value = value == null ? 0.1 : (value! + 0.1) % 1.0;
Ian Hickson's avatar
Ian Hickson committed
63 64
                        attachWidgetTreeToRenderTree(container);
                      },
Hixie's avatar
Hixie committed
65
                    ),
66
                    CircularProgressIndicator(value: value),
Ian Hickson's avatar
Ian Hickson committed
67 68 69 70
                  ],
                ),
              ),
            ),
71
            const Rectangle(Color(0xFFFFFF00)),
Ian Hickson's avatar
Ian Hickson committed
72 73 74 75
          ],
        ),
      ),
    ),
76
  ).attachToRenderTree(WidgetsBinding.instance.buildOwner!, element);
77 78
}

79 80
Duration? timeBase;
late RenderTransform transformBox;
81

82
void rotate(Duration timeStamp) {
83
  timeBase ??= timeStamp;
84
  final double delta = (timeStamp - timeBase!).inMicroseconds.toDouble() / Duration.microsecondsPerSecond; // radians
85 86 87

  transformBox.setIdentity();
  transformBox.rotateZ(delta);
88

89
  WidgetsBinding.instance.buildOwner!.buildScope(element!);
90 91 92
}

void main() {
93
  final WidgetsBinding binding = WidgetsFlutterBinding.ensureInitialized();
94
  final RenderProxyBox proxy = RenderProxyBox();
95
  attachWidgetTreeToRenderTree(proxy);
96

97
  final RenderFlex flexRoot = RenderFlex(direction: Axis.vertical);
98
  addFlexChildSolidColor(flexRoot, const Color(0xFFFF00FF), flex: 1);
99
  flexRoot.add(proxy);
100
  addFlexChildSolidColor(flexRoot, const Color(0xFF0000FF), flex: 1);
101

102 103
  transformBox = RenderTransform(child: flexRoot, transform: Matrix4.identity(), alignment: Alignment.center);
  final RenderPadding root = RenderPadding(padding: const EdgeInsets.all(80.0), child: transformBox);
104

105 106 107 108 109 110 111 112 113 114 115
  // TODO(goderbauer): Create a window if embedder doesn't provide an implicit view to draw into.
  assert(binding.platformDispatcher.implicitView != null);
  final RenderView view = RenderView(
    view: binding.platformDispatcher.implicitView!,
    child: root,
  );
  final PipelineOwner pipelineOwner = PipelineOwner()..rootNode = view;
  binding.rootPipelineOwner.adoptChild(pipelineOwner);
  binding.addRenderView(view);
  view.prepareInitialFrame();

116
  binding.addPersistentFrameCallback(rotate);
117
}