Embarking on a Flutter CustomPaint adventure: Part 1; Unveiling the Canvas with Custom Paint Basics 🎨
Tags: flutter, custompaint, uidesign, mobile
Published: 2024-01-22 23:15:34


![Animated X logo using CustomPaint](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mzgzbg1v2raktheeom1l.gif)

I bet you've seen really beautiful designs or even animations on a Flutter application and wondered 'how do they create these things?' 🤔.

You could also be in the category of developers who actually know what those designs are done with, but, find it difficult to grasp 😔.

Or, you could be those who have no idea what I am talking about 👀.

But, not to worry, the Painter 🧑‍🎨 is here to the rescueeee!! 🚀🚀.

Before we delve in, let's establish some ground basics and understanding.

<u>**WHAT IS CUSTOM PAINT IN FLUTTER?**</u>

**[CustomPaint](https://api.flutter.dev/flutter/widgets/CustomPaint-class.html)** is really just an widget in Flutter. But, a painter cannot leave his upcoming apprentice more confused 🤣.

**CustomPaint** is a versatile widget that empowers developers to create custom graphics and perform custom painting on the screen. It acts as a canvas, allowing you to draw shapes, paths, and images, providing full control over the visual elements within your application. With CustomPaint, you can unleash your creativity, building unique and tailored visual experiences for your **[Flutter](https://flutter.dev)** applications. (You guessed right, ChatGPT helped me here 😹).

<u>**WHAT'S NEXT 🤔?**</u>

We've already established a clearer definition of what CustomPaint is all about. I won't bore you with all the theories here. In later articles, I'll slot them in bit by bit, so, you'll be well versed in both theory and practical, just like a real-painter should 💪.

I'll be showing us a very simple paint (the boring stuff) and slowly, we'll advance our paint and algorithm (the exciting stuff). For each episode of our article on CustomPaint, I'll introduce new and interesting concept. You just have to hold my hands tightly, while I take you on a journey like you never experienced before 🌚.

<u>**HURRAY, LET'S PAINT!!! 🎨**</u>

As I promised, I won't bore you and I'll quickly get down to business.

Firstly, we'll create our project

![Create custom paint project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/52k8a24emit8od2kmvqx.png)

We should be all be familiar with this, I only did it so, everyone can follow through with the tutorial.

Next:

![Open project from terminal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1khybqccbezi35l7j90e.png)

Open the project in your favourite [IDE](https://docs.flutter.dev/get-started/editor). If you do it exactly as I did and you have VSCode installed, it will launch the project on [VSCode](https://code.visualstudio.com/). You may be wondering why I had to open it from the terminal; short answer: Makes me look more like a BADASS developer 😎.

```
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: false,
      ),
      home: const CustomPaintApp(),
    );
  }
}

class CustomPaintApp extends StatelessWidget {
  const CustomPaintApp({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Custom Paint'),
      ),
    );
  }
}
```

So, far so good, we have a clean slate (Canvas?) to start working on.

<u>**OUR FIRST PAINT**</u>

We already established earlier that, the entirety of our discussion is the CustomPaint widget. So, we'll be using the widget for anything paint, in future episodes, I'll explain more about it, for now, let's just paint 😤.


![Custom Paint from flutter team](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f2clj7l9f5tzsns8k6f5.png)

This screen grab is from the official flutter docs and if you read it carefully, you'll understand that, we must use a [CustomPaint](https://api.flutter.dev/flutter/widgets/CustomPaint-class.html) (widget) and a [CustomPainter](https://api.flutter.dev/flutter/rendering/CustomPainter-class.html) (an [abstract](https://dart.dev/language/class-modifiers#:~:text=or%20mixin%20class.-,abstract,-To%20define%20a) class that we will always extend) for any Paint we need.

```
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: false,
      ),
      home: const CustomPaintApp(),
    );
  }
}

class CustomPaintApp extends StatelessWidget {
  const CustomPaintApp({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Custom Paint'),
      ),
      body: Center(
        child: CustomPaint(
          painter: SquarePainter(),
        ),
      ),
    );
  }
}

class SquarePainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    var paint = Paint()
      ..color = Colors.red
      ..style = PaintingStyle.fill;

    canvas.drawPaint(paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
}

```
The Code


![First Paint Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bq5fh9q4pxl0pjg1j96k.png)

The output 🌚.

Not what you were expecting? 💔

Stick around, we'll cover more in the next Edpisode. Until then, keep FLUTTERING! 💙.

Meanwhile, feel free to explore some of my other creative works [here](https://github.com/gabbygreat/helper-project). Not all of them are CustomPaint though, but, most of them are.

I know you enjoyed the article and would like to see more of it.

![Smirk](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/irh1cs31fv097ssehyee.jpeg)

Hold on to your paint brush because, more are yet to come. Can you follow me on my socials?, I drop amazing contents too, every once in a while 🥹.

- [Twitter](https://twitter.com/iGabbygreat)

- [WhatsApp](https://wa.me/+2348034339010)

- [LinkedIn](https://www.linkedin.com/in/gabbygreat)

If you have any questions relating to this article you can leave them in the comment section and I'll attend to them as soon as I can 😊.

Until then, keep FLUTTERING! (again) 💙.