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