After the user taps, I want the canvas to redraw, but it never does. I tried to use setState and return different painters but nothing seems to work. It gets repainted if I flip the screen or replace the page with itself but that's really inefficient and doesn't look good. I tried researching this myself but all potential answers were too confusing to understand or simply did not work. Any help would be greatly appreciated.
Here are the relevant parts of the code:
import 'package:flutter/material.dart';
class Playpage extends MaterialPageRoute<Null> {
Playpage() : super(builder: (BuildContext ctx) {
return Scaffold(
appBar:PreferredSize(
preferredSize: Size.fromHeight(40.0),
child:AppBar(
title: Text('Level!'),
centerTitle: true,
),
),
body: Container(
color: Colors.white,
padding: EdgeInsets.symmetric(horizontal: 50, vertical: 0),
child: LayoutBuilder(
// Inner yellow container
builder: (_, constraints) => Container(
width: constraints.widthConstraints().maxWidth,
height: constraints.heightConstraints().maxHeight,
color: Colors.yellow,
child:
GestureDetector(
onTap: () {
//Navigator.pushReplacement(ctx, Playpage());
},
onTapDown: (TapDownDetails details) => _onTapDown(details),
child: new CustomPaint (painter: new BoardPainter()),
),
),
),
),
);
}
);
}
Color colour= Colors.white;
class BoardPainter extends CustomPainter {
@override
bool shouldRepaint(CustomPainter oldDelegate){
return true;}
@override
// TODO: draw something with canvas
void paint(Canvas canvas, Size size){
canvas.drawRRect(
RRect.fromRectAndRadius(Rect.fromLTWH(200, 200, 100, 100), Radius.circular(20)),
Paint() ..color = colour,
);
}
}
_onTapDown(TapDownDetails details) {
//setState() {
colour = Colors.blue;
BoardPainter();
//}
}