android - 如何在文本字段 flutter 中设置虚拟键盘的数据

标签 android flutter dart keyboard flutter-layout

我如何将虚拟键盘而不是普通键盘的焦点放在textfiled上。
继承人的代码

import 'dart:async';
import 'package:android/GlobalVariables.dart' as globals;
import 'package:flutter/material.dart';
import 'package:localstorage/localstorage.dart';
import 'Confirm.dart';
import 'package:virtual_keyboard/virtual_keyboard.dart';
import 'package:keyboard_actions/keyboard_actions.dart';
import 'package:connectivity/connectivity.dart';
import 'package:flutter/services.dart';
import 'HomeScreen.dart';

    void bh(context) async {
      var connectivityResult = await (Connectivity().checkConnectivity());
      if (connectivityResult == ConnectivityResult.mobile) {
        print("Connected to Mobile Network");
      } else if (connectivityResult == ConnectivityResult.wifi) {
        print("Connected to WiFi");
      } else {
        showDialog(
            context: context,
            builder: (BuildContext context) {
              return AlertDialog(
                title: Center(
                  child: Text(
                    "Connectivity ",
                    style: TextStyle(fontSize: 15),
                  ),
                ),
                actions: <Widget>[
                  Center(
                      child: Text(
                    "Please Check your Internet Connection",
                    style: TextStyle(fontSize: 15),
                  )),
                  FlatButton(
                    child: Text(
                      'ok',
                      style: TextStyle(fontSize: 15),
                    ),
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                  ),
                ],
              );
            });
      }
    }

    class DashboardScreen extends StatefulWidget {
      @override
      _DashboardScreenState createState() => new _DashboardScreenState();
    }

    class _DashboardScreenState extends State<DashboardScreen> {
      final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
      LocalStorage userKey = new LocalStorage('userKey');
      TextEditingController pinNumber = new TextEditingController();

      bool hasKey;
      var userKeyValue;

      void showInSnackBar(String message) {
        scaffoldKey.currentState.showSnackBar(SnackBar(content: Text(message)));
      }

      @override
      void initState() {
        super.initState();
        bh(context);
      }

      void checkKey() async {
        userKeyValue = await userKey.getItem('userKey');
        print(userKeyValue);
        if (userKeyValue == null) {
          hasKey = false;
        } else {
          hasKey = true;
        }
      }

      void sendDataNav(BuildContext context) {
        String textToSend = pinNumber.text;

        Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => Confirm(text: textToSend),
            ));
      }

      var study_no;
      var select_mon;
      LocalStorage mon = LocalStorage('mon');
      LocalStorage studyNumber = LocalStorage('UserstudyNumber');
      void start(context) async {
        select_mon = DateTime.now().toString().substring(5, 7);
        print("study number $study_no");
        if (study_no == null) {
          print("in $study_no");
          Future.delayed(Duration.zero, () {
            Navigator.pushNamed(context, '/setting');
          });
        } else {
          print(select_mon);
          Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => HomeScreen(stu: study_no, mon: select_mon),
              ));
        }
      }

      @override
      Widget build(BuildContext context) {
        return Scaffold(
          resizeToAvoidBottomPadding: false,
          key: scaffoldKey,
          body: SingleChildScrollView(
            child: Column(
              children: <Widget>[
                Container(
                  height: MediaQuery.of(context).size.height / 2.5,
                  width: MediaQuery.of(context).size.width,
                  color: globals.primaryColor,
                  child: Image.asset('image/asset/Logob.png'),
                ),
                Padding(
                  padding: const EdgeInsets.all(15.0),
                  child: Container(
                    width: MediaQuery.of(context).size.width,
                    child: TextField(
                      controller: pinNumber,
                      style: new TextStyle(
                        fontSize: 20.0,
                      ),
                      decoration: InputDecoration(
                        contentPadding: const EdgeInsets.symmetric(
                            vertical: 10, horizontal: 20),
                        border: OutlineInputBorder(
                          borderSide: BorderSide(
                              color: Colors.green, style: BorderStyle.solid),
                          borderRadius: new BorderRadius.horizontal(),
                        ),
                        focusedBorder: OutlineInputBorder(
                          borderSide: BorderSide(
                              color: globals.primaryColor,
                              style: BorderStyle.solid),
                          borderRadius: new BorderRadius.horizontal(),
                        ),
                        //icon: Icon(Icons.calendar_today,),
                        hintText: 'PIN', suffixStyle: TextStyle(fontSize: 10),
                        labelText: 'Enter Your Pin',
                      ),

                      // Navigator.pushNamed(context,  '/afterCalender')
                      // keyboardType: TextInputType.number,
                      // autofocus: true,
                      onSubmitted: (ss) async {
                        if (pinNumber.text.length < 1) {
                          showInSnackBar("Enter Pin");
                          print('invalid');
                          return;
                        }
                        userKeyValue = await userKey.getItem('userKey');
                        if (userKeyValue == null) {
                          hasKey = false;
                        } else {
                          hasKey = true;
                        }
                        if (hasKey) {
                          if (userKeyValue != pinNumber.text) {
                            // show error message
                            showInSnackBar("Wrong Pin");
                            print('not valid');
                          } else {
                            print('valid');
                            study_no =
                                studyNumber.getItem('UserstudyNumber').toString();
                            start(context);
                            showInSnackBar("Sucessful");
                          }
                        } else {
                          if (pinNumber.text.length != 4) {
                            showInSnackBar("Enter Pin of 4 Numbers");
                            // show error message of length
                            print('hello');
                          } else {
                            setState(() {
                              sendDataNav(context);
                            });
                            // userKey.setItem('userKey', pinNumber.text);
                          }
                        }
                      },
                    ),
                  ),
                ),
                VirtualKeyboard(
                    height: 300,
                    fontSize: 15,
                    textColor: Colors.black,

                    type: VirtualKeyboardType.Numeric,
                    onKeyPress: (key) => print(key.text)),
              ],
            ),
          ),
        );
      }
    }

我如何将虚拟键盘而不是普通键盘的焦点放在textfiled上。
我想要禁用系统键盘和虚拟键盘以获取焦点并允许添加数据。 ................................................... ................................................... ................................................... ..............................................

最佳答案

触发onKeyPress回调后,您可以使用pinNumber文本 Controller 设置文本。

关于android - 如何在文本字段 flutter 中设置虚拟键盘的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60426871/

相关文章:

android - 找不到与 firebase-messaging 版本对应的 google-play-services 版本

java - 使用 Jsoup 和 Android Studio 截断网站

android - onItemClick 仅在文本上触发,而不是在 ListView 的整行上触发

firebase - Flutter/Firebase getter 'length'在null上被调用

firebase - 名称为 DEFAULT 的 Flutter Firebase 实时数据库 FirebaseApp 不存在。可用的应用程序名称 : [DEFAULT]

ios - 体系结构 i386 的 undefined symbol : _FlutterMethodNotImplemented

dart - art在整数移位时崩溃

flutter - 在Flutter Dart中访问和修改来自不同类的变量

android - 在Kotlin中重新格式化代码后,如何避免缩进以继续大括号?

flutter - ChangeNotifier挂载等效吗?