Skip to content

Instantly share code, notes, and snippets.

@flutterdevrelgists
Last active February 12, 2024 19:07
  • Star 32 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save flutterdevrelgists/5c0e154dd50af4a9ac856908061291bc to your computer and use it in GitHub Desktop.
Sunflower example
// Copyright 2019 the Dart project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file.
import 'dart:math' as math;
import 'package:flutter/material.dart';
const Color primaryColor = Colors.orange;
const TargetPlatform platform = TargetPlatform.android;
void main() {
runApp(Sunflower());
}
class SunflowerPainter extends CustomPainter {
static const seedRadius = 2.0;
static const scaleFactor = 4;
static const tau = math.pi * 2;
static final phi = (math.sqrt(5) + 1) / 2;
final int seeds;
SunflowerPainter(this.seeds);
@override
void paint(Canvas canvas, Size size) {
final center = size.width / 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 offset = Offset(x, y);
if (!size.contains(offset)) {
continue;
}
drawSeed(canvas, x, y);
}
}
@override
bool shouldRepaint(SunflowerPainter oldDelegate) {
return oldDelegate.seeds != seeds;
}
// Draw a small circle representing a seed centered at (x,y).
void drawSeed(Canvas canvas, double x, double y) {
final paint = Paint()
..strokeWidth = 2
..style = PaintingStyle.fill
..color = primaryColor;
canvas.drawCircle(Offset(x, y), seedRadius, paint);
}
}
class Sunflower extends StatefulWidget {
@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;
});
},
),
),
],
),
),
),
);
}
}
@VictoriaLev
Copy link

mmmmmmmmmmmmmmmmmmmmmmmmmm liked

@zihad18
Copy link

zihad18 commented Feb 12, 2024

j

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment