Building a Sketch Application by using Custom Painters in Dart's Flutter Framework

in #utopian-io6 years ago (edited)

flutter-logo.jpg

What Will I Learn?

  • You will learn how to use Custom Painters
  • You will learn how to scale offsets based on render objects
  • You will learn how to find the closest render object ancestor with the find render object method
  • You will learn how to work with pan update events in gesture detectors
  • You will learn about the Paint object
  • You will learn the difference between global and local offset coordinates

Requirements

System Requirements:
OS Support for Flutter:
  • Windows 7 SP1 or later (64-bit)
  • macOS (64-bit)
  • Linux (64-bit)

Required Knowledge

  • A basic knowledge of canvas and geometry
  • A fair understanding of Mobile development and Imperative or Object Oriented Programming
  • A sense of how tree searching works

Resources for Flutter and this Project:

Sources:

Flutter Logo (Google): https://flutter.io/

Difficulty

  • Intermediate

Description

Outline and Overview

In this Flutter Video tutorial, we take a look at how we can build an a Sketch style application in the Flutter framework. We make use of the Gesture Detector Widget, a Custom Painter, Offsets, the Render Box Object and a few other concepts to build this application.

Outline for this Tutorial

In the past few Flutter Video Tutorials of this series, we have looked at slivers and render boxes. Custom Painters and Painters are also a central concept in Flutter. In this tutorial, we look at how we can use some of these ideas; specifically the render box and the Custom Painter, to build out a Sketch style application.

Item 1: Custom Painters

When a widget is built, it is laid out and painted using various different properties. The main properties that define how a widget will look are the layout, the constraints and the painter. The painter of a widget follows a set of rules which we can augment with the custom painter class. The custom painter class is a widget just like all of the other elements inside of flutter and it is very similar the inherited widget.

custom-painter.jpg

In this image, you can see the custom painter that was used in our application. This painter has a method called shouldRepaint that is called when the widget needs to be repainted. This method has a form that is very similar to the updateShouldNotify method that is featured in the inherited widget. We also define a paint function which is called when the shouldRepaint method comes back with true.

Item 2: Using Render Boxes to Scale Offsets

An offset is a fairly basic concept that can be used in many instances. The offset is a immutable two dimensional floating point that exists on a Cartesian plane. This means that an offset has both an x and a y value. Offsets are used to find out the position of items and widgets inside of Flutter. They can also be scaled from logical pixels to canvas coordinates.

global-to-local.jpg

The job of this globalToLocal method call is to convert an offset from a unit of logical pixels to a scaled unit that is based on the canvas of the render object defined by box.

render-object-box.jpg

The box object is defined by using a search algorithm called findRenderObject. This algorithm searches through the flutter widget tree to find the closest ancestor render box object. This is a relatively expensive computation due to its time complexity being O(N) in the depth of the tree. However, because the method finds the closest render box object, this expense is minimized.

Further Changing Offsets with On Pan Update

In this application, we make use of a Gesture Detector widget. This widget is used to respond when the user has their finger on the draw area. When the user's finger is dragging across the draw area, we want a line to appear underneath of it. We do this by using our custom painter and by scaling the offset. We have to further scale the offset to account for Appbar Widget on top of our scaffold. We can use the translate method to subtract the pixel size of the Appbar widget from the Y coordinate of the offset.

draw.gif

You can see the application in action in this video. As the mouse is moved while the left click is held down, a line is drawn underneath of it. This line continues until the user stops pressing the left click on their mouse or removes their finger from the touch screen of their device. This line is made up of multiple different offsets and a set of tiny lines drawn between each offset by our custom painter.

Project GitHub Repository:

The Source Code for this project can be found here

Video Tutorial

Projects and Series

Stand Alone Projects:
Building a Calculator
Movie Searcher Application

Minesweeper Game

Weather Application

Curriculum

Sort:  

What's the font you're using for your vs code?

Fira Code with Ligatures enabled.

Thank you for your contribution @tensor.
It has been reviewed and scored.


Need help? Write a ticket on https://support.utopian.io.
Chat with us on Discord.

[utopian-moderator]

Thank you for moderating my contribution.

You just received a Tier 0 upvote! Looking for bigger rewards? Click here and learn how to get them or visit us on Discord

So, after personally commenting on my content that you had realized your mistake you still give me this tier 0 vote. You guys are really winning me over here.....

@tensor You have earned a random upvote from @botreporter because this post did not use any bidbots.

Hey @tensor

We're already looking forward to your next contribution!

Utopian Witness!

Vote for Utopian Witness! We are made of developers, system administrators, entrepreneurs, artists, content creators, thinkers. We embrace every nationality, mindset and belief.

Want to chat? Join us on Discord https://discord.gg/h52nFrV

Coin Marketplace

STEEM 0.20
TRX 0.13
JST 0.030
BTC 64269.87
ETH 3393.79
USDT 1.00
SBD 2.48