Skip to content

Instantly share code, notes, and snippets.

@mjohnsullivan
Created February 5, 2019 23:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjohnsullivan/286d6bfb586546b5fa52704f1e6ba029 to your computer and use it in GitHub Desktop.
Save mjohnsullivan/286d6bfb586546b5fa52704f1e6ba029 to your computer and use it in GitHub Desktop.
A custom painter that draws a smiley face in Flutter
// 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