android - Flutter 自定义键盘(“假”软键盘)
问题描述
我正在开发一个金融应用程序,我想要一个自定义文本输入字段和键盘,用于使用内置计算器输入货币。
我尝试过使用持久性和模态的 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();
可以为空。由于该行导致错误,您可以添加一个空检查来防止此问题。
else {
if(bottomSheetController != null)
bottomSheetController!.close();
}
推荐阅读
- r - 在使用 R Shiny 从查询中获取数据后尝试进行 selectInput
- sql-server - SQL Query - 如何通过检查当前行与所有行来获取配对行
- angular - 路由中的条件重定向到
- html - 当您在 css 中调整响应网格的大小时,一些图像会被隐藏
- sql - 在插入新记录之前检查值不存在于列中
- c++ - 你可以在同一个块中的互斥锁 lock_guard 之前有代码吗
- symfony - 如果条件为 fos_elastica.yml 配置文件中的弹性搜索索引
- rust - Rust:链接失败,退出代码 1181
- java - JOOQ MySQL 日期时间类型
- azure-functions - Azure 函数:从 c# 加载非托管 c++ dll,错误:“无法加载 DLL 或其依赖项之一”