google / flutter.widgets

https://pub.dev/packages/flutter_widgets
BSD 3-Clause "New" or "Revised" License
1.37k stars 474 forks source link

[visibility_detector] Incorrect Visibility info Bug after updating to 4.0.0 or later #524

Open tolotrasamuel opened 5 months ago

tolotrasamuel commented 5 months ago

Problem description

Package: visibility_detector

Steps to reproduce

  1. I made a reproducible example:
import 'package:flutter/material.dart';
import 'package:visibility_detector/visibility_detector.dart';

class LogoItem extends StatefulWidget {
  final int id;
  const LogoItem({super.key, required this.id});

  @override
  State<LogoItem> createState() => _LogoItemState();
}

class _LogoItemState extends State<LogoItem> {
  bool visible = true;

  @override
  Widget build(BuildContext context) {
    final key = Key("logo_${widget.id}");
    return Container(
      color: Colors.grey,
      child: VisibilityDetector(
        // key: UniqueKey(),
        key: key,
        onVisibilityChanged: (info) {
          final visible = info.visibleFraction > 0;
          if (visible == this.visible) {
            return;
          }
          print("Visibility changed to $visible ${info.visibleFraction} ${info.size} ${info.visibleBounds}");
          this.visible = visible;
          setState(() {

          });
        },
        child: _buildLogo(),
      ),
    );
  }
  Widget _buildLogo() {
    if (!visible) {
      return const SizedBox.square(dimension: 300, child: Placeholder());
    }
    return  Stack(
      children: [
        const FlutterLogo(size: 300),
        Text("Logo ${widget.id}", style: const TextStyle(color: Colors.white, fontSize: 20)),
      ],
    );
  }
}

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

  static const row = 5;
  static const column = 5;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Interactive Viewer with Visibility Detector Bug'),
      ),
      body: InteractiveViewer(
        minScale: 0.01,
        boundaryMargin: const EdgeInsets.all(double.infinity),
        constrained: false,
        child:  Column(
          mainAxisAlignment: MainAxisAlignment.center,
          mainAxisSize: MainAxisSize.min,
          children: List.generate(row, (rowIndex) {
            return Row(
              mainAxisAlignment: MainAxisAlignment.center,
              mainAxisSize: MainAxisSize.min,
              children: List.generate(column, (colIndex) {
                final id = rowIndex * column + colIndex;
                return LogoItem(id: id);
              }),
            );
          })
        ),
      ),
    );
  }
}

Expected behavior

In version 0.3.3, It works correctly as shown in the video:

https://github.com/google/flutter.widgets/assets/29230303/8e0f66a6-2c50-45bc-a8e3-34c87e74d8be

Actual behavior

In version 0.4.0 and after, It outputs incorrect values as shown in the video

https://github.com/google/flutter.widgets/assets/29230303/72485d2d-a714-40d9-bd9a-7ee1d95f8b40

Environment

Faced this on Android. Didn't test other platform. Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 3.19.3, on macOS 14.0 23A344 darwin-arm64, locale en-US) [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) [✓] Xcode - develop for iOS and macOS (Xcode 15.0.1) [✓] Chrome - develop for the web [✓] Android Studio (version 2023.1) [✓] Connected device (4 available) [✓] Network resources

• No issues found!

Additional details

N/A