gpt4 book ai didi

Flutter:CustomPainter 绘制方法被调用多次而不是一次

转载 作者:行者123 更新时间:2023-12-04 08:02:24 24 4
gpt4 key购买 nike

我有一个简单的应用程序,它通过 CustomPainter 进行绘制 Canvas 上的红色或绿色圆圈,取决于在 AppBar 中按下了哪个按钮:
Red Circle
Green Circle
类(class)ColorCircle扩展 CustomPainter并负责绘制彩色圆圈:

class ColorCircle extends CustomPainter {
MaterialColor myColor;

ColorCircle({@required this.myColor});

@override
void paint(Canvas canvas, Size size) {
debugPrint('ColorCircle.paint, ${DateTime.now()}');
final paint = Paint()..color = myColor;
canvas.drawCircle(Offset(size.width / 2, size.height / 2), 100, paint);
}

@override
bool shouldRepaint(CustomPainter oldDelegate) => false;
}
绘制不同颜色的效果很好,但是当我单击(仅一次!)或将鼠标悬停在其中一个按钮上时, paint方法被多次调用:
Debugmessage

进一步的实现细节:
我用的是 StatefulWidget用于存储 actualColor .在构建方法中 actualColor传递给 ColorCircle构造函数:
class _MyHomePageState extends State<MyHomePage> {
MaterialColor actualColor = Colors.red;

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
actions: <Widget>[
OutlinedButton(
onPressed: () => setState(() => actualColor = Colors.red),
child: Text('RedCircle'),
),
OutlinedButton(
onPressed: () => setState(() => actualColor = Colors.green),
child: Text('GreenCircle'),
),
],
),
body: Center(
child: CustomPaint(
size: Size(300, 300),
painter: ColorCircle(myColor: actualColor),
),
),
);
}
}
可以在此处找到带有运行示例的完整源代码: CustonPainter Demo

那么为什么是 paint多次调用而不是只调用一次? (你怎么能实现它以便 paint 只被调用一次?)。

最佳答案

一个糟糕的解决方案可能是添加 RepaintBoundary围绕悬停小部件:

class _MyHomePageState extends State<MyHomePage> {
MaterialColor actualColor = Colors.red;

@override
Widget build(BuildContext context) {
print('Rebuilding with $actualColor');
return Scaffold(
appBar: AppBar(
title: Text('CustomPainter Demo'),
actions: <Widget>[
RepaintBoundary(
child: OutlinedButton(
style: ButtonStyle(
foregroundColor: MaterialStateProperty.all(Colors.black)),
onPressed: () {
setState(() => actualColor = Colors.red);
},
child: Text('RedCircle')),
),
RepaintBoundary(
child: OutlinedButton(
style: ButtonStyle(
foregroundColor: MaterialStateProperty.all(Colors.black)),
onPressed: () {
setState(() => actualColor = Colors.green);
},
child: Text('GreenCircle')),
),
],
),
body: Center(
child: CustomPaint(
size: Size(300, 300),
painter: ColorCircle(myColor: actualColor),
),
),
);
}
}
然后,正确定义 shouldRepaint ColorCircle的方法(目前返回 false ):
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return (oldDelegate as ColorCircle).myColor != myColor;
}
这似乎是一个非常糟糕的解决方案。我很想知道一个更好、更可持续的答案。
带有 RepaintBoundary 的完整源代码解决方法
import 'package:flutter/material.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'CustomPainter Demo',
home: MyHomePage(),
);
}
}

class ColorCirle extends CustomPainter {
MaterialColor myColor;

ColorCirle({@required this.myColor});
@override
void paint(Canvas canvas, Size size) {
debugPrint('ColorCircle.paint, ${DateTime.now()}');
final paint = Paint()..color = myColor;
canvas.drawCircle(Offset(size.width / 2, size.height / 2), 100, paint);
}

@override
bool shouldRepaint(CustomPainter oldDelegate) {
return (oldDelegate as ColorCirle).myColor != myColor;
}
}

class MyHomePage extends StatefulWidget {
const MyHomePage({Key key}) : super(key: key);

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
MaterialColor actualColor = Colors.red;

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('CustomPainter Demo'),
actions: <Widget>[
RepaintBoundary(
child: OutlinedButton(
style: ButtonStyle(
foregroundColor: MaterialStateProperty.all(Colors.black)),
onPressed: () {
setState(() => actualColor = Colors.red);
},
child: Text('RedCircle')),
),
RepaintBoundary(
child: OutlinedButton(
style: ButtonStyle(
foregroundColor: MaterialStateProperty.all(Colors.black)),
onPressed: () {
setState(() => actualColor = Colors.green);
},
child: Text('GreenCircle')),
),
],
),
body: Center(
child: CustomPaint(
size: Size(300, 300),
painter: ColorCirle(myColor: actualColor),
),
),
);
}
}

关于Flutter:CustomPainter 绘制方法被调用多次而不是一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66388418/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com