-
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrate to package:web and implement keyboard detection
- Loading branch information
1 parent
42ddef5
commit ad25e70
Showing
2 changed files
with
33 additions
and
11 deletions.
There are no files selected for viewing
37 changes: 29 additions & 8 deletions
37
flutter_keyboard_visibility_web/lib/flutter_keyboard_visibility_web.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,46 @@ | ||
import 'dart:html' as html show window, Navigator; | ||
import 'dart:async'; | ||
import 'package:web/web.dart' as html; | ||
import 'package:flutter_keyboard_visibility_platform_interface/flutter_keyboard_visibility_platform_interface.dart'; | ||
import 'package:flutter_web_plugins/flutter_web_plugins.dart'; | ||
|
||
/// The web implementation of the [FlutterKeyboardVisibilityPlatform] of the | ||
/// FlutterKeyboardVisibility plugin. | ||
class FlutterKeyboardVisibilityPluginWeb | ||
extends FlutterKeyboardVisibilityPlatform { | ||
class FlutterKeyboardVisibilityPluginWeb extends FlutterKeyboardVisibilityPlatform { | ||
/// Constructs a [FlutterKeyboardVisibilityPluginWeb]. | ||
FlutterKeyboardVisibilityPluginWeb(html.Navigator navigator); | ||
|
||
static final _onChangeController = StreamController<bool>(); | ||
static final _onChange = _onChangeController.stream.asBroadcastStream(); | ||
|
||
/// Factory method that initializes the FlutterKeyboardVisibility plugin | ||
/// platform with an instance of the plugin for the web. | ||
static void registerWith(Registrar registrar) { | ||
FlutterKeyboardVisibilityPlatform.instance = | ||
FlutterKeyboardVisibilityPluginWeb(html.window.navigator); | ||
FlutterKeyboardVisibilityPlatform.instance = FlutterKeyboardVisibilityPluginWeb(html.window.navigator); | ||
html.EventStreamProvider<html.Event>('resize').forTarget(html.window.visualViewport).listen((e) { | ||
final minKeyboardHeight = 200; | ||
|
||
final screenHeight = html.window.screen.height; | ||
final viewportHeight = html.window.visualViewport?.height.toInt() ?? 0; | ||
final newState = (screenHeight - minKeyboardHeight) > viewportHeight; | ||
_updateValue(newState); | ||
}); | ||
} | ||
|
||
static bool get isVisible => _isVisible; | ||
static bool _isVisible = false; | ||
|
||
static void _updateValue(bool newValue) { | ||
// Don't report the same value multiple times | ||
if (newValue == _isVisible) { | ||
return; | ||
} | ||
|
||
_isVisible = newValue; | ||
_onChangeController.add(newValue); | ||
} | ||
|
||
/// Emits changes to keyboard visibility from the platform. Web is not | ||
/// implemented yet so false is returned. | ||
@override | ||
Stream<bool> get onChange async* { | ||
yield false; | ||
} | ||
Stream<bool> get onChange => _onChange; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters