|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | +import 'dart:html' as html; |
| 5 | +import 'dart:js_util' as js_util; |
| 6 | + |
| 7 | +import 'package:flutter/material.dart'; |
| 8 | + |
| 9 | +void main() => runApp(MyApp()); |
| 10 | + |
| 11 | +class MyApp extends StatelessWidget { |
| 12 | + // This widget is the root of your application. |
| 13 | + @override |
| 14 | + Widget build(BuildContext context) { |
| 15 | + return MaterialApp( |
| 16 | + title: 'Flutter Scroll Wheel Test', |
| 17 | + theme: ThemeData( |
| 18 | + // This is the theme of your application. |
| 19 | + // |
| 20 | + // Try running your application with "flutter run". You'll see the |
| 21 | + // application has a blue toolbar. Then, without quitting the app, try |
| 22 | + // changing the primarySwatch below to Colors.green and then invoke |
| 23 | + // "hot reload" (press "r" in the console where you ran "flutter run", |
| 24 | + // or simply save your changes to "hot reload" in a Flutter IDE). |
| 25 | + // Notice that the counter didn't reset back to zero; the application |
| 26 | + // is not restarted. |
| 27 | + primarySwatch: Colors.blue, |
| 28 | + fontFamily: 'RobotoMono', |
| 29 | + // This makes the visual density adapt to the platform that you run |
| 30 | + // the app on. For desktop platforms, the controls will be smaller and |
| 31 | + // closer together (more dense) than on mobile platforms. |
| 32 | + visualDensity: VisualDensity.adaptivePlatformDensity, |
| 33 | + ), |
| 34 | + home: MyHomePage(title: 'Flutter Scroll Wheel Test'), |
| 35 | + ); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +class MyHomePage extends StatefulWidget { |
| 40 | + MyHomePage({Key key, this.title}) : super(key: key); |
| 41 | + |
| 42 | + // This widget is the home page of your application. It is stateful, meaning |
| 43 | + // that it has a State object (defined below) that contains fields that affect |
| 44 | + // how it looks. |
| 45 | + |
| 46 | + // This class is the configuration for the state. It holds the values (in this |
| 47 | + // case the title) provided by the parent (in this case the App widget) and |
| 48 | + // used by the build method of the State. Fields in a Widget subclass are |
| 49 | + // always marked "final". |
| 50 | + |
| 51 | + final String title; |
| 52 | + |
| 53 | + @override |
| 54 | + _MyHomePageState createState() => _MyHomePageState(); |
| 55 | +} |
| 56 | + |
| 57 | +class _MyHomePageState extends State<MyHomePage> { |
| 58 | + int _counter = 0; |
| 59 | + |
| 60 | + void _incrementCounter() { |
| 61 | + setState(() { |
| 62 | + // This call to setState tells the Flutter framework that something has |
| 63 | + // changed in this State, which causes it to rerun the build method below |
| 64 | + // so that the display can reflect the updated values. If we changed |
| 65 | + // _counter without calling setState(), then the build method would not be |
| 66 | + // called again, and so nothing would appear to happen. |
| 67 | + _counter++; |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + @override |
| 72 | + Widget build(BuildContext context) { |
| 73 | + // This method is rerun every time setState is called, for instance as done |
| 74 | + // by the _incrementCounter method above. |
| 75 | + // |
| 76 | + // The Flutter framework has been optimized to make rerunning build methods |
| 77 | + // fast, so that you can just rebuild anything that needs updating rather |
| 78 | + // than having to individually change instances of widgets. |
| 79 | + return Scaffold( |
| 80 | + appBar: AppBar( |
| 81 | + // Here we take the value from the MyHomePage object that was created by |
| 82 | + // the App.build method, and use it to set our appbar title. |
| 83 | + title: Text(widget.title), |
| 84 | + ), |
| 85 | + body: ListView.builder( |
| 86 | + itemCount: 1000, |
| 87 | + itemBuilder: (context, index) => Padding( |
| 88 | + padding: EdgeInsets.all(20), |
| 89 | + child: Container( |
| 90 | + height: 100, |
| 91 | + color: Colors.lightBlue, |
| 92 | + child: Center( |
| 93 | + child: Text("Item $index"), |
| 94 | + ), |
| 95 | + ), |
| 96 | + ), |
| 97 | + ), |
| 98 | + floatingActionButton: |
| 99 | + FloatingActionButton.extended( |
| 100 | + key: const Key('scroll-button'), |
| 101 | + onPressed: () { |
| 102 | + final int centerX = 100; //html.window.innerWidth ~/ 2; |
| 103 | + final int centerY = 100; //html.window.innerHeight ~/ 2; |
| 104 | + dispatchMouseWheelEvent(centerX, centerY, DeltaMode.kLine, 0, 1); |
| 105 | + dispatchMouseWheelEvent(centerX, centerY, DeltaMode.kLine, 0, 1); |
| 106 | + dispatchMouseWheelEvent(centerX, centerY, DeltaMode.kLine, 0, 1); |
| 107 | + dispatchMouseWheelEvent(centerX, centerY, DeltaMode.kLine, 0, 1); |
| 108 | + dispatchMouseWheelEvent(centerX, centerY, DeltaMode.kLine, 0, 1); |
| 109 | + }, |
| 110 | + label: Text('Scroll'), |
| 111 | + icon: Icon(Icons.thumb_up), |
| 112 | + ), |
| 113 | + ); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | + |
| 118 | +abstract class DeltaMode { |
| 119 | + static const int kPixel = 0x00; |
| 120 | + static const int kLine = 0x01; |
| 121 | + static const int kPage = 0x02; |
| 122 | +} |
| 123 | + |
| 124 | +html.WheelEvent dispatchMouseWheelEvent(int mouseX, int mouseY, |
| 125 | + int deltaMode, double deltaX, double deltaY, |
| 126 | + {bool shiftKeyPressed = false}) { |
| 127 | + html.EventTarget target = html.document.elementFromPoint(mouseX, mouseY); |
| 128 | + |
| 129 | + target.dispatchEvent(html.MouseEvent("mouseover", |
| 130 | + screenX: mouseX, |
| 131 | + screenY: mouseY, |
| 132 | + clientX: mouseX, |
| 133 | + clientY: mouseY, |
| 134 | + )); |
| 135 | + |
| 136 | + target.dispatchEvent(html.MouseEvent("mousemove", |
| 137 | + screenX: mouseX, |
| 138 | + screenY: mouseY, |
| 139 | + clientX: mouseX, |
| 140 | + clientY: mouseY, |
| 141 | + )); |
| 142 | + |
| 143 | + html.WheelEvent event = html.WheelEvent('wheel', |
| 144 | + screenX: mouseX, |
| 145 | + screenY: mouseY, |
| 146 | + clientX: mouseX, |
| 147 | + clientY: mouseY, |
| 148 | + deltaMode: deltaMode, |
| 149 | + deltaX : deltaX, |
| 150 | + deltaY : deltaY, |
| 151 | + shiftKey: shiftKeyPressed, |
| 152 | + ); |
| 153 | + target.dispatchEvent(event); |
| 154 | + return event; |
| 155 | +} |
0 commit comments