|
1 | | -// ignore: avoid_web_libraries_in_flutter |
2 | | -import 'dart:html'; |
3 | | - |
| 1 | +import 'dart:js_interop'; |
4 | 2 | import 'package:flutter/material.dart'; |
| 3 | +import 'package:web/web.dart' as web; |
5 | 4 |
|
6 | 5 | import 'platforms_widgets_binding.dart'; |
7 | 6 |
|
8 | 7 | PlatformsWidgetsBinding getInstance() => WebWidgetsBinding(); |
9 | 8 |
|
10 | 9 | class WebWidgetsBinding extends PlatformsWidgetsBinding { |
| 10 | + late JSFunction _focusListener; |
| 11 | + late JSFunction _blurListener; |
| 12 | + |
11 | 13 | @override |
12 | 14 | void addObserver(WidgetsBindingObserver state) { |
13 | | - window.addEventListener('focus', (event) => onFocus(event, state)); |
14 | | - window.addEventListener('blur', (event) => onBlur(event, state)); |
15 | | - } |
| 15 | + _focusListener = ((web.Event _) { |
| 16 | + state.didChangeAppLifecycleState(AppLifecycleState.resumed); |
| 17 | + }).toJS; |
16 | 18 |
|
17 | | - @override |
18 | | - void removeObserver(WidgetsBindingObserver state) { |
19 | | - window.removeEventListener('focus', (event) => onFocus(event, state)); |
20 | | - window.removeEventListener('blur', (event) => onBlur(event, state)); |
21 | | - } |
| 19 | + _blurListener = ((web.Event _) { |
| 20 | + state.didChangeAppLifecycleState(AppLifecycleState.paused); |
| 21 | + }).toJS; |
22 | 22 |
|
23 | | - void onFocus(Event e, WidgetsBindingObserver state) { |
24 | | - state.didChangeAppLifecycleState(AppLifecycleState.resumed); |
| 23 | + web.window.addEventListener('focus', _focusListener); |
| 24 | + web.window.addEventListener('blur', _blurListener); |
25 | 25 | } |
26 | 26 |
|
27 | | - void onBlur(Event e, WidgetsBindingObserver state) { |
28 | | - state.didChangeAppLifecycleState(AppLifecycleState.paused); |
| 27 | + @override |
| 28 | + void removeObserver(WidgetsBindingObserver state) { |
| 29 | + web.window.removeEventListener('focus', _focusListener); |
| 30 | + web.window.removeEventListener('blur', _blurListener); |
29 | 31 | } |
30 | 32 | } |
0 commit comments