flutter icon indicating copy to clipboard operation
flutter copied to clipboard

[BUG] Hovering ListTile or TextField triggers unnecessary rebuild of CustomPaint

Open asmith20002 opened this issue 2 years ago • 0 comments

Looks similar to #117627.

Bug Summary

Hovering a ListTile with onTap enabled or Hovering a TextField triggers CustomPaint paint function. Even with shouldRepaint set to false.

Demonstration

https://user-images.githubusercontent.com/17809451/218256520-9283782f-39f2-48fb-a154-1b8cbc4f8b51.mp4

Sample code

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Column(
        children: [
          CustomPaint(
            painter: PaintBlue(),
            child: const SizedBox(
              width: 500,
              height: 200,
              child: Center(
                child: Icon(
                  Icons.format_paint_rounded,
                  color: Colors.white,
                  size: 40,
                ),
              ),
            ),
          ),
          const SizedBox(
            height: 100,
          ),
          const TextField(
            decoration: InputDecoration(
                hintText: 'Hovering me will cause rebuild of custompaint.'),
          ),
          const SizedBox(
            height: 100,
          ),
          ListTile(
            title: const Text('ListTile'),
            subtitle: const Text(
                'Hovering a ListTile with onTap enabled will also rebuild customPaint.'),
            onTap: () {
              // Do nothing.
            },
          )
        ],
      ),
    );
  }
}

class PaintBlue extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    // ignore: avoid_print
    print('Rebuilding!');

    canvas.drawRect(Rect.fromLTWH(0, 0, size.width, size.height),
        Paint()..color = Colors.blue);
  }

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

Flutter Doctor

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.7.3, on Microsoft Windows [Version 10.0.22621.1194], locale en-US)
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
[√] Chrome - develop for the web
[√] Visual Studio - develop for Windows (Visual Studio Community 2022 17.4.4)
[√] Android Studio (version 2021.2)
[√] VS Code, 64-bit edition (version 1.75.0)
[√] Connected device (4 available)
[√] HTTP Host Availability

• No issues found!

asmith20002 avatar Feb 11 '23 11:02 asmith20002