android - 如何构建 flutter 键盘?

标签 android ios flutter dart keyboard

我可以从头开始构建一个全新的键盘,就像 GO 键盘或任何其他键盘一样,可以在 android 和 ios 上工作,甚至只是使用 flutter 来运行 android 吗?

最佳答案

您可以使用包https://pub.dev/packages/keyboard_actions
示例代码https://github.com/diegoveloper/flutter_keyboard_actions/tree/master/example
彩色按钮键盘和自定义数字键盘的示例演示
enter image description here

示例代码 fragment

KeyboardCustomInput<Color>(
                focusNode: _nodeText9,
                height: 65,
                notifier: custom2Notifier,
                builder: (context, val, hasFocus) {
                  return Container(
                    width: double.maxFinite,
                    color: val ?? Colors.transparent,
                  );
                },
              ),
              KeyboardCustomInput<String>(
                focusNode: _nodeText10,
                height: 65,
                notifier: custom3Notifier,
                builder: (context, val, hasFocus) {
                  return Container(
                    alignment: Alignment.center,
                    child: Text(
                      val.isEmpty ? "Tap Here" : val,
                      style:
                          TextStyle(fontSize: 25, fontWeight: FontWeight.w500),
                    ),
                  );
                },
              ),


class NumericKeyboard extends StatelessWidget
    with KeyboardCustomPanelMixin<String>
    implements PreferredSizeWidget {
  final ValueNotifier<String> notifier;
  final FocusNode focusNode;

  NumericKeyboard({
    Key key,
    this.notifier,
    this.focusNode,
  }) : super(key: key);

  @override
  Size get preferredSize => Size.fromHeight(280);

  final format = NumberFormat("0000");

  String _formatValue(String value) {
    final updatedValue = format.format(double.parse(value));
    final finalValue = updatedValue.substring(0, updatedValue.length - 2) +
        "." +
        updatedValue.substring(updatedValue.length - 2, updatedValue.length);
    return finalValue;
  }

  void _onTapNumber(String value) {
    if (value == "Done") {
      focusNode.unfocus();
      return;
    }
    final currentValue = notifier.value.replaceAll(".", "");
    final temp = currentValue + value;
    updateValue(_formatValue(temp));
  }

  void _onTapBackspace() {
    final currentValue = notifier.value.replaceAll(".", "");
    final temp = currentValue.substring(0, currentValue.length - 1);
    updateValue(_formatValue(temp));
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: preferredSize.height,
      color: Color(0xFF313131),
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: GridView(
          shrinkWrap: true,
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 3,
            childAspectRatio: 2.2,
            crossAxisSpacing: 10,
            mainAxisSpacing: 10,
          ),
          children: [
            _buildButton(text: "7"),
            _buildButton(text: "8"),
            _buildButton(text: "9"),
            _buildButton(text: "4"),
            _buildButton(text: "5"),
            _buildButton(text: "6"),
            _buildButton(text: "1"),
            _buildButton(text: "2"),
            _buildButton(text: "3"),
            _buildButton(icon: Icons.backspace, color: Colors.black),
            _buildButton(text: "0"),
            _buildButton(text: "Done", color: Colors.black),
          ],
        ),
      ),
    );
  }

  Widget _buildButton({
    String text,
    IconData icon,
    Color color,
  }) =>
      NumericButton(
        text: text,
        icon: icon,
        color: color,
        onTap: () => icon != null ? _onTapBackspace() : _onTapNumber(text),
      );
}

关于android - 如何构建 flutter 键盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59619648/

相关文章:

java - 在 if 语句中读取字符串时遇到问题

flutter - 相同的StreamBuilders处于波动状态,一个正在工作而其他返回null

java - Android 应用程序 - 可以修改 Constants.java 中的字符串吗?

java - VideoBitmapDecoder 不接受 int (Android-Glide)

ios - 从另一个类获取值后 NSInteger 保持为 null

ios - 你如何在 Swift 中解析字典数组?

dart - 如何在 flutter 中沿 Z 轴旋转图像?

flutter - 方法 'bloc' 没有为类型 'BuildContext' 定义

java - Android 上的 Http Url 连接问题抛出 java.lang.IllegalStateException : Already connected

ios - 如何取消正在进行的所有触摸操作?