Skip to content

Instantly share code, notes, and snippets.

View av's full-sized avatar
💻
🌚

Ivan Charapanau av

💻
🌚
View GitHub Profile
@av
av / main.dart
Created October 11, 2019 11:41
Flutter: tough_bikes BikeButton with CustomPaint
class BikeButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
height: 50.0,
child: CustomPaint(
painter: BikeButtonPainter(),
child: Center(
child: Text('Buy Tough Bike'.toUpperCase()),
),
@av
av / main.dart
Created October 11, 2019 11:47
Flutter: tough_bikes BikeButtonPainter
class BikeButtonPainter extends CustomPainter {
final fillPaint = Paint()..color = Colors.grey;
@override
void paint(Canvas canvas, Size size) {
// This [Rect] will fill the whole canvas we got
var fillRect = Rect.fromPoints(Offset.zero, size.bottomRight(Offset.zero));
canvas.drawRect(
fillRect,
@av
av / main.dart
Created October 11, 2019 11:49
Flutter: tough_bikes, button container styling
/// In BikeList.build > Column.children
Container(
decoration: BoxDecoration(
boxShadow: const [
BoxShadow(spreadRadius: 0.0, blurRadius: 20.0),
],
color: Colors.grey.shade800,
),
child: Row(
children: <Widget>[
@av
av / main.dart
Created October 11, 2019 12:00
Flutter: tough_bikes, random offset helpers
// In BikeButtonPainter
// ...
static final rnd = Random();
static Offset randomOffset(Size size) {
return Offset(
rnd.nextDouble() * size.width,
rnd.nextDouble() * size.height
);
}
// ...
@av
av / main.dart
Created October 11, 2019 12:02
Flutter: tough_bikes, line paint
// In BikeButtonPainter
// ...
final linePaint = Paint()..color = Colors.white24;
// ...
@av
av / main.dart
Created October 11, 2019 12:04
Flutter: tough_bikes, draw random lines
@override
void paint(Canvas canvas, Size size) {
// This [Rect] will fill the whole canvas we got
var fillRect = Rect.fromPoints(Offset.zero, size.bottomRight(Offset.zero));
var count = 100;
// Background for our texture
canvas.drawRect(fillRect, fillPaint);
for (var i = 0; i < count; i++) {
@av
av / main.dart
Created October 11, 2019 12:06
Flutter: tough_bikes, more lines
// in BikeButtonPainter.paint
// ...
var count = 1000;
// ...
@av
av / main.dart
Created October 11, 2019 12:09
Flutter: tough_bikes, even more lines
// in BikeButtonPainter.paint
// ...
var count = 10000;
// ...
@av
av / main.dart
Created October 11, 2019 12:39
Flutter: tough_bikes, density
// in BikeButtonPainter
// ...
/// Defines how many lines per unit of area we want
final double density;
/// Expose to outside, allowing to override default value
BikeButtonPainter({
this.density = .1,
});
@av
av / main.dart
Created October 11, 2019 12:47
Flutter: tough_bikes, offsets
var start = Offset(1, 1);
var displacement = Offset(-2, -2);
var end = start + displacement;
print(end); // Offset(-1, -1);