Flutter自定义键盘(“ Fake”软键盘)

时间:2018-07-27 22:37:37

标签: android ios keyboard dart flutter

我正在开发一个财务应用程序,我想要一个自定义的textentry字段和一个键盘,用于使用内置计算器输入货币。

我尝试使用持久性和模态性的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())
        ),
      ),
    );
  }
}

0 个答案:

没有答案