Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update the sunflower sample to perform better at various sizes #2836

Merged
merged 6 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 68 additions & 105 deletions pkgs/samples/lib/sunflower.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,89 @@ import 'dart:math' as math;

import 'package:flutter/material.dart';

const Color primaryColor = Colors.orange;
const TargetPlatform platform = TargetPlatform.android;

void main() {
runApp(const Sunflower());
}

class SunflowerPainter extends CustomPainter {
static const seedRadius = 2.0;
static const scaleFactor = 4;
static const tau = math.pi * 2;
class Sunflower extends StatefulWidget {
const Sunflower({super.key});

@override
State<StatefulWidget> createState() {
return _SunflowerState();
}
}

static final phi = (math.sqrt(5) + 1) / 2;
class _SunflowerState extends State<Sunflower> {
double seeds = 100.0;

int get seedCount => seeds.floor();

@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: const Text('Sunflower'),
),
body: Column(
children: [
Expanded(
child: LayoutBuilder(builder: (context, constraints) {
return SizedBox(
width: constraints.maxWidth,
height: constraints.maxHeight,
child: CustomPaint(
painter: SunflowerPainter(seedCount),
),
);
}),
),
Text('Showing $seedCount seeds'),
Container(
constraints: const BoxConstraints.tightFor(width: 300),
padding: const EdgeInsets.only(bottom: 12),
child: Slider(
min: 20,
max: 2000,
value: seeds,
onChanged: (newValue) {
setState(() {
seeds = newValue;
});
},
),
),
],
),
),
);
}
}

class SunflowerPainter extends CustomPainter {
static const Color primaryColor = Colors.orange;
static const double seedRadius = 2.0;
static const double tau = math.pi * 2;
static final double phi = (math.sqrt(5) + 1) / 2;

final int seeds;

SunflowerPainter(this.seeds);

@override
void paint(Canvas canvas, Size size) {
final center = size.width / 2;
final scaleFactor = 4 * size.shortestSide / 400;

final centerX = size.width / 2;
final centerY = size.height / 2;

for (var i = 0; i < seeds; i++) {
final theta = i * tau / phi;
final r = math.sqrt(i) * scaleFactor;
final x = center + r * math.cos(theta);
final y = center - r * math.sin(theta);
final x = centerX + r * math.cos(theta);
final y = centerY - r * math.sin(theta);
final offset = Offset(x, y);
if (!size.contains(offset)) {
continue;
Expand All @@ -46,105 +102,12 @@ class SunflowerPainter extends CustomPainter {
return oldDelegate.seeds != seeds;
}

// Draw a small circle representing a seed centered at (x,y).
void drawSeed(Canvas canvas, double x, double y) {
// Draw a small circle representing a seed centered at (x,y).
final paint = Paint()
..strokeWidth = 2
..style = PaintingStyle.fill
..color = primaryColor;
canvas.drawCircle(Offset(x, y), seedRadius, paint);
}
}

class Sunflower extends StatefulWidget {
const Sunflower({super.key});

@override
State<StatefulWidget> createState() {
return _SunflowerState();
}
}

class _SunflowerState extends State<Sunflower> {
double seeds = 100.0;

int get seedCount => seeds.floor();

@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData().copyWith(
platform: platform,
brightness: Brightness.dark,
sliderTheme: SliderThemeData.fromPrimaryColors(
primaryColor: primaryColor,
primaryColorLight: primaryColor,
primaryColorDark: primaryColor,
valueIndicatorTextStyle: const DefaultTextStyle.fallback().style,
),
),
home: Scaffold(
appBar: AppBar(
title: const Text('Sunflower'),
),
drawer: Drawer(
child: ListView(
children: const [
DrawerHeader(
child: Center(
child: Text(
'Sunflower 🌻',
style: TextStyle(fontSize: 32),
),
),
),
],
),
),
body: Container(
constraints: const BoxConstraints.expand(),
decoration: BoxDecoration(
border: Border.all(
color: Colors.transparent,
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Container(
decoration: BoxDecoration(
border: Border.all(
color: Colors.transparent,
),
),
child: SizedBox(
width: 400,
height: 400,
child: CustomPaint(
painter: SunflowerPainter(seedCount),
),
),
),
Text('Showing $seedCount seeds'),
ConstrainedBox(
constraints: const BoxConstraints.tightFor(width: 300),
child: Slider.adaptive(
min: 20,
max: 2000,
value: seeds,
onChanged: (newValue) {
setState(() {
seeds = newValue;
});
},
),
),
],
),
),
),
);
}
}
Loading