Skip to content

Instantly share code, notes, and snippets.

@zmtzawqlp
Created January 23, 2020 02:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zmtzawqlp/753d8f0624e312c92232f43d68542b0b to your computer and use it in GitHub Desktop.
Save zmtzawqlp/753d8f0624e312c92232f43d68542b0b to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) => MaterialApp(
home: MyHomePage(),
);
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) => Scaffold(
body: CustomPaint(
painter: AspectRatioPainter(
isSelected: true, aspectRatio: 1.0, aspectRatioS: '1:1'),
),
);
}
class AspectRatioPainter extends CustomPainter {
final String aspectRatioS;
final double aspectRatio;
final bool isSelected;
AspectRatioPainter(
{this.aspectRatioS, this.aspectRatio, this.isSelected: false});
@override
void paint(Canvas canvas, Size size) {
final Color color = isSelected ? Colors.blue : Colors.grey;
var rect = (Offset.zero & size);
print(rect);
canvas.drawRect(
rect,
Paint()
..color = color
..style = PaintingStyle.stroke);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
var oldOne = oldDelegate as AspectRatioPainter;
return oldOne.isSelected != isSelected ||
oldOne.aspectRatioS != aspectRatioS ||
oldOne.aspectRatio != aspectRatio;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment