CustomPaint Widget and Attributes

CustomPaint Widget and Attributes

ยท

2 min read

The CustomPaint widget in Flutter is used to create custom graphics and drawings by implementing the CustomPainter class. It allows developers to create complex and custom shapes, paths, and animations directly within the Flutter framework.

Attributes:

  1. painter (CustomPainter):

    • The custom painter responsible for painting the custom graphics. It should extend the CustomPainter class and implement the paint and shouldRepaint methods.
  2. foregroundPainter (CustomPainter):

    • An optional custom painter that draws on top of the child widget. This painter is drawn after the child widget has been painted.
  3. size (Size):

    • The size of the custom paint area. If not specified, the size will be determined by the parent widget.
  4. isComplex (bool):

    • Whether the custom paint is expected to be complex. If set to true, the painting framework will attempt to use more advanced techniques for rendering.
  5. willChange (bool):

    • Whether the custom paint is expected to change frequently. If set to true, the painting framework will allocate resources differently to optimize for frequent changes.

Example:

import 'package:flutter/material.dart';

class CustomPaintExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('CustomPaint Widget Example'),
      ),
      body: Center(
        child: CustomPaint(
          size: Size(200, 200),
          painter: MyPainter(),
        ),
      ),
    );
  }
}

class MyPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint()
      ..color = Colors.blue
      ..strokeWidth = 5
      ..style = PaintingStyle.stroke;

    canvas.drawLine(Offset(0, 0), Offset(size.width, size.height), paint);
    canvas.drawLine(Offset(size.width, 0), Offset(0, size.height), paint);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return false;
  }
}

void main() {
  runApp(MaterialApp(
    home: CustomPaintExample(),
  ));
}

Explanation:

  • In this example, a CustomPaint widget is used to draw custom graphics.

  • The size attribute is set to a fixed size of 200x200 pixels.

  • The painter attribute is set to a custom painter class MyPainter.

  • The MyPainter class extends CustomPainter and overrides the paint method to define custom drawing operations.

  • Inside the paint method, a blue diagonal line is drawn from the top-left corner to the bottom-right corner, and another diagonal line is drawn from the top-right corner to the bottom-left corner.

  • The shouldRepaint method returns false because the custom paint does not need to be repainted frequently in this example.

  • When you run this code, you'll see a 200x200 square with diagonal lines drawn inside it.

Did you find this article valuable?

Support Vinit Mepani by becoming a sponsor. Any amount is appreciated!

ย