Skip to content

Commit 2378afc

Browse files
authored
update the sunflower sample to perform better at various sizes (#2836)
* update the sunflower sample * improve auto-sizing of the sample * review feedback * update the slider * remove the drawer from sunflower
1 parent 7a922ef commit 2378afc

File tree

2 files changed

+136
-210
lines changed

2 files changed

+136
-210
lines changed

pkgs/samples/lib/sunflower.dart

Lines changed: 68 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,89 @@ import 'dart:math' as math;
66

77
import 'package:flutter/material.dart';
88

9-
const Color primaryColor = Colors.orange;
10-
const TargetPlatform platform = TargetPlatform.android;
11-
129
void main() {
1310
runApp(const Sunflower());
1411
}
1512

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+
}
2021

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;
2275

2376
final int seeds;
2477

2578
SunflowerPainter(this.seeds);
2679

2780
@override
2881
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;
3086

3187
for (var i = 0; i < seeds; i++) {
3288
final theta = i * tau / phi;
3389
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);
3692
final offset = Offset(x, y);
3793
if (!size.contains(offset)) {
3894
continue;
@@ -46,105 +102,12 @@ class SunflowerPainter extends CustomPainter {
46102
return oldDelegate.seeds != seeds;
47103
}
48104

49-
// Draw a small circle representing a seed centered at (x,y).
50105
void drawSeed(Canvas canvas, double x, double y) {
106+
// Draw a small circle representing a seed centered at (x,y).
51107
final paint = Paint()
52108
..strokeWidth = 2
53109
..style = PaintingStyle.fill
54110
..color = primaryColor;
55111
canvas.drawCircle(Offset(x, y), seedRadius, paint);
56112
}
57113
}
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

Comments
 (0)