@@ -6,33 +6,89 @@ import 'dart:math' as math;
6
6
7
7
import 'package:flutter/material.dart' ;
8
8
9
- const Color primaryColor = Colors .orange;
10
- const TargetPlatform platform = TargetPlatform .android;
11
-
12
9
void main () {
13
10
runApp (const Sunflower ());
14
11
}
15
12
16
- class SunflowerPainter extends CustomPainter {
17
- static const seedRadius = 2.0 ;
18
- static const scaleFactor = 4 ;
19
- static const tau = math.pi * 2 ;
13
+ class Sunflower extends StatefulWidget {
14
+ const Sunflower ({super .key});
15
+
16
+ @override
17
+ State <StatefulWidget > createState () {
18
+ return _SunflowerState ();
19
+ }
20
+ }
20
21
21
- static final phi = (math.sqrt (5 ) + 1 ) / 2 ;
22
+ class _SunflowerState extends State <Sunflower > {
23
+ double seeds = 100.0 ;
24
+
25
+ int get seedCount => seeds.floor ();
26
+
27
+ @override
28
+ Widget build (BuildContext context) {
29
+ return MaterialApp (
30
+ debugShowCheckedModeBanner: false ,
31
+ home: Scaffold (
32
+ appBar: AppBar (
33
+ title: const Text ('Sunflower' ),
34
+ ),
35
+ body: Column (
36
+ children: [
37
+ Expanded (
38
+ child: LayoutBuilder (builder: (context, constraints) {
39
+ return SizedBox (
40
+ width: constraints.maxWidth,
41
+ height: constraints.maxHeight,
42
+ child: CustomPaint (
43
+ painter: SunflowerPainter (seedCount),
44
+ ),
45
+ );
46
+ }),
47
+ ),
48
+ Text ('Showing $seedCount seeds' ),
49
+ Container (
50
+ constraints: const BoxConstraints .tightFor (width: 300 ),
51
+ padding: const EdgeInsets .only (bottom: 12 ),
52
+ child: Slider (
53
+ min: 20 ,
54
+ max: 2000 ,
55
+ value: seeds,
56
+ onChanged: (newValue) {
57
+ setState (() {
58
+ seeds = newValue;
59
+ });
60
+ },
61
+ ),
62
+ ),
63
+ ],
64
+ ),
65
+ ),
66
+ );
67
+ }
68
+ }
69
+
70
+ class SunflowerPainter extends CustomPainter {
71
+ static const Color primaryColor = Colors .orange;
72
+ static const double seedRadius = 2.0 ;
73
+ static const double tau = math.pi * 2 ;
74
+ static final double phi = (math.sqrt (5 ) + 1 ) / 2 ;
22
75
23
76
final int seeds;
24
77
25
78
SunflowerPainter (this .seeds);
26
79
27
80
@override
28
81
void paint (Canvas canvas, Size size) {
29
- final center = size.width / 2 ;
82
+ final scaleFactor = 4 * size.shortestSide / 400 ;
83
+
84
+ final centerX = size.width / 2 ;
85
+ final centerY = size.height / 2 ;
30
86
31
87
for (var i = 0 ; i < seeds; i++ ) {
32
88
final theta = i * tau / phi;
33
89
final r = math.sqrt (i) * scaleFactor;
34
- final x = center + r * math.cos (theta);
35
- final y = center - r * math.sin (theta);
90
+ final x = centerX + r * math.cos (theta);
91
+ final y = centerY - r * math.sin (theta);
36
92
final offset = Offset (x, y);
37
93
if (! size.contains (offset)) {
38
94
continue ;
@@ -46,105 +102,12 @@ class SunflowerPainter extends CustomPainter {
46
102
return oldDelegate.seeds != seeds;
47
103
}
48
104
49
- // Draw a small circle representing a seed centered at (x,y).
50
105
void drawSeed (Canvas canvas, double x, double y) {
106
+ // Draw a small circle representing a seed centered at (x,y).
51
107
final paint = Paint ()
52
108
..strokeWidth = 2
53
109
..style = PaintingStyle .fill
54
110
..color = primaryColor;
55
111
canvas.drawCircle (Offset (x, y), seedRadius, paint);
56
112
}
57
113
}
58
-
59
- class Sunflower extends StatefulWidget {
60
- const Sunflower ({super .key});
61
-
62
- @override
63
- State <StatefulWidget > createState () {
64
- return _SunflowerState ();
65
- }
66
- }
67
-
68
- class _SunflowerState extends State <Sunflower > {
69
- double seeds = 100.0 ;
70
-
71
- int get seedCount => seeds.floor ();
72
-
73
- @override
74
- Widget build (BuildContext context) {
75
- return MaterialApp (
76
- debugShowCheckedModeBanner: false ,
77
- theme: ThemeData ().copyWith (
78
- platform: platform,
79
- brightness: Brightness .dark,
80
- sliderTheme: SliderThemeData .fromPrimaryColors (
81
- primaryColor: primaryColor,
82
- primaryColorLight: primaryColor,
83
- primaryColorDark: primaryColor,
84
- valueIndicatorTextStyle: const DefaultTextStyle .fallback ().style,
85
- ),
86
- ),
87
- home: Scaffold (
88
- appBar: AppBar (
89
- title: const Text ('Sunflower' ),
90
- ),
91
- drawer: Drawer (
92
- child: ListView (
93
- children: const [
94
- DrawerHeader (
95
- child: Center (
96
- child: Text (
97
- 'Sunflower 🌻' ,
98
- style: TextStyle (fontSize: 32 ),
99
- ),
100
- ),
101
- ),
102
- ],
103
- ),
104
- ),
105
- body: Container (
106
- constraints: const BoxConstraints .expand (),
107
- decoration: BoxDecoration (
108
- border: Border .all (
109
- color: Colors .transparent,
110
- ),
111
- ),
112
- child: Column (
113
- crossAxisAlignment: CrossAxisAlignment .center,
114
- mainAxisAlignment: MainAxisAlignment .start,
115
- children: [
116
- Container (
117
- decoration: BoxDecoration (
118
- border: Border .all (
119
- color: Colors .transparent,
120
- ),
121
- ),
122
- child: SizedBox (
123
- width: 400 ,
124
- height: 400 ,
125
- child: CustomPaint (
126
- painter: SunflowerPainter (seedCount),
127
- ),
128
- ),
129
- ),
130
- Text ('Showing $seedCount seeds' ),
131
- ConstrainedBox (
132
- constraints: const BoxConstraints .tightFor (width: 300 ),
133
- child: Slider .adaptive (
134
- min: 20 ,
135
- max: 2000 ,
136
- value: seeds,
137
- onChanged: (newValue) {
138
- setState (() {
139
- seeds = newValue;
140
- });
141
- },
142
- ),
143
- ),
144
- ],
145
- ),
146
- ),
147
- ),
148
- );
149
- }
150
- }
0 commit comments