Created
January 23, 2020 02:04
-
-
Save zmtzawqlp/753d8f0624e312c92232f43d68542b0b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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