samedi 25 septembre 2021

Flutter basic app stops responding on Chrome when displaying context menu running in profile or release mode

Here is the problem.

  1. Create a fresh flutter app - the one that increments the counter.

  2. In the initState I override the mouse left click context menu.

  3. Create a simple context menu.

  4. In the build method add listener that shows that context menu when right mouse button is clicked.

  5. I try to run the app in chrome

    flutter run -d chrome --web-port 5000 --web-renderer html

In debug mode everything works fine.

However in profile mode or release mode, the context menu is displayed but when I mouse-click anywhere, the page stops responding completely.

Here is how I run it in profile mode:

flutter run -d chrome  --web-port 5000 --web-renderer html --profile

Here is full code:

import 'package:universal_html/html.dart';
import 'dart:ui';

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  @override
  void initState() {
    super.initState();

    document.onContextMenu.listen((event) => event.preventDefault());
  }

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  Future<void> _onPointerDown(PointerDownEvent event) async {
    // Check if right mouse button clicked
    if (event.kind == PointerDeviceKind.mouse &&
        event.buttons == kSecondaryMouseButton) {
      final overlay =
          Overlay.of(context)!.context.findRenderObject() as RenderBox;
      final menuItem = await showMenu<int>(
          color: Colors.grey.shade100,
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
          context: context,
          items: [
            PopupMenuItem(
              padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 3),
              height: 15,
              child: Row(
                children: const [
                  Icon(
                    Icons.brush,
                    color: Colors.blue,
                  ),
                  SizedBox(
                    width: 10,
                  ),
                  Text('Highlight'),
                ],
              ),
              value: 1,
            ),
            PopupMenuItem(
                padding:
                    const EdgeInsets.symmetric(horizontal: 15, vertical: 3),
                height: 15,
                child: Row(
                  children: const [
                    Icon(
                      Icons.note_add_rounded,
                      color: Colors.blue,
                    ),
                    SizedBox(
                      width: 10,
                    ),
                    Text('Notes'),
                  ],
                ),
                value: 2),
            PopupMenuItem(
                padding:
                    const EdgeInsets.symmetric(horizontal: 15, vertical: 3),
                height: 15,
                child: Row(
                  children: const [
                    Icon(
                      Icons.clear,
                      color: Colors.red,
                    ),
                    SizedBox(
                      width: 10,
                    ),
                    Text('Clear'),
                  ],
                ),
                value: 3),
            PopupMenuItem(
                padding:
                    const EdgeInsets.symmetric(horizontal: 15, vertical: 3),
                height: 15,
                child: Row(
                  children: const [
                    Icon(
                      Icons.cancel,
                      color: Colors.red,
                    ),
                    SizedBox(
                      width: 10,
                    ),
                    Text('Clear all'),
                  ],
                ),
                value: 4),
          ],
          position: RelativeRect.fromSize(
              event.position & Size(48.0, 48.0), overlay.size));
      // Check if menu item clicked
      switch (menuItem) {
        case 1: // Case for 'Highlight'
          {}
          break;
        case 2: // Case for 'Notes'
          {}
          break;
        case 3: //Case for 'Clear'
          {}
          break;
        case 4: // Case for 'Clear all'
          {}
          break;
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Listener(
      onPointerDown: _onPointerDown,
      child: Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              const Text(
                'You have pushed the button this many times:',
              ),
              Text(
                '$_counter',
                style: Theme.of(context).textTheme.headline4,
              ),
            ],
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: _incrementCounter,
          tooltip: 'Increment',
          child: const Icon(Icons.add),
        ),
      ),
    );
  }
}

Here is flutter doctor output:

flutter doctor -v                                                       greg@Gregs-MBP

[✓] Flutter (Channel stable, 2.5.1, on macOS 11.5.2 20G95 darwin-arm, locale en-AU) • Flutter version 2.5.1 at /Users/greg/Development/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision ffb2ecea52 (8 days ago), 2021-09-17 15:26:33 -0400 • Engine revision b3af521a05 • Dart version 2.14.2

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3) • Android SDK at /Users/greg/Library/Android/sdk • Platform android-S, build-tools 30.0.3 • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165) • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS • Xcode at /Applications/Xcode.app/Contents/Developer • Xcode 13.0, Build version 13A233 • CocoaPods version 1.10.1

[✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2020.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 • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)

[✓] VS Code (version 1.60.2) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.26.0

[✓] Connected device (3 available) • Tsvi’s iPad (mobile) • 00008027-001A150622E8002E • ios • iOS 14.8 18H17 • macOS (desktop) • macos • darwin-arm64 • macOS 11.5.2 20G95 darwin-arm • Chrome (web) • chrome • web-javascript • Google Chrome 94.0.4606.61

• No issues found!

I have absolutely no idea what is going on here. If anyone could help me it would be greatly appreciated!

I also tried creating a build and uploading to a web server - same thing, the page stops responding.




Aucun commentaire:

Enregistrer un commentaire