scroll_wheel_main.dart 3.04 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:html' as html;

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 Scroll Wheel Test',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        fontFamily: 'RobotoMono',
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: const MyHomePage(title: 'Flutter Scroll Wheel Test'),
    );
  }
}

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: ListView.builder(
        itemCount: 1000,
        itemBuilder: (BuildContext context, int index) => Padding(
          padding: const EdgeInsets.all(20),
          child: Container(
            height: 100,
            color: Colors.lightBlue,
            child: Center(
              child: Text('Item $index'),
            ),
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton.extended(
        key: const Key('scroll-button'),
        onPressed: () {
          const int centerX = 100; //html.window.innerWidth ~/ 2;
          const int centerY = 100; //html.window.innerHeight ~/ 2;
          dispatchMouseWheelEvent(centerX, centerY, DeltaMode.kLine, 0, 1);
          dispatchMouseWheelEvent(centerX, centerY, DeltaMode.kLine, 0, 1);
          dispatchMouseWheelEvent(centerX, centerY, DeltaMode.kLine, 0, 1);
          dispatchMouseWheelEvent(centerX, centerY, DeltaMode.kLine, 0, 1);
          dispatchMouseWheelEvent(centerX, centerY, DeltaMode.kLine, 0, 1);
        },
        label: const Text('Scroll'),
        icon: const Icon(Icons.thumb_up),
      ),
    );
  }
}

abstract class DeltaMode {
  static const int kPixel = 0x00;
  static const int kLine = 0x01;
  static const int kPage = 0x02;
}

void dispatchMouseWheelEvent(int mouseX, int mouseY,
    int deltaMode, double deltaX, double deltaY) {
  final html.EventTarget target = html.document.elementFromPoint(mouseX, mouseY)!;

  target.dispatchEvent(html.MouseEvent('mouseover',
    screenX: mouseX,
    screenY: mouseY,
    clientX: mouseX,
    clientY: mouseY,
  ));

  target.dispatchEvent(html.MouseEvent('mousemove',
    screenX: mouseX,
    screenY: mouseY,
    clientX: mouseX,
    clientY: mouseY,
  ));

  target.dispatchEvent(html.WheelEvent('wheel',
    screenX: mouseX,
    screenY: mouseY,
    clientX: mouseX,
    clientY: mouseY,
    deltaMode: deltaMode,
    deltaX : deltaX,
    deltaY : deltaY,
    shiftKey: false,
  ));
}