flutterlayoutwidgetcustom-paintingcustom-painter

How to have a dynamically sized CustomPainter in Flutter?


Consider the following code from the Flutter docs:

CustomPaint(
  painter: Sky(),
  child: const Center(
    child: Text(
      'Once upon a time...',
      style: TextStyle(
        fontSize: 40.0,
        fontWeight: FontWeight.w900,
        color: Color(0xFFFFFFFF),
      ),
    ),
  ),
)

I don't want a text widget.

I don't want the CustomPaint widget to be obscured by a child widget.

Also I want the CustomPaint widget to fill whatever space is available to it.

I presume the solution is to replace the Text widget with a BLANK widget like Expanded.

But, I've tried the Expanded, Container, Spacer, and Space widgets in various combinations but always either get an error or a zero size.

Thanks!


Solution

  • Here how you do it:

    Expanded(
      child: CustomPaint(
        painter: MyCustomPainter(),
        size: Size.infinite,
    
      ),
    ),