0

i have a weird behavior on my application i have built a custom OTP screen and when i test it the keyboard shows a weird behavior the system keyboard flicker when moving throw fields i have tried different solutions but nothing worked for me:

Note: i don't want to use external packages.

enter image description here

screen code:

build(BuildContext context) {
// AppBar height
final double appBarHeight = kToolbarHeight; // Default appBar height is 56.0
final double statusBarHeight = MediaQuery.of(context).padding.top;

return Scaffold(
  extendBodyBehindAppBar: true,
  appBar: AppBar(
    backgroundColor: Colors.transparent,
    elevation: 0.0,
  ),
  body: Container(
    width: double.infinity,
    height: double.infinity,
    decoration: BoxDecoration(
      gradient: ThemeController().themeGradient,
    ),
    child: Padding(
      padding: EdgeInsets.only(
        top: appBarHeight + statusBarHeight,
        left: 16.0,
        right: 16.0,
        bottom: 16.0,
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text(
            'التحقق من الجوال',
            style: TextStyle(
              color: Colors.white,
              fontSize: 24.0,
            ),
          ),
          Text(
            'تم إرسال رمز التحقق إلى رقم جوالك الرجاء تحقق من الرسائل',
            style: TextStyle(
              color: Colors.white70,
              fontSize: 16.0,
            ),
            textAlign: TextAlign.center,
          ),
          const SizedBox(height: 32.0),
          Directionality(
            textDirection: TextDirection.ltr,
            child: Column(
              children: [
                // Pinput(
                //   length: 6,
                // ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    OTPFieldWidget(
                      controller: textEditingControllerOne,
                      first: true,
                    ),
                    OTPFieldWidget(
                      controller: textEditingControllerTwo,
                    ),
                    OTPFieldWidget(
                      controller: textEditingControllerThree,
                    ),
                    OTPFieldWidget(
                      controller: textEditingControllerFour,
                    ),
                    OTPFieldWidget(
                      controller: textEditingControllerFive,
                    ),
                    OTPFieldWidget(
                      controller: textEditingControllerSix,
                      last: true,
                    ),
                  ],
                ),
              ],
            ),
          ),
          const SizedBox(height: 48.0),
          CustomElevatedButton(
            onPressed: () => Get.offAllNamed(HomepageScreen.routeName),
            label: 'تحقق',
          ),
        ],
      ),
    ),
  ),
);

}

OTP widget code:

TextField(
  controller: controller,
  onTap: () {
    controller.selection =
        TextSelection.collapsed(offset: controller.text.length);
  },
  onChanged: (value) {
    if (value.isNotEmpty && !last) {
      FocusScope.of(context).nextFocus();
    }
    if (value.isEmpty && !first) FocusScope.of(context).previousFocus();
  },
  autofocus: autoFocus,
  textAlign: TextAlign.center,
  maxLength: 1,
  keyboardType: TextInputType.number,
  inputFormatters: [FilteringTextInputFormatter.digitsOnly],
  style: TextStyle(
    color: Colors.white,
    fontWeight: FontWeight.bold,
    fontSize: 24.0,
  ),
  cursorHeight: 32.0,
  decoration: InputDecoration(
    contentPadding: EdgeInsets.symmetric(
      vertical: 8.0,
    ),
    counterText: '',
    constraints: BoxConstraints(
      maxWidth: 48.0,
    ),
    border: OutlineInputBorder(
      borderRadius: BorderRadius.circular(12),
      borderSide: BorderSide(
        color: Colors.white,
        width: 2.0,
      ),
    ),
    focusedBorder: OutlineInputBorder(
      borderRadius: BorderRadius.circular(12),
      borderSide: BorderSide(
        width: 2,
        color: Colors.white70,
      ),
    ),
    enabledBorder: OutlineInputBorder(
      borderRadius: BorderRadius.circular(12),
      borderSide: BorderSide(
        width: 2,
        color: Colors.white70,
      ),
    ),
    errorBorder: OutlineInputBorder(
      borderRadius: BorderRadius.circular(12),
      borderSide: BorderSide(
        width: 2,
        color: Colors.white70,
      ),
    ),
    focusedErrorBorder: OutlineInputBorder(
      borderRadius: BorderRadius.circular(12),
      borderSide: BorderSide(
        width: 2,
        color: Colors.white70,
      ),
    ),
  ),
);

Thanks for your time, happy coding for you all.

2
  • what are solutions you tried? are you tried to define multiple focusNode for each field? are you tried to make the autoFouuc = true for the first field only? maybe keyboard type of numbers make some issues are you tried using numberWithOptions ? please clarify the steps you did for tying to solve this issue Commented Dec 19, 2024 at 5:59
  • Hello saayeed, i have tried define focusNode and pass it for the fields (did not work), make sure all fields keyboard type of number (did not work), and the autoFocus (did not work), ill try numberWithOptoins and check if it will works, thank you for your comment. Commented Dec 20, 2024 at 7:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.