简体   繁体   中英

flutter - How can I remove the content padding for error in TextFormField?

Because of the content padding the error message also starts from the left padding. How can I show for error message different content padding. Actually it should start from the left side border. But I need the content padding for inside the TextFormField, not for error messages.

在此处输入图像描述

                                        Padding(
                                      padding: const EdgeInsets.all(10),
                                      child: Form(
                                        key: _formKeyOldPassword,
                                        onChanged: () {
                                          setState(() {
                                            _proceedOldPassword =
                                                _formKeyOldPassword
                                                    .currentState!
                                                    .validate();
                                          });
                                        },
                                        child: TextFormField(
                                          controller: oldPasswordController,
                                          obscureText: _oldPasswordVisible,
                                          autofocus: false,
                                          style: AppStyle().textStyleLabel,
                                          validator: (text) {
                                            return Validations()
                                                .validatePassword(
                                                    text.toString().trim());
                                          },
                                          onChanged: (value) {
                                            setState(() {
                                              if (value[0].endsWith(' ')) {
                                                oldPasswordController
                                                        .selection =
                                                    const TextSelection(
                                                        baseOffset: 0,
                                                        extentOffset: 0);
                                                oldPasswordController.text =
                                                    value.trim();
                                              } else {
                                                Validations()
                                                    .validatePassword(value
                                                        .toString()
                                                        .trim());
                                              }
                                            });
                                          },
                                          decoration: InputDecoration(
                                            suffixIcon: IconButton(
                                              icon: Icon(_oldPasswordVisible
                                                  ? Icons.visibility_off
                                                  : Icons.visibility),
                                              color: AppColors.suffixIcon,
                                              onPressed: () {
                                                setState(() {
                                                  _oldPasswordVisible =
                                                      !_oldPasswordVisible;
                                                });
                                              },
                                            ),
                                            border: InputBorder.none,
                                            errorBorder: OutlineInputBorder(
                                              borderSide: const BorderSide(
                                                color: AppColors.error,
                                              ),
                                              borderRadius:
                                                  BorderRadius.circular(
                                                      4.0),
                                            ),
                                            focusedErrorBorder:
                                                OutlineInputBorder(
                                              borderSide: const BorderSide(
                                                color: AppColors.error,
                                              ),
                                              borderRadius:
                                                  BorderRadius.circular(
                                                      4.0),
                                            ),
                                            hintText: Strings.h006,
                                            hintStyle:
                                                AppStyle().textStyleHint,
                                            filled: true,
                                            fillColor:
                                                AppColors.textFormFieldFill,
                                            isDense: true,
                                            contentPadding:
                                                 const EdgeInsets.only(
                                              left: 20.0,
                                              bottom: 0.0,
                                              top: 15.0,
                                            ),
                                            focusedBorder:
                                                OutlineInputBorder(
                                              borderSide: const BorderSide(
                                                  color: AppColors
                                                      .textFormFieldFill),
                                              borderRadius:
                                                  BorderRadius.circular(
                                                      4.0),
                                            ),
                                            enabledBorder:
                                                UnderlineInputBorder(
                                              borderSide: const BorderSide(
                                                color: AppColors
                                                    .textFormFieldFill,
                                              ),
                                              borderRadius:
                                                  BorderRadius.circular(
                                                      10.0),
                                            ),
                                          ),
                                        ),
                                      ),
                                    )

Kindly share code for this widget so I can provide you a solution.

There is no direct and official way to do this. Apparently, contentPadding is also applied to the errorText.

However I have found a workaround that can work for you. Remove left padding from contentPadding of InputDecoration. Instead add this padding inside the prefix property of InputDecoration. So your final InputDecoration will look like this:

decoration: InputDecoration(
...
   contentPadding: const EdgeInsets.only(bottom: 0.0, top: 15.0),
   prefix: const Padding(
     padding: EdgeInsets.only(left: 20.0)),
     ),
...
),

In this way the errorText will start from the left side of the border.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM