// 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;
}
A custom painter that draws a smiley face in Flutter
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.