Skip to content

Instantly share code, notes, and snippets.

@mkiisoft
Forked from tarek360/CurvedShape.dart
Created July 16, 2019 05:35
Show Gist options
  • Select an option

  • Save mkiisoft/7d7b1c5211ea360da0f3a85d736a07f3 to your computer and use it in GitHub Desktop.

Select an option

Save mkiisoft/7d7b1c5211ea360da0f3a85d736a07f3 to your computer and use it in GitHub Desktop.

Revisions

  1. @tarek360 tarek360 revised this gist Jan 2, 2019. 1 changed file with 37 additions and 24 deletions.
    61 changes: 37 additions & 24 deletions CurvedShape.dart
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,8 @@ import 'package:flutter/services.dart';
    import 'dart:math';

    const CURVE_HEIGHT = 160.0;
    const AVATAR_RADIUS = CURVE_HEIGHT * 0.28;
    const AVATAR_DIAMETER = AVATAR_RADIUS * 2;

    void main() => runApp(new MyApp());

    @@ -27,14 +29,29 @@ class MyHomePage extends StatefulWidget {
    }

    class _MyHomePageState extends State<MyHomePage> {

    @override
    Widget build(BuildContext context) {
    return Scaffold(
    body: new Stack(
    children: [
    _buildContent(),
    CurvedShape(),
    Container(
    margin: EdgeInsets.only(top: CURVE_HEIGHT - AVATAR_DIAMETER),
    width: double.infinity,
    height: AVATAR_DIAMETER,
    padding: EdgeInsets.all(8),
    child: Container(
    decoration: new BoxDecoration(
    shape: BoxShape.circle,
    color: Colors.deepOrangeAccent[400],
    ),
    child: new Icon(
    Icons.mood,
    color: Colors.deepOrangeAccent[100],
    size: AVATAR_RADIUS,
    ),
    ))
    ],
    ),
    );
    @@ -56,7 +73,6 @@ class _MyHomePageState extends State<MyHomePage> {
    }

    class CurvedShape extends StatelessWidget {

    @override
    Widget build(BuildContext context) {
    return Container(
    @@ -72,44 +88,41 @@ class CurvedShape extends StatelessWidget {
    class _MyPainter extends CustomPainter {
    @override
    void paint(Canvas canvas, Size size) {

    Paint paint = new Paint()
    ..style = PaintingStyle.fill
    ..isAntiAlias = true
    ..color = Colors.purple[700];

    final avatarRadius = size.height * 0.28;
    Offset center = Offset(size.width / 2, size.height / 2);
    Offset circleCenter = Offset(center.dx, size.height - avatarRadius);

    final arcStartAngle = 200 / 180 * pi;
    final avatarArcStartX = circleCenter.dx + avatarRadius * cos(arcStartAngle);
    final avatarArcStartY = circleCenter.dy + avatarRadius * sin(arcStartAngle);

    final arcEndAngle = -5 / 180 * pi;
    final avatarArcEndX = circleCenter.dx + avatarRadius * cos(arcEndAngle);
    final avatarArcEndY = circleCenter.dy + avatarRadius * sin(arcEndAngle);
    Offset circleCenter = Offset(size.width / 2, size.height - AVATAR_RADIUS);

    Offset topLeft = Offset(0, 0);
    Offset bottomLeft = Offset(0, size.height * 0.25);

    Offset topRight = Offset(size.width, 0);
    Offset bottomRight = Offset(size.width, size.height * 0.5);

    Offset leftCurveControlPoint = Offset(circleCenter.dx * 0.5, size.height - AVATAR_RADIUS * 1.5);
    Offset rightCurveControlPoint = Offset(circleCenter.dx * 1.6, size.height - AVATAR_RADIUS);

    final arcStartAngle = 200 / 180 * pi;
    final avatarLeftPointX = circleCenter.dx + AVATAR_RADIUS * cos(arcStartAngle);
    final avatarLeftPointY = circleCenter.dy + AVATAR_RADIUS * sin(arcStartAngle);
    Offset avatarLeftPoint = Offset(avatarLeftPointX, avatarLeftPointY); // the left point of the arc

    final arcEndAngle = -5 / 180 * pi;
    final avatarRightPointX = circleCenter.dx + AVATAR_RADIUS * cos(arcEndAngle);
    final avatarRightPointY = circleCenter.dy + AVATAR_RADIUS * sin(arcEndAngle);
    Offset avatarRightPoint = Offset(avatarRightPointX, avatarRightPointY); // the right point of the arc

    Path path = Path()
    ..moveTo(topRight.dx, topRight.dy)
    ..lineTo(topLeft.dx, topLeft.dy)
    ..moveTo(topLeft.dx, topLeft.dy) // this move isn't required since the start point is (0,0)
    ..lineTo(bottomLeft.dx, bottomLeft.dy)
    ..quadraticBezierTo(
    circleCenter.dx * 0.5, size.height - avatarRadius * 1.5, avatarArcStartX, avatarArcStartY)
    ..arcToPoint(Offset(avatarArcEndX, avatarArcEndY), radius: Radius.circular(avatarRadius))
    ..quadraticBezierTo(circleCenter.dx * 1.6, size.height - avatarRadius, bottomRight.dx, bottomRight.dy)
    ..quadraticBezierTo(leftCurveControlPoint.dx, leftCurveControlPoint.dy, avatarLeftPoint.dx, avatarLeftPoint.dy)
    ..arcToPoint(avatarRightPoint, radius: Radius.circular(AVATAR_RADIUS))
    ..quadraticBezierTo(rightCurveControlPoint.dx, rightCurveControlPoint.dy, bottomRight.dx, bottomRight.dy)
    ..lineTo(topRight.dx, topRight.dy)
    ..close();

    canvas.drawPath(path, paint);

    paint.color = Colors.deepOrangeAccent;
    canvas.drawCircle(circleCenter, avatarRadius - 8, paint);
    }

    @override
  2. @tarek360 tarek360 created this gist Jan 2, 2019.
    119 changes: 119 additions & 0 deletions CurvedShape.dart
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,119 @@
    import "package:flutter/material.dart";
    import 'package:flutter/services.dart';
    import 'dart:math';

    const CURVE_HEIGHT = 160.0;

    void main() => runApp(new MyApp());

    class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
    SystemChrome.setEnabledSystemUIOverlays([]);
    return new MaterialApp(
    title: 'CurvedShape',
    theme: new ThemeData(primarySwatch: Colors.blue),
    home: new MyHomePage(title: 'CurvedShape'),
    );
    }
    }

    class MyHomePage extends StatefulWidget {
    MyHomePage({Key key, this.title}) : super(key: key);
    final String title;

    @override
    _MyHomePageState createState() => new _MyHomePageState();
    }

    class _MyHomePageState extends State<MyHomePage> {

    @override
    Widget build(BuildContext context) {
    return Scaffold(
    body: new Stack(
    children: [
    _buildContent(),
    CurvedShape(),
    ],
    ),
    );
    }

    Widget _buildContent() {
    return Container(
    child: SingleChildScrollView(
    padding: EdgeInsets.fromLTRB(16, CURVE_HEIGHT, 16, 16),
    child: Text(
    "There are many variations of passages of Lorem Ipsum available, but the majority have suffered "
    "alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc."
    "There are many variations of passages of Lorem Ipsum available, but the majority have suffered "
    "alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.",
    style: TextStyle(fontSize: 18),
    )),
    );
    }
    }

    class CurvedShape extends StatelessWidget {

    @override
    Widget build(BuildContext context) {
    return Container(
    width: double.infinity,
    height: CURVE_HEIGHT,
    child: CustomPaint(
    painter: _MyPainter(),
    ),
    );
    }
    }

    class _MyPainter extends CustomPainter {
    @override
    void paint(Canvas canvas, Size size) {

    Paint paint = new Paint()
    ..style = PaintingStyle.fill
    ..isAntiAlias = true
    ..color = Colors.purple[700];

    final avatarRadius = size.height * 0.28;
    Offset center = Offset(size.width / 2, size.height / 2);
    Offset circleCenter = Offset(center.dx, size.height - avatarRadius);

    final arcStartAngle = 200 / 180 * pi;
    final avatarArcStartX = circleCenter.dx + avatarRadius * cos(arcStartAngle);
    final avatarArcStartY = circleCenter.dy + avatarRadius * sin(arcStartAngle);

    final arcEndAngle = -5 / 180 * pi;
    final avatarArcEndX = circleCenter.dx + avatarRadius * cos(arcEndAngle);
    final avatarArcEndY = circleCenter.dy + avatarRadius * sin(arcEndAngle);

    Offset topLeft = Offset(0, 0);
    Offset bottomLeft = Offset(0, size.height * 0.25);

    Offset topRight = Offset(size.width, 0);
    Offset bottomRight = Offset(size.width, size.height * 0.5);

    Path path = Path()
    ..moveTo(topRight.dx, topRight.dy)
    ..lineTo(topLeft.dx, topLeft.dy)
    ..lineTo(bottomLeft.dx, bottomLeft.dy)
    ..quadraticBezierTo(
    circleCenter.dx * 0.5, size.height - avatarRadius * 1.5, avatarArcStartX, avatarArcStartY)
    ..arcToPoint(Offset(avatarArcEndX, avatarArcEndY), radius: Radius.circular(avatarRadius))
    ..quadraticBezierTo(circleCenter.dx * 1.6, size.height - avatarRadius, bottomRight.dx, bottomRight.dy)
    ..close();

    canvas.drawPath(path, paint);

    paint.color = Colors.deepOrangeAccent;
    canvas.drawCircle(circleCenter, avatarRadius - 8, paint);
    }

    @override
    bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
    }
    }