用户点击后,我希望画布重绘,但它永远不会。我尝试使用 setState 并返回不同的画家,但似乎没有任何效果。如果我翻转屏幕或将页面替换为自身,它会被重新绘制,但这确实效率低下并且看起来不太好。我自己尝试对此进行研究,但所有潜在的答案都太令人困惑而无法理解,或者根本不起作用。任何帮助将不胜感激。
以下是代码的相关部分:
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();
//}
}