flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
163.67k stars 26.91k forks source link

[flutter_markdown] Disposes gesture recognizers when reparenting #150558

Open knopp opened 1 month ago

knopp commented 1 month ago

Example to reproduce

class _BaseContextMenu extends StatefulWidget {
  @override
  State<_BaseContextMenu> createState() => _BaseContextMenuState();
}

class _BaseContextMenuState extends State<_BaseContextMenu> {
  bool pointerDown = false;
  final key = GlobalKey();

  @override
  Widget build(BuildContext context) {
    final child = MarkdownBody(
      key: key,
      data: 'What is this [content](https://example.com)',
      onTapLink: (text, _, __) {
        print('Clicked on $text');
      },
    );
    return Listener(
      onPointerDown: (event) {
        setState(() {
          pointerDown = true;
        });
      },
      onPointerUp: (event) {
        setState(() {
          pointerDown = false;
        });
      },
      child: pointerDown
          ? ColoredBox(
              color: Colors.green,
              child: child,
            )
          : child,
    );
  }
}

The recognizer is never triggered, because gesture detectors are disposed inside _MarkdownWidgetState.didChangeDependencies() (through _parseMarkdown). During element reparenting the element is deactivated and activated, and didChangeDependencies is called during element activation.

huycozy commented 1 month ago

Reproduced this using the latest package version flutter_markdown: ^0.7.2+1.

flutter doctor -v ```bash [✓] Flutter (Channel stable, 3.22.2, on macOS 14.1 23B74 darwin-x64, locale en-VN) • Flutter version 3.22.2 on channel stable at /Users/huynq/Documents/GitHub/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 761747bfc5 (31 hours ago), 2024-06-05 22:15:13 +0200 • Engine revision edd8546116 • Dart version 3.4.3 • DevTools version 2.34.3 [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at /Users/huynq/Library/Android/sdk • Platform android-34, build-tools 34.0.0 • ANDROID_HOME = /Users/huynq/Library/Android/sdk • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 15.3) • Xcode at /Applications/Xcode15.3.app/Contents/Developer • Build 15E204a • CocoaPods version 1.15.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2023.3) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • android-studio-dir = /Applications/Android Studio.app/ • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160) [✓] VS Code (version 1.89.1) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.90.0 [✓] Connected device (3 available) • iPhone (mobile) • d9a94afe2b649fef56ba0bfeb052f0f2a7dae95e • ios • iOS 15.8 19H370 • macOS (desktop) • macos • darwin-x64 • macOS 14.1 23B74 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 125.0.6422.142 [✓] Network resources • All expected network resources are available. • No issues found! ```
mjurfest commented 4 days ago

@huycozy @stuartmorgan do you have an update about this issue?

stuartmorgan commented 4 days ago

@mjurfest Please see https://github.com/flutter/flutter/blob/master/docs/contributing/issue_hygiene/README.md#do-not-add-me-too-or-same-or-is-there-an-update-comments-to-issues-or-prs