DEV Community

loading...
Cover image for Quickly create complex bezier paths in Flutter using SVGs

Quickly create complex bezier paths in Flutter using SVGs

flutterclutter profile image flutter-clutter Originally published at flutterclutter.dev ・5 min read

Especially when using bezier curves, it quickly becomes cumbersome to work with a Path object in Flutter and iteratively adapt the values. In this tutorial, we will find a way to quickly produce beautiful and complex paths with no code editing.

The issue

The challenge is: we are dealing with a visual problem but we are trying to solve it in the code. Having no preview while editing values in our bezier curves makes it a very fiddly task.

The goal

Let's learn an approach that fulfills the following requirements:

  • The initial creation is very easy because there is visual output
  • Everything can be done very quickly within a matter of seconds or minutes
  • Afterwards adjustments are easily possible

The example

Bezier paths in Flutter, example

To show a practical usage of the tutorial we will follow an example in which we produce two decorative paths, one at the top and one at the bottom to make our screen more appealing.

Visual creation

If you just want to see how it works and skip the step of creating an SVG just head over to the converter and hit the example buttons. Same applies if you already have an SVG.

Let's start by creating our SVG file. In this tutorial I will use Inkscape, which is an open source vector graphics editor. You're free to use anything else that fits the purpose, e. g. Adobe Illustrator or Sketch. The procedure is very similar.

Let's start by creating our SVG file. In this tutorial I will use Inkscape, which is an open source vector graphics editor. You're free to use anything else that fits the purpose, e. g. Adobe Illustrator or Sketch. The procedure is very similar.

Inkscape bezier tool

From the toolbox of your vector software choose the bezier tool. We use this tool because we want to have custom shapes with curves.
The bezier tool allows us to set the points of our path. When holding the left mouse button, we can intuitively choose the degree of the curve (implicity setting control points of the bezier curve).

Now we try to create the red shape, starting on the upper left. It is important to know that it does not have to be perfect right away. We can adjust everything later. Trying to make it perfect just wastes time. So we make a rough estimation of our shape.
After that we do the same for our orange shape. It does not really matter where we place it. What is important, though: the distance from the path to the (imaginary) left border. This distance (relative to the width of our shape) will be the actual used distance in the flutter Path later on. So if you want the path to end at 50 % horizontally in your screen later, you should let it end there in Inkscape as well.
We also take care that both of our paths are closed, meaning that we set the last point of our path by clicking on the initial point.

Now that we have rough estimations of our shapes, we make adjustments by using the node tool. This allows us to reposition points of our bezier paths. If we want to automatically smoothen the paths, we can use Path > Simplify or CRTL + L on Windows or CMD + L on the Mac.
Also, we give the shapes the color we want to have by selecting the shape and clicking on the fill on the bottom left.

Afterwards adjustments are easily possible

An important note: the converter takes every path within the SVG file and takes the boundaries of the collection as the boundaries for creating the Flutter path. That means: if we wanted e. g. a padding of the red path to the left border of our canvas which resulted in a padding in our app screen later on, we would need to add a rectangle around everything, starting at (0,0) and move the red path further right.

Using the converter

Before we save the file, we make sure that everything is positioned at (0,0) of the canvas. Then we head over to the SVG to Flutter path converter.
We click the Upload SVG button and choose the file from our harddisk we have just created.

The code output of the converter

Insert the code into Flutter

We create a new widget and put the paths we got from the converter in two different CustomPainter widgets. If we had created two separate SVG files for every path, we would have gotten the correct output right away. But the creation in Inkscape would have taken a little longer. That's why we went for the single-SVG-file-approach.

import 'package:flutter/material.dart';

class Test extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        Align(
          alignment: Alignment.topLeft,
          child: CustomPaint(
            painter: HeaderPainter(),
            child: SizedBox(
                width: MediaQuery.of(context).size.width,
                height: 300
            )
          )
        ),
        Align(
          alignment: Alignment.bottomLeft,
          child: CustomPaint(
            painter: FooterPainter(),
            child: SizedBox(
                width: MediaQuery.of(context).size.width,
                height: 300
            )
          )
        ),
      ],
    );
  }

}

class HeaderPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint();
    Path path = Path();

    // Path number 1

    paint.color = Color(0xFFFF5252);
    path = Path();
    path.lineTo(0, size.height);
    path.cubicTo(size.width * 0.09, size.height * 0.93, size.width * 0.11, size.height * 0.78,size.width * 0.11, size.height * 0.66);
    path.cubicTo(size.width * 0.11, size.height * 0.49, size.width * 0.16, size.height * 0.37,size.width / 4, size.height * 0.28);
    path.cubicTo(size.width * 0.36, size.height * 0.23, size.width * 0.54, size.height * 0.18,size.width * 0.68, size.height * 0.16);
    path.cubicTo(size.width * 0.81, size.height * 0.13, size.width * 0.89, size.height * 0.07,size.width * 0.98, 0);
    path.cubicTo(size.width * 0.94, 0, size.width * 0.86, 0,size.width * 0.84, 0);
    path.cubicTo(size.width * 0.56, 0, size.width * 0.28, 0,0, 0);
    path.cubicTo(0, 0, 0, size.height,0, size.height);
    canvas.drawPath(path, paint);
  }
  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}

class FooterPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint();
    Path path = Path();

    paint.color = Color(0xFFFFAB40).withOpacity(1);
    path = Path();
    path.lineTo(size.width, size.height / 5);
    path.cubicTo(size.width, size.height / 5, size.width * 0.94, size.height * 0.88,size.width * 0.65, size.height * 0.93);
    path.cubicTo(size.width * 0.36, size.height * 0.97, size.width / 5, size.height,size.width / 5, size.height);
    path.cubicTo(size.width / 5, size.height, size.width, size.height,size.width, size.height);
    path.cubicTo(size.width, size.height, size.width, size.height / 5,size.width, size.height / 5);
    canvas.drawPath(path, paint);
  }
  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}

We insert the paths into the widget tree by using a CustomPaint to let it draw and an Align widget to position it at the top and the bottom.

The resulting screen

Empty screen with our paths

The resulting screen brought to life

The screen brought to life

Final thoughts

With an idea in mind and a program that is able to create SVG files, we can easily let our imagination become (virtual) reality within just a minute. By uploading the SVG file into the SVG to Flutter path converter we get the Dart code directly which we can insert in our code and instantly see the result. A comfortable alternative to creating curves manually in the code.

Discussion (0)

pic
Editor guide