提交 59f80b09 authored 作者: Kevin's avatar Kevin

修复收起虚拟键盘报错

修复因空安全导致的报错
上级 2acdf3d1
## [1.0.2]
* TODO: 修复收起虚拟键盘报错
* TODO: 修复因空安全导致的报错
## [1.0.1] ## [1.0.1]
* TODO: 修复警告错误 * TODO: 修复警告错误
......
...@@ -9,7 +9,7 @@ Usage Add this to your package's pubspec.yaml file: ...@@ -9,7 +9,7 @@ Usage Add this to your package's pubspec.yaml file:
Flutter >=2.0 Flutter >=2.0
``` yaml ``` yaml
dependencies: dependencies:
cool_ui: "^1.0.1" cool_ui: "^1.0.2"
``` ```
Flutter >=1.17 Flutter >=1.17
......
...@@ -31,104 +31,109 @@ class CoolKeyboard { ...@@ -31,104 +31,109 @@ class CoolKeyboard {
if (isInterceptor) return; if (isInterceptor) return;
isInterceptor = true; isInterceptor = true;
ServicesBinding.instance!.defaultBinaryMessenger ServicesBinding.instance!.defaultBinaryMessenger
.setMockMessageHandler("flutter/textinput", (ByteData? data) async { .setMockMessageHandler("flutter/textinput", _textInputHanlde);
var methodCall = _codec.decodeMethodCall(data); }
switch (methodCall.method) {
case 'TextInput.show': static Future<ByteData?> _textInputHanlde(ByteData? data) async {
if (_currentKeyboard != null) { var methodCall = _codec.decodeMethodCall(data);
if (clearTask != null) { switch (methodCall.method) {
clearTask!.cancel(); case 'TextInput.show':
clearTask = null; if (_currentKeyboard != null) {
} if (clearTask != null) {
openKeyboard(); clearTask!.cancel();
return _codec.encodeSuccessEnvelope(null); clearTask = null;
} else {
if (data != null) {
return await _sendPlatformMessage("flutter/textinput", data);
}
}
break;
case 'TextInput.hide':
if (_currentKeyboard != null) {
if (clearTask == null) {
clearTask = new Timer(Duration(milliseconds: 16),
() => hideKeyboard(animation: true));
}
return _codec.encodeSuccessEnvelope(null);
} else {
if (data != null) {
return await _sendPlatformMessage("flutter/textinput", data);
}
} }
break; openKeyboard();
case 'TextInput.setEditingState': return _codec.encodeSuccessEnvelope(null);
var editingState = TextEditingValue.fromJSON(methodCall.arguments); } else {
if (_keyboardController != null) { if (data != null) {
_keyboardController!.value = editingState; return await _sendPlatformMessage("flutter/textinput", data);
return _codec.encodeSuccessEnvelope(null);
} }
break; }
case 'TextInput.clearClient': break;
case 'TextInput.hide':
if (_currentKeyboard != null) {
if (clearTask == null) { if (clearTask == null) {
clearTask = new Timer(Duration(milliseconds: 16), clearTask = new Timer(Duration(milliseconds: 16),
() => hideKeyboard(animation: true)); () => hideKeyboard(animation: true));
} }
clearKeyboard(); return _codec.encodeSuccessEnvelope(null);
break; } else {
case 'TextInput.setClient': if (data != null) {
var setInputType = methodCall.arguments[1]['inputType']; return await _sendPlatformMessage("flutter/textinput", data);
InputClient? client; }
_keyboards.forEach((inputType, keyboardConfig) { }
if (inputType.name == setInputType['name']) { break;
client = InputClient.fromJSON(methodCall.arguments); case 'TextInput.setEditingState':
var editingState = TextEditingValue.fromJSON(methodCall.arguments);
_keyboardParam = if (_keyboardController != null) {
(client!.configuration.inputType as CKTextInputType).params; _keyboardController!.value = editingState;
return _codec.encodeSuccessEnvelope(null);
clearKeyboard(); }
_currentKeyboard = keyboardConfig; break;
_keyboardController = KeyboardController(client: client!) case 'TextInput.clearClient':
..addListener(() { var isShow = _currentKeyboard != null;
var callbackMethodCall = MethodCall( if (clearTask == null) {
"TextInputClient.updateEditingState", [ clearTask = new Timer(
_keyboardController!.client.connectionId, Duration(milliseconds: 16), () => hideKeyboard(animation: true));
_keyboardController!.value.toJSON() }
]); clearKeyboard();
ServicesBinding.instance!.defaultBinaryMessenger if (isShow) {
.handlePlatformMessage( return _codec.encodeSuccessEnvelope(null);
"flutter/textinput", }
_codec.encodeMethodCall(callbackMethodCall), break;
(data) {}); case 'TextInput.setClient':
}); var setInputType = methodCall.arguments[1]['inputType'];
if (_pageKey != null) { InputClient? client;
_pageKey!.currentState?.update(); _keyboards.forEach((inputType, keyboardConfig) {
} if (inputType.name == setInputType['name']) {
} client = InputClient.fromJSON(methodCall.arguments);
});
_keyboardParam =
if (client != null) { (client!.configuration.inputType as CKTextInputType).params;
await _sendPlatformMessage("flutter/textinput",
_codec.encodeMethodCall(MethodCall('TextInput.hide')));
return _codec.encodeSuccessEnvelope(null);
} else {
if (clearTask == null) {
hideKeyboard(animation: false);
}
clearKeyboard(); clearKeyboard();
_currentKeyboard = keyboardConfig;
_keyboardController = KeyboardController(client: client!)
..addListener(() {
var callbackMethodCall = MethodCall(
"TextInputClient.updateEditingState", [
_keyboardController!.client.connectionId,
_keyboardController!.value.toJSON()
]);
ServicesBinding.instance!.defaultBinaryMessenger
.handlePlatformMessage("flutter/textinput",
_codec.encodeMethodCall(callbackMethodCall), (data) {});
});
if (_pageKey != null) {
_pageKey!.currentState?.update();
}
} }
break; });
}
if (data != null) { if (client != null) {
ByteData response = await _sendPlatformMessage("flutter/textinput", data); await _sendPlatformMessage("flutter/textinput",
return response; _codec.encodeMethodCall(MethodCall('TextInput.hide')));
} return _codec.encodeSuccessEnvelope(null);
return null; } else {
}); if (clearTask == null) {
hideKeyboard(animation: false);
}
clearKeyboard();
}
// break;
}
if (data != null) {
ByteData? response =
await _sendPlatformMessage("flutter/textinput", data);
return response;
}
return null;
} }
static Future<ByteData> _sendPlatformMessage( static Future<ByteData?> _sendPlatformMessage(
String channel, ByteData message) { String channel, ByteData message) {
final Completer<ByteData> completer = Completer<ByteData>(); final Completer<ByteData?> completer = Completer<ByteData?>();
ui.window.sendPlatformMessage(channel, message, (ByteData? reply) { ui.window.sendPlatformMessage(channel, message, (ByteData? reply) {
try { try {
completer.complete(reply); completer.complete(reply);
...@@ -161,12 +166,12 @@ class CoolKeyboard { ...@@ -161,12 +166,12 @@ class CoolKeyboard {
var tempKey = _pageKey; var tempKey = _pageKey;
_root!.setKeyboard((ctx) { _root!.setKeyboard((ctx) {
if (_currentKeyboard != null && _keyboardHeightNotifier.value != null) { if (_currentKeyboard != null && _keyboardHeightNotifier.value != 0) {
return KeyboardPage( return KeyboardPage(
key: tempKey, key: tempKey,
builder: (ctx) { builder: (ctx) {
return _currentKeyboard!.builder( return _currentKeyboard!
ctx, _keyboardController!, _keyboardParam); .builder(ctx, _keyboardController!, _keyboardParam);
}, },
height: _keyboardHeightNotifier.value); height: _keyboardHeightNotifier.value);
} else { } else {
...@@ -174,7 +179,7 @@ class CoolKeyboard { ...@@ -174,7 +179,7 @@ class CoolKeyboard {
} }
}); });
BackButtonInterceptor.add((_, _2) { BackButtonInterceptor.add((_, __) {
CoolKeyboard.sendPerformAction(TextInputAction.done); CoolKeyboard.sendPerformAction(TextInputAction.done);
return true; return true;
}, zIndex: 1, name: 'CustomKeyboard'); }, zIndex: 1, name: 'CustomKeyboard');
...@@ -229,8 +234,10 @@ class CoolKeyboard { ...@@ -229,8 +234,10 @@ class CoolKeyboard {
static sendPerformAction(TextInputAction action) { static sendPerformAction(TextInputAction action) {
var callbackMethodCall = MethodCall("TextInputClient.performAction", var callbackMethodCall = MethodCall("TextInputClient.performAction",
[_keyboardController!.client.connectionId, action.toString()]); [_keyboardController!.client.connectionId, action.toString()]);
ServicesBinding.instance!.defaultBinaryMessenger.handlePlatformMessage("flutter/textinput", ServicesBinding.instance!.defaultBinaryMessenger.handlePlatformMessage(
_codec.encodeMethodCall(callbackMethodCall), (data) {}); "flutter/textinput",
_codec.encodeMethodCall(callbackMethodCall),
(data) {});
} }
static updateKeyboardHeight() { static updateKeyboardHeight() {
...@@ -245,13 +252,13 @@ class CoolKeyboard { ...@@ -245,13 +252,13 @@ class CoolKeyboard {
class KeyboardConfig { class KeyboardConfig {
final KeyboardBuilder builder; final KeyboardBuilder builder;
final GetKeyboardHeight getHeight; final GetKeyboardHeight getHeight;
const KeyboardConfig({required this.builder,required this.getHeight}); const KeyboardConfig({required this.builder, required this.getHeight});
} }
class InputClient { class InputClient {
final int connectionId; final int connectionId;
final TextInputConfiguration configuration; final TextInputConfiguration configuration;
const InputClient({required this.connectionId,required this.configuration}); const InputClient({required this.connectionId, required this.configuration});
factory InputClient.fromJSON(List<dynamic> encoded) { factory InputClient.fromJSON(List<dynamic> encoded) {
return InputClient( return InputClient(
...@@ -331,7 +338,8 @@ class CKTextInputType extends TextInputType { ...@@ -331,7 +338,8 @@ class CKTextInputType extends TextInputType {
final String name; final String name;
final String? params; final String? params;
const CKTextInputType({required this.name, bool? signed, bool? decimal, this.params}) const CKTextInputType(
{required this.name, bool? signed, bool? decimal, this.params})
: super.numberWithOptions(signed: signed, decimal: decimal); : super.numberWithOptions(signed: signed, decimal: decimal);
@override @override
...@@ -376,7 +384,8 @@ class CKTextInputType extends TextInputType { ...@@ -376,7 +384,8 @@ class CKTextInputType extends TextInputType {
class KeyboardPage extends StatefulWidget { class KeyboardPage extends StatefulWidget {
final WidgetBuilder builder; final WidgetBuilder builder;
final double height; final double height;
const KeyboardPage({required this.builder, this.height = 0, Key? key}) : super(key: key); const KeyboardPage({required this.builder, this.height = 0, Key? key})
: super(key: key);
@override @override
State<StatefulWidget> createState() => KeyboardPageState(); State<StatefulWidget> createState() => KeyboardPageState();
......
name: cool_ui name: cool_ui
description: Some practical Widget for flutter,Popover,Weui,Custom Keyboard description: Some practical Widget for flutter,Popover,Weui,Custom Keyboard
version: 1.0.1 version: 1.0.2
author: Kevin <liangkaikevin@gmail.com> author: Kevin <liangkaikevin@gmail.com>
homepage: https://github.com/Im-Kevin/cool_ui homepage: https://github.com/Im-Kevin/cool_ui
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论