diff --git a/packages/flutter/lib/src/material/input.dart b/packages/flutter/lib/src/material/input.dart
index 89d095e4ab..cad4757ab6 100644
--- a/packages/flutter/lib/src/material/input.dart
+++ b/packages/flutter/lib/src/material/input.dart
@@ -80,7 +80,7 @@ const Curve _kTransitionCurve = Curves.ease;
class _InputState extends State {
String _value;
EditableString _editableString;
- KeyboardHandle _keyboardHandle = KeyboardHandle.unattached;
+ KeyboardHandle _keyboardHandle;
// Used by tests.
EditableString get editableValue => _editableString;
@@ -96,19 +96,22 @@ class _InputState extends State {
}
void dispose() {
- if (_keyboardHandle.attached)
+ if (_isAttachedToKeyboard)
_keyboardHandle.release();
super.dispose();
}
+ bool get _isAttachedToKeyboard => _keyboardHandle != null && _keyboardHandle.attached;
+
void _attachOrDetachKeyboard(bool focused) {
- if (focused && !_keyboardHandle.attached) {
- _keyboardHandle = keyboard.show(_editableString.stub, config.keyboardType);
+ if (focused && !_isAttachedToKeyboard) {
+ _keyboardHandle = keyboard.show(_editableString.createStub(), config.keyboardType);
_keyboardHandle.setText(_editableString.text);
_keyboardHandle.setSelection(_editableString.selection.start,
_editableString.selection.end);
- } else if (!focused && _keyboardHandle.attached) {
+ } else if (!focused && _isAttachedToKeyboard) {
_keyboardHandle.release();
+ _keyboardHandle = null;
_editableString.didDetachKeyboard();
}
}
@@ -251,7 +254,7 @@ class _InputState extends State {
behavior: HitTestBehavior.opaque,
onTap: () {
if (Focus.at(context)) {
- assert(_keyboardHandle.attached);
+ assert(_isAttachedToKeyboard);
_keyboardHandle.showByRequest();
} else {
Focus.moveTo(config.key);
diff --git a/packages/flutter/lib/src/services/keyboard.dart b/packages/flutter/lib/src/services/keyboard.dart
index 8e82e3f0c5..8e58165ef4 100644
--- a/packages/flutter/lib/src/services/keyboard.dart
+++ b/packages/flutter/lib/src/services/keyboard.dart
@@ -10,19 +10,6 @@ import 'shell.dart';
export 'package:mojo_services/keyboard/keyboard.mojom.dart';
-class _KeyboardConnection {
-
- _KeyboardConnection() {
- proxy = new KeyboardServiceProxy.unbound();
- shell.connectToService(null, proxy);
- }
-
- KeyboardServiceProxy proxy;
- KeyboardService get keyboardService => proxy.ptr;
-
- static final _KeyboardConnection instance = new _KeyboardConnection();
-}
-
/// An interface to the system's keyboard.
///
/// Most clients will want to use the [keyboard] singleton instance.
@@ -40,11 +27,8 @@ class Keyboard {
KeyboardHandle show(KeyboardClientStub stub, KeyboardType keyboardType) {
assert(stub != null);
- if (_currentHandle != null) {
- if (_currentHandle.stub == stub)
- return _currentHandle;
- _currentHandle.release();
- }
+ _currentHandle?.release();
+ assert(_currentHandle == null);
_currentHandle = new KeyboardHandle._show(this, stub, keyboardType);
return _currentHandle;
}
@@ -67,16 +51,12 @@ class Keyboard {
class KeyboardHandle {
- KeyboardHandle._show(Keyboard keyboard, this.stub, KeyboardType keyboardType) : _keyboard = keyboard {
+ KeyboardHandle._show(Keyboard keyboard, KeyboardClientStub stub, KeyboardType keyboardType) : _keyboard = keyboard {
_keyboard.service.show(stub, keyboardType);
_attached = true;
}
- KeyboardHandle._unattached(Keyboard keyboard) : _keyboard = keyboard, stub = null, _attached = false;
- static final unattached = new KeyboardHandle._unattached(keyboard);
-
final Keyboard _keyboard;
- final KeyboardClientStub stub;
bool _attached;
bool get attached => _attached;
@@ -111,4 +91,11 @@ class KeyboardHandle {
}
-final Keyboard keyboard = new Keyboard(_KeyboardConnection.instance.keyboardService);
+KeyboardServiceProxy _initKeyboardProxy() {
+ KeyboardServiceProxy proxy = new KeyboardServiceProxy.unbound();
+ shell.connectToService(null, proxy);
+ return proxy;
+}
+
+final KeyboardServiceProxy _keyboardProxy = _initKeyboardProxy();
+final Keyboard keyboard = new Keyboard(_keyboardProxy.ptr);
diff --git a/packages/flutter/lib/src/widgets/editable.dart b/packages/flutter/lib/src/widgets/editable.dart
index dd573d5a0a..4b5edbdb58 100644
--- a/packages/flutter/lib/src/widgets/editable.dart
+++ b/packages/flutter/lib/src/widgets/editable.dart
@@ -64,7 +64,6 @@ class _KeyboardClientImpl implements KeyboardClient {
}) {
assert(onUpdated != null);
assert(onSubmitted != null);
- stub = new KeyboardClientStub.unbound()..impl = this;
selection = new TextRange(start: text.length, end: text.length);
}
@@ -84,7 +83,9 @@ class _KeyboardClientImpl implements KeyboardClient {
TextRange selection = TextRange.empty;
/// A keyboard client stub that can be attached to a keyboard service.
- KeyboardClientStub stub;
+ KeyboardClientStub createStub() {
+ return new KeyboardClientStub.unbound()..impl = this;
+ }
void _delete(TextRange range) {
if (range.isCollapsed || !range.isValid) return;
@@ -196,7 +197,7 @@ class EditableString {
/// A keyboard client stub that can be attached to a keyboard service.
///
/// See [Keyboard].
- KeyboardClientStub get stub => _client.stub;
+ KeyboardClientStub createStub() => _client.createStub();
void didDetachKeyboard() {
_client.composing = TextRange.empty;