Mr.Robot
Mr.Robot

Reputation: 169

Keyboard automatically closes in flutter while using form

In my code my keyboard closes automatically when using text formfield and I realized that it closes when I use Form and its key for validation, I am using list view with form but when I remove form it works fine but I need a form to validate, what should I do ?

  Form(
            key: _formKeyPlant,
            child: ListView(children: [
              SizedBox(
                width: size.width,
                child: Column(
                  children: [
                  
                    CustomTextField(
                      controller: name,
                      title: 'Name',
                      validator: (text) {
                        if (text == null || text.isEmpty) {
                          return 'Required';
                        } else if (text.contains(RegExp(r'[0-9]')) ||
                            text.contains(RegExp(r'[#?!@$%^&*-]'))) {
                          return 'Invalid Input';
                        }
                        return null;
                      },
                    ),
                    15.verticalSpace,
                    CustomTextField(
                      controller: location,
                      title: 'Location',
                      validator: (text) {
                        if (text == null || text.isEmpty) {
                          return 'Required';
                        } else if (text.contains(RegExp(r'[0-9]')) ||
                            text.contains(RegExp(r'[#?!@$%^&*-]'))) {
                          return 'Invalid Input';
                        }
                        return null;
                      },
                    ),
                    15.verticalSpace,
                    CustomTextField(
                      enabled: false,
                      onTap: () async {
                        final response = await showAppBottomSheet(
                          context,
                          const TreesInfoBottomSheet(),
                        );
                        if (response != null) {
                          treeType.text = response['name'];
                          treeTypeId = response['id'];
                        }
                      },
                      controller: treeType,
                      title: 'Tree Type',
                    ),
                    15.verticalSpace,
                    CustomTextField(
                      controller: ageOfTree,
                      inputType: TextInputType.number,
                      validator: (text) {
                        if (text == null || text.isEmpty) {
                          return 'Required';
                        } else if (text.contains(RegExp(r'[a-z]')) ||
                            text.contains(RegExp(r'[A-Z]')) ||
                            text.contains(RegExp(r'[#?!@$%^&*-]'))) {
                          return 'Invalid Input';
                        }
                        return null;
                      },
                      title: 'Age of Tree',
                    ),
                    15.verticalSpace,
                    CustomTextField(
                      controller: registration,
                      title: 'Registration ID (Optional)',
                    ),
                    15.verticalSpace,
                    CustomTextField(
                      maxLines: 6,
                      controller: comments,
                      labelText: 'comments',
                    ),
               
                  
                      },
                    ),
                    40.verticalSpace,
                  ],
                ).px(18),
              ),
            ]),
          ),

Upvotes: 0

Views: 231

Answers (1)

Rahul Variya
Rahul Variya

Reputation: 1352

Correct formate of using textform filed with form and scroll widget is

Please try the below code your keyboard with never hide on textformfiled

final Key _key = GlobalKey();
  FocusNode f1 = FocusNode();
  FocusNode f2 = FocusNode();
  @override
  Widget build(BuildContext context) {
    return Form(
      key: _key,
      child: SingleChildScrollView(
        child: Column(
          children: [
            TextFormField(
              focusNode: f1,
              onFieldSubmitted: (c) {
                f1.unfocus();
                f2.requestFocus();
              },
            ),
            TextFormField(
              focusNode: f2,
            ),
          ],
        ),
      ),
    );
  }

Upvotes: 2

Related Questions