Custom painting in Flutter - Flutter In Focus

Flutter · 197K views · 2019 · 5:36

Open on YouTube

Software (other)UX & UI designtutorial

Why Stack Overflow recommends it

Stack Overflow users link it as the official walkthrough of CustomPaint and CustomPainter, and for its FittedBox plus SizedBox trick for sizing and scaling custom-painted content.

Recommended by 2 people across 2 different months, 2019 to 2023.

What it is

A 5.5 minute Flutter in Focus episode from the official Flutter channel, in which Matt introduces the CustomPaint widget and CustomPainter class. It covers writing a custom painter and sizing the canvas properly, using a FittedBox with a SizedBox, to produce a polished custom UI.

What people said

Recommends David Oldford · 2020-07 · Stack Overflow · on “Flutter camera appears stretched”

Edited: the below method works pretty well but using a FittedBox along with a SizedBox works much better. see this video from the flutter devs [this video] . FittedBox(child: SizedBox(child: texture, width: imageWidthInPixels, height: imageHeightInPixels)); The best way to do this is to follow the instructions in the Widget of the week video on AspectRatio [LINK] . Use AspectRatio to make sure the video maintains it's aspect ratio (go figure.) Then Wrap in an Align and then an Expanded. If you use a RotatedBox to adjust for orientation wrap the aspect ratio in it within the Align. Expanded( child:Align( child:AspectRatio( child:texture, aspectRatio:cameraAspectRatio)

Recommends m123 · 2021-05 · Stack Overflow · on “Dear all flutter developer I would like to ask about How to draw line as the image below "flutter concept"?”

I would suggest to you to use the CustomPaint class, with this you are able to paint custom shapes. Docs: [LINK] Official video-guide: [this video]

Cites fabriziog · 2019-09 · Stack Overflow · on “Draw an image with custom Size and Rotation into a CustomPaint in Flutter”

How can I draw an image to a customPaint() specifying its size and rotation in Flutter? Normally with only drawImage() the image gets drawn with its dimensions being its dimensions in pixels. For example, a 40x40 pixels image gets drawn with dimensions of 40 by 40. How can I draw the same image but for example with a size of 80x80 and a rotation of 120°? I've tried drawAtlas but I couldn't find anything online. This is the code I have to draw. (It draws a 20x20 rectangle at the top-right of the screen). class ParticlePainter extends CustomPainter { final List<GameObject> objects; @override void paint(Canvas canvas, Size size) { canvas.drawRect( Rect.fromCenter( height: 20, width: 20, center:

Cites Daniel Wilson · 2020-02 · Stack Overflow · on “How to constrain an image on the canvas”

I would like to draw an image 200x200 pixels on the canvas, and then do some other things to it. The image I have is 900x690, so ideally I would like to "center crop" the image to use an Android term. This video [this video] for some reason tells us to use a FittedBox and then a SizedBox . I have also tried putting both of those in to a Container . I have tried setting the container size, the sized box size, and the customer painter Size to 200x200 and everything results in a full screen image. Who knows what I am doing wrong :) Here is the code: import 'dart:typed_data'; import 'dart:ui' as ui; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutt

Cites Zero Live · 2020-10 · Stack Overflow · on “Flutter - How to scale and draw over image in custompainter?”

I am trying to draw over image on custompainter. I am using the example on flutter custompainter video [this video] and here is what I have so far. I can draw in the image but I cannot scale image. How do I scale image on gesture and draw in image? I would prefer not to use any package. Container( height: double.infinity, width: double.infinity, color: Colors.black87, child: FittedBox( child: GestureDetector( onScaleStart: _scaleStartGesture, onScaleUpdate: _scaleUpdateGesture, onScaleEnd: (_) => _scaleEndGesture(), child: SizedBox( height: _image.height.toDouble(), width: _image.width.toDouble(), child: CustomPaint( willChange: true, painter: ImagePainter( image: _image, points: points ), ), ),

Cites Sam7919 · 2022-07 · Stack Overflow · on “Why is CustomPaint affected by whether it is under a Scaffold?”

App〈 MaterialApp〈 MyWidget : all is good Whether I draw the figure on a canvas with a small or a large scale—by replacing v = 100000.0 with v = 1000.0 in the following code, the image doesn't change, as expected. (If you are not familiar with the idea of using a SizedBox inside a FittedBox , it is explained here [this video] .) [LINK] import 'package:flutter/material.dart'; void main() => runApp(const MyApp()); const v = 10000.0; class Painter extends CustomPainter { @override void paint(Canvas canvas, Size size) { final mypaint = Paint() ..style = PaintingStyle.stroke ..strokeWidth = 0.012 * v ..color = Colors.blue; canvas.drawCircle( Offset(1.5 * v, 1 * v), 1 * v, mypaint); } @override bool sho

Cites MendelG · 2023-08 · Stack Overflow · on “How to draw a shape like this”

You can use CustomPainter [LINK] . The code will be two circles with a line between them, and a Plus/Minus text within the cirlces. Result [LINK] Example import 'package:flutter/material.dart'; const Color darkBlue = Color.fromARGB(255, 18, 32, 47); void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( theme: ThemeData.dark().copyWith( scaffoldBackgroundColor: darkBlue, ), debugShowCheckedModeBanner: false, home: Scaffold( body: Center( child: CircleShapeWidget(), ), ), ); } } class CircleShapePainter extends CustomPainter { @override void paint(Canvas canvas, Size size) { Paint paint = Paint() ..color = Colo