OLD | NEW |
| (Empty) |
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 | |
5 import 'dart:math' as math; | |
6 | |
7 import 'package:sky/rendering/box.dart'; | |
8 import 'package:sky/rendering/flex.dart'; | |
9 import 'package:sky/rendering/sky_binding.dart'; | |
10 import 'package:sky/theme/colors.dart' as colors; | |
11 import 'package:sky/theme/edges.dart'; | |
12 import 'package:sky/theme/theme_data.dart'; | |
13 import 'package:sky/theme/typography.dart' as typography; | |
14 import 'package:sky/widgets/basic.dart'; | |
15 import 'package:sky/widgets/material.dart'; | |
16 import 'package:sky/widgets/raised_button.dart'; | |
17 import 'package:sky/widgets/scaffold.dart'; | |
18 import 'package:sky/widgets/theme.dart'; | |
19 import 'package:sky/widgets/tool_bar.dart'; | |
20 import 'package:sky/widgets/widget.dart'; | |
21 | |
22 import '../rendering/sector_layout.dart'; | |
23 | |
24 RenderBox initCircle() { | |
25 return new RenderBoxToRenderSectorAdapter( | |
26 innerRadius: 25.0, | |
27 child: new RenderSectorRing(padding: 0.0) | |
28 ); | |
29 } | |
30 | |
31 class SectorApp extends App { | |
32 | |
33 RenderBoxToRenderSectorAdapter sectors = initCircle(); | |
34 math.Random rand = new math.Random(1); | |
35 | |
36 void addSector() { | |
37 double deltaTheta; | |
38 var ring = (sectors.child as RenderSectorRing); | |
39 SectorDimensions currentSize = ring.getIntrinsicDimensions(const SectorConst
raints(), ring.deltaRadius); | |
40 if (currentSize.deltaTheta >= kTwoPi - (math.PI * 0.2 + 0.05)) | |
41 deltaTheta = kTwoPi - currentSize.deltaTheta; | |
42 else | |
43 deltaTheta = math.PI * rand.nextDouble() / 5.0 + 0.05; | |
44 Color color = new Color(((0xFF << 24) + rand.nextInt(0xFFFFFF)) | 0x808080); | |
45 ring.add(new RenderSolidColor(color, desiredDeltaTheta: deltaTheta)); | |
46 updateEnabledState(); | |
47 } | |
48 | |
49 void removeSector() { | |
50 (sectors.child as RenderSectorRing).remove((sectors.child as RenderSectorRin
g).lastChild); | |
51 updateEnabledState(); | |
52 } | |
53 | |
54 static RenderBox initSector(Color color) { | |
55 RenderSectorRing ring = new RenderSectorRing(padding: 1.0); | |
56 ring.add(new RenderSolidColor(const Color(0xFF909090), desiredDeltaTheta: kT
woPi * 0.15)); | |
57 ring.add(new RenderSolidColor(const Color(0xFF909090), desiredDeltaTheta: kT
woPi * 0.15)); | |
58 ring.add(new RenderSolidColor(color, desiredDeltaTheta: kTwoPi * 0.2)); | |
59 return new RenderBoxToRenderSectorAdapter( | |
60 innerRadius: 5.0, | |
61 child: ring | |
62 ); | |
63 } | |
64 RenderBoxToRenderSectorAdapter sectorAddIcon = initSector(const Color(0xFF00DD
00)); | |
65 RenderBoxToRenderSectorAdapter sectorRemoveIcon = initSector(const Color(0xFFD
D0000)); | |
66 | |
67 bool enabledAdd = true; | |
68 bool enabledRemove = false; | |
69 void updateEnabledState() { | |
70 setState(() { | |
71 var ring = (sectors.child as RenderSectorRing); | |
72 SectorDimensions currentSize = ring.getIntrinsicDimensions(const SectorCon
straints(), ring.deltaRadius); | |
73 enabledAdd = currentSize.deltaTheta < kTwoPi; | |
74 enabledRemove = ring.firstChild != null; | |
75 }); | |
76 } | |
77 | |
78 Widget build() { | |
79 return new Theme( | |
80 data: new ThemeData.light(primary: colors.Blue, darkToolbar: true), | |
81 child: new Scaffold( | |
82 toolbar: new ToolBar( | |
83 center: new Text('Sector Layout in a Widget Tree')), | |
84 body: new Material( | |
85 edge: MaterialEdge.canvas, | |
86 child: new Flex([ | |
87 new Container( | |
88 padding: new EdgeDims.symmetric(horizontal: 8.0, vertical: 25.0)
, | |
89 child: new Flex([ | |
90 new RaisedButton( | |
91 enabled: enabledAdd, | |
92 child: new ShrinkWrapWidth( | |
93 child: new Flex([ | |
94 new Container( | |
95 padding: new EdgeDims.all(4.0), | |
96 margin: new EdgeDims.only(right: 10.0), | |
97 child: new WidgetToRenderBoxAdapter(sectorAddIcon) | |
98 ), | |
99 new Text('ADD SECTOR'), | |
100 ]) | |
101 ), | |
102 onPressed: addSector | |
103 ), | |
104 new RaisedButton( | |
105 enabled: enabledRemove, | |
106 child: new ShrinkWrapWidth( | |
107 child: new Flex([ | |
108 new Container( | |
109 padding: new EdgeDims.all(4.0), | |
110 margin: new EdgeDims.only(right: 10.0), | |
111 child: new WidgetToRenderBoxAdapter(sectorRemoveIcon
) | |
112 ), | |
113 new Text('REMOVE SECTOR'), | |
114 ]) | |
115 ), | |
116 onPressed: removeSector | |
117 ) | |
118 ], | |
119 justifyContent: FlexJustifyContent.spaceAround | |
120 ) | |
121 ), | |
122 new Flexible( | |
123 child: new Container( | |
124 margin: new EdgeDims.all(8.0), | |
125 decoration: new BoxDecoration( | |
126 border: new Border.all(new BorderSide(color: new Color(0xFF0
00000))) | |
127 ), | |
128 padding: new EdgeDims.all(8.0), | |
129 child: new WidgetToRenderBoxAdapter(sectors) | |
130 ) | |
131 ), | |
132 ], | |
133 direction: FlexDirection.vertical, | |
134 justifyContent: FlexJustifyContent.spaceBetween | |
135 ) | |
136 ) | |
137 ) | |
138 ); | |
139 } | |
140 } | |
141 | |
142 void main() { | |
143 runApp(new SectorApp()); | |
144 SkyBinding.instance.onFrame = () { | |
145 // uncomment this for debugging: | |
146 // SkyBinding.instance.debugDumpRenderTree(); | |
147 }; | |
148 } | |
OLD | NEW |