17 Reasons Why You Should Ignore Trade Forex Online

As an expert in the Forex sector, I'm typically requested by my relatives and buddies about the best way to trade Forex. Properly, the very first thing I notify them is "Do not", due to the fact…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Drawing Custom Shapes in Flutter using CustomPainter

Flutter is a Mobile UI framework by Google which allows developers to create beautiful apps in record time for both iOS and Android with a single codebase. Most talked about feature that Flutter comes with is “Hot Reload” that allows blazing fast reload times (without actually loosing the current state of the app)

This article assumes that you are already familiar with or at least have an understanding of building a basic Flutter App with simple nested widgets.

Apart from inbuilt or trivial widgets that Flutter Framework provides like a Text Widget, a RaisedButton or a Container for bare bones what if we want to draw a custom button that has a custom shape or you just simply want to bring out that inner artist in you, Flutter makes it a breeze to draw these custom artistic shapes.

Custom Lines draw using CustomPainter in flutter.

Custom Lines

Custom Shapes and Curves drawn using CustomPainter in Flutter

Custom Shapes and Custom Curves

Step 1: Create a new Flutter Project

Run the following command in your Terminal/Command prompt

This will create a new flutter project and set up all the dependencies for you. After its done just open the folder in your preferred code editor. I will be using VSCode throughout this article for this project.

You can see the basic material app code that flutter generated for you in the lib/main.dart file. To run the app in emulator or your phone just goto Command Pallet (Command/Control + Shift + P) and type “Flutter: Select Device” and choose the device in which you would like to run the app. Goto Debug -> Start Without Debugging to start compiling and running the app on the chosen device.

Selecting the device option from the VSCode Command Pallete

Flutter: Select Device

Selecting the Device on which the app will run on from the list of devices connected in VSCode

List of Emulators or devices connected

Start the app without debugging option in VSCode to run the flutter app

Running the app on chosen device

Step 2: Creating a Base UI

Let’s remove all the unwanted code from the main.dart file and start from the beginning with a bare minimum app screen that has a simple scaffold with an AppBar having the title.

Step 3: Let’s draw something

CustomPaint widget requires mainly two things, a painter and a child widget. The Custom paint uses the painter to paint/draw (ex: custom shapes) on the canvas after which it draws the child widget on top of it. Let’s add this CustomPaint Widget to our app and start drawing something.

ShapesPainter is an instance of a class that extends CustomPainter. The CustomPainter class provides us with 2 methods to override.

So to just sum it all up, we use a paint (more like a brush) and draw stuffs on the Canvas that we have been provided with which has some width and height. It’s similar to how we draw something on a piece of paper using a pencil. Here the paper would have definite width and height (that would be the Canvas object here) and the pencil that we used to draw might have different levels of darkness and color etc. (that would be our Paint object here).

Let’s create a simple paint object which has a color of Colors.deepOrange and draw a circle on the canvas.

canvas.drawCircle(…) method takes in three parameters, the centre of the circle, the radius and the paint object to be used. Here, we draw the circle at the centre of the canvas. We find the centre using size.width and size.height from the Size object which returns the width and height of the canvas.

The shouldRepaint method here just returns false since we don’t have any fields/values that might change and influence the custom shapes that we draw.

Custom White Rectangle drawn which spans to the canvas width and height but its drawn above the orange circle
White Rectangle

But wait, where is the circle?

Well, the circle hasn’t gone anywhere. It’s just hidden behind the rectangle. This brings up an important characteristic of drawing using CustomPainter here. The order how you write the draw commands matter. If you look at the code closely, the drawCircle() function is called first and after that the drawRect(). So, the circle will be drawn on the canvas first and then the rectangle. We can easily fix (since we want the circle on the rectangle) that by just moving the drawCircle() function after the drawRect().

Looks good! Now let’s draw a custom path and wrap this up!

To draw a custom path we’ll use drawPath() method from the canvas. We’ll draw a path from top-left(0,0) to bottom-left(0, size.height) to top-right(size.width, 0) which forms a simple triangle.

Coordinate system in the Flutter canvas
Coordinate system

We can use the lineTo() method to create a line from (x,y) to (x1, y1). Here it would be from (0,0) which is initialised by default, to (0, size.height) which is the bottom-left of the canvas. Finally we use close() to close the path which will forms a triangle.

A yellow path drawn on canvas that makes up a triangle
Yellow path

Let’s move the code that draws the circle to the end so that it’s layered on top of all the other shapes.

Here’s the entire code.

Final app with a white rectangular background and yellow triangle path and an orange circle on top all of which drawn on the canvas.
Final App with custom shapes

Play around with the code and try new shapes and colors and what not.

🤤

Add a comment

Related posts:

HavEther Listed On Binance Info

After long struggle to be listed on Binance info Platform, the struggle finally comes to an end, now binance has approved HavEther to be appear on their their platform: https://info.binance.com/en…

Lead Generation Content Types to Include on Your Landing Pages

The process of recruiting and converting strangers and prospects into someone who has expressed interest in your company’s product or service is known as lead generation. Job applications, blog…

101 Machine Learning Algorithms for Data Science with Cheat Sheets

Think of this as the one-stop-shop/dictionary/directory for your machine learning algorithms. The algorithms have been sorted into 9 groups: Anomaly Detection, Association Rule Learning…