颤振自定义键盘("假"软键盘)



我正在开发一个金融应用程序,我想要一个自定义的文本输入字段和带有内置计算器的货币输入键盘。

我尝试过使用BottomSheet,既有持久的,也有模态的。模态行为是理想的,但它总是显示出障碍。持久的是我现在拥有的,使用焦点节点来显示和隐藏它,但它会抛出奇怪的错误:

I/flutter (30319): The following NoSuchMethodError was thrown while dispatching notifications for FocusNode:
I/flutter (30319): The method 'removeLocalHistoryEntry' was called on null.
I/flutter (30319): Receiver: null
I/flutter (30319): Tried calling: removeLocalHistoryEntry(Instance of 'LocalHistoryEntry')
I/flutter (30319):
I/flutter (30319): When the exception was thrown, this was the stack:
I/flutter (30319): #0      Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:46:5)
I/flutter (30319): #1      LocalHistoryEntry.remove (package:flutter/src/widgets/routes.dart:296:12)
I/flutter (30319): #2      _NumpadFieldState.initState.<anonymous closure> (file:///D:/code/financepie/lib/widgets/numpad/numpadfield.dart:30:32)
...

无论如何,底层行为(向下拖动(对于复制android/ios软键盘来说并不理想。有更好的解决方案吗?下面的当前代码:

import 'package:flutter/material.dart';
import 'numpad.dart';
class NumpadField extends StatefulWidget {
@override
_NumpadFieldState createState() {
return new _NumpadFieldState();
}
}
class _NumpadFieldState extends State<NumpadField> {
ValueNotifier<List<String>> state;
FocusNode focusNode;
PersistentBottomSheetController bottomSheetController;
@override initState() {
super.initState();
state = ValueNotifier<List<String>>([]);
state.addListener(() => setState((){}));
focusNode = FocusNode();
focusNode.addListener(() {
print(focusNode);
if (focusNode.hasFocus) {
bottomSheetController = showBottomSheet(
context: context,
builder: (context) => Numpad(state: state),
);
} else {
bottomSheetController?.close(); ///this line causing the error
}
}); 
}
@override dispose() {
state.dispose();
focusNode.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
FocusScope.of(context).requestFocus(focusNode);
},
child: Container(
child: Text(state.value.fold<String>("", (str, e) => "$str $e")),
constraints: BoxConstraints.expand(height: 24.0),
decoration: BoxDecoration(
border: BorderDirectional(bottom: BorderSide())
),
),
);
}
}

bottomSheetController?.close();可以为null。由于该行导致错误,您可以添加一个空检查来防止此问题。

else {
if(bottomSheetController != null)
bottomSheetController!.close();
}