Created
February 5, 2019 23:30
-
-
Save mjohnsullivan/286d6bfb586546b5fa52704f1e6ba029 to your computer and use it in GitHub Desktop.
A custom painter that draws a smiley face in Flutter
This file contains hidden or 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
// Copyright 2019 The Chromium 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 'package:flutter/material.dart'; | |
import 'dart:math' as Math; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Smiley Face', | |
home: Scaffold( | |
body: Container( | |
constraints: BoxConstraints.expand(), | |
child: Smiley(), | |
)), | |
); | |
} | |
} | |
class Smiley extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return CustomPaint( | |
painter: SmileyPainter(), | |
); | |
} | |
} | |
class SmileyPainter extends CustomPainter { | |
@override | |
void paint(Canvas canvas, Size size) { | |
final radius = Math.min(size.width, size.height) / 2; | |
final center = Offset(size.width / 2, size.height / 2); | |
// Draw the body | |
final paint = Paint()..color = Colors.yellow; | |
canvas.drawCircle(center, radius, paint); | |
// Draw the mouth | |
final smilePaint = Paint() | |
..style = PaintingStyle.stroke | |
..strokeWidth = 10; | |
canvas.drawArc(Rect.fromCircle(center: center, radius: radius / 2), 0, | |
Math.pi, false, smilePaint); | |
// Draw the eyes | |
canvas.drawCircle( | |
Offset(center.dx - radius / 2, center.dy - radius / 2), 10, Paint()); | |
canvas.drawCircle( | |
Offset(center.dx + radius / 2, center.dy - radius / 2), 10, Paint()); | |
} | |
@override | |
bool shouldRepaint(CustomPainter oldDelegate) => false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment