提交 ecd2f383 authored 作者: Kevin's avatar Kevin

重构了键盘弹出的方式

修复长按输入框报错问题 升级0.6.0
上级 6f8911ed
## [0.6.0]
* TODO: 重构了键盘弹出的方式,修复长按输入框报错问题
## [0.5.5] ## [0.5.5]
* TODO: Popover添加了阴影效果 * TODO: Popover添加了阴影效果
* TODO: Popover 添加了方向 * TODO: Popover 添加了方向
......
...@@ -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 >=1.17 Flutter >=1.17
``` yaml ``` yaml
dependencies: dependencies:
cool_ui: "^0.5.4" cool_ui: "^0.6.0"
``` ```
Flutter >=1.7 Flutter >=1.7
......
...@@ -8,8 +8,8 @@ class CoolKeyboard { ...@@ -8,8 +8,8 @@ class CoolKeyboard {
static JSONMethodCodec _codec = const JSONMethodCodec(); static JSONMethodCodec _codec = const JSONMethodCodec();
static KeyboardConfig _currentKeyboard; static KeyboardConfig _currentKeyboard;
static Map<CKTextInputType, KeyboardConfig> _keyboards = {}; static Map<CKTextInputType, KeyboardConfig> _keyboards = {};
static KeyboardRootState _root;
static BuildContext _context; static BuildContext _context;
static OverlayEntry _keyboardEntry;
static KeyboardController _keyboardController; static KeyboardController _keyboardController;
static GlobalKey<KeyboardPageState> _pageKey; static GlobalKey<KeyboardPageState> _pageKey;
static bool isInterceptor = false; static bool isInterceptor = false;
...@@ -21,7 +21,8 @@ class CoolKeyboard { ...@@ -21,7 +21,8 @@ class CoolKeyboard {
static Timer clearTask; static Timer clearTask;
static init(BuildContext context) { static init(KeyboardRootState root, BuildContext context) {
_root = root;
_context = context; _context = context;
interceptorInput(); interceptorInput();
} }
...@@ -47,8 +48,9 @@ class CoolKeyboard { ...@@ -47,8 +48,9 @@ class CoolKeyboard {
break; break;
case 'TextInput.hide': case 'TextInput.hide':
if (_currentKeyboard != null) { if (_currentKeyboard != null) {
if(clearTask == null){ if (clearTask == null) {
clearTask = new Timer(Duration(milliseconds: 16), ()=>hideKeyboard(animation: true)); clearTask = new Timer(Duration(milliseconds: 16),
() => hideKeyboard(animation: true));
} }
return _codec.encodeSuccessEnvelope(null); return _codec.encodeSuccessEnvelope(null);
} else { } else {
...@@ -63,8 +65,9 @@ class CoolKeyboard { ...@@ -63,8 +65,9 @@ class CoolKeyboard {
} }
break; break;
case 'TextInput.clearClient': case 'TextInput.clearClient':
if(clearTask == null){ if (clearTask == null) {
clearTask = new Timer(Duration(milliseconds: 16), ()=>hideKeyboard(animation: true)); clearTask = new Timer(Duration(milliseconds: 16),
() => hideKeyboard(animation: true));
} }
clearKeyboard(); clearKeyboard();
break; break;
...@@ -104,7 +107,7 @@ class CoolKeyboard { ...@@ -104,7 +107,7 @@ class CoolKeyboard {
_codec.encodeMethodCall(MethodCall('TextInput.hide'))); _codec.encodeMethodCall(MethodCall('TextInput.hide')));
return _codec.encodeSuccessEnvelope(null); return _codec.encodeSuccessEnvelope(null);
} else { } else {
if(clearTask == null){ if (clearTask == null) {
hideKeyboard(animation: false); hideKeyboard(animation: false);
} }
clearKeyboard(); clearKeyboard();
...@@ -142,7 +145,7 @@ class CoolKeyboard { ...@@ -142,7 +145,7 @@ class CoolKeyboard {
static openKeyboard() { static openKeyboard() {
var keyboardHeight = _currentKeyboard.getHeight(_context); var keyboardHeight = _currentKeyboard.getHeight(_context);
_keyboardHeightNotifier.value = keyboardHeight; _keyboardHeightNotifier.value = keyboardHeight;
if (_keyboardEntry != null && _pageKey != null) return; if (_root.hasKeyboard && _pageKey != null) return;
_pageKey = GlobalKey<KeyboardPageState>(); _pageKey = GlobalKey<KeyboardPageState>();
// KeyboardMediaQueryState queryState = _context // KeyboardMediaQueryState queryState = _context
// .ancestorStateOfType(const TypeMatcher<KeyboardMediaQueryState>()) // .ancestorStateOfType(const TypeMatcher<KeyboardMediaQueryState>())
...@@ -150,7 +153,7 @@ class CoolKeyboard { ...@@ -150,7 +153,7 @@ class CoolKeyboard {
// queryState.update(); // queryState.update();
var tempKey = _pageKey; var tempKey = _pageKey;
_keyboardEntry = OverlayEntry(builder: (ctx) { _root.setKeyboard((ctx) {
if (_currentKeyboard != null && _keyboardHeightNotifier.value != null) { if (_currentKeyboard != null && _keyboardHeightNotifier.value != null) {
return KeyboardPage( return KeyboardPage(
key: tempKey, key: tempKey,
...@@ -164,8 +167,6 @@ class CoolKeyboard { ...@@ -164,8 +167,6 @@ class CoolKeyboard {
} }
}); });
Overlay.of(_context).insert(_keyboardEntry);
BackButtonInterceptor.add((_) { BackButtonInterceptor.add((_) {
CoolKeyboard.sendPerformAction(TextInputAction.done); CoolKeyboard.sendPerformAction(TextInputAction.done);
return true; return true;
...@@ -180,29 +181,24 @@ class CoolKeyboard { ...@@ -180,29 +181,24 @@ class CoolKeyboard {
clearTask = null; clearTask = null;
} }
BackButtonInterceptor.removeByName('CustomKeyboard'); BackButtonInterceptor.removeByName('CustomKeyboard');
if (_keyboardEntry != null && _pageKey != null) { if (_root.hasKeyboard && _pageKey != null) {
// _pageKey.currentState.animationController // _pageKey.currentState.animationController
// .addStatusListener((AnimationStatus status) { // .addStatusListener((AnimationStatus status) {
// if (status == AnimationStatus.dismissed || // if (status == AnimationStatus.dismissed ||
// status == AnimationStatus.completed) { // status == AnimationStatus.completed) {
// if (_keyboardEntry != null) { // if (_root.hasKeyboard) {
// _keyboardEntry.remove(); // _keyboardEntry.remove();
// _keyboardEntry = null; // _keyboardEntry = null;
// } // }
// } // }
// }); // });
if (animation) { if (animation) {
var keyboardEntry = _keyboardEntry;
_keyboardEntry = null;
_pageKey.currentState.exitKeyboard(); _pageKey.currentState.exitKeyboard();
Future.delayed(Duration(milliseconds: 116)).then((_) { Future.delayed(Duration(milliseconds: 116)).then((_) {
if (keyboardEntry != null) { _root.clearKeyboard();
keyboardEntry.remove();
}
}); });
} else { } else {
_keyboardEntry.remove(); _root.clearKeyboard();
_keyboardEntry = null;
} }
} }
_pageKey = null; _pageKey = null;
...@@ -231,7 +227,9 @@ class CoolKeyboard { ...@@ -231,7 +227,9 @@ class CoolKeyboard {
} }
static updateKeyboardHeight() { static updateKeyboardHeight() {
if (_pageKey != null && _pageKey.currentState != null && clearTask == null) { if (_pageKey != null &&
_pageKey.currentState != null &&
clearTask == null) {
_pageKey.currentState.updateHeight(_keyboardHeightNotifier.value); _pageKey.currentState.updateHeight(_keyboardHeightNotifier.value);
} }
} }
......
...@@ -2,10 +2,13 @@ part of cool_ui; ...@@ -2,10 +2,13 @@ part of cool_ui;
class KeyboardRootWidget extends StatefulWidget { class KeyboardRootWidget extends StatefulWidget {
final Widget child; final Widget child;
/// The text direction for this subtree. /// The text direction for this subtree.
final TextDirection textDirection; final TextDirection textDirection;
const KeyboardRootWidget({Key key, this.child, this.textDirection = TextDirection.ltr}) : super(key: key); const KeyboardRootWidget(
{Key key, this.child, this.textDirection = TextDirection.ltr})
: super(key: key);
@override @override
State<StatefulWidget> createState() { State<StatefulWidget> createState() {
...@@ -15,30 +18,46 @@ class KeyboardRootWidget extends StatefulWidget { ...@@ -15,30 +18,46 @@ class KeyboardRootWidget extends StatefulWidget {
} }
class KeyboardRootState extends State<KeyboardRootWidget> { class KeyboardRootState extends State<KeyboardRootWidget> {
List<OverlayEntry> _initialEntries = []; WidgetBuilder _keyboardbuilder;
bool get hasKeyboard => _keyboardbuilder != null;
// List<OverlayEntry> _initialEntries = [];
@override @override
void initState() { void initState() {
super.initState(); super.initState();
// _initialEntries.add(this.initChild());
_initialEntries.add(this.initChild());
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
// TODO: implement build // TODO: implement build
return Directionality( return KeyboardMediaQuery(child: Builder(builder: (context) {
textDirection: widget.textDirection, CoolKeyboard.init(this, context);
child: KeyboardMediaQuery(
child: Overlay( List<Widget> children = [widget.child];
initialEntries: _initialEntries, if (_keyboardbuilder != null) {
))); children.add(Builder(
builder: _keyboardbuilder,
));
}
return Directionality(
textDirection: widget.textDirection,
child: Stack(
children: children,
));
}));
}
setKeyboard(WidgetBuilder keyboardbuilder) {
this._keyboardbuilder = keyboardbuilder;
setState(() {});
} }
OverlayEntry initChild() { clearKeyboard() {
return OverlayEntry(builder: (ctx) { if (this._keyboardbuilder != null) {
CoolKeyboard.init(ctx); this._keyboardbuilder = null;
return widget.child; setState(() {});
}); }
} }
} }
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: 0.5.5 version: 0.6.0
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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论