簡體   English   中英

如何更改 Textformfield 驗證文本的 position? Flutter

[英]How can I change the position of the Textformfield validation text ? Flutter

我制作了一個自定義表單以及何時使用驗證。 我的問題是錯誤消息沒有按我的意願出現。 這是它的屏幕截圖。

在此處輸入圖像描述

所以我想將錯誤消息的 position 更改為在我的容器下方。 有誰知道該怎么做?

這是表單的代碼:

class AuthForm extends StatefulWidget {
  final bool isPassword;
  final IconData prefixIcon;
  final String hintText;
  late bool isPasswordVisible = isPassword;
  final bool isCalendar;
  final TextEditingController controller;
  final bool isDropDown;
  final bool isPhone;
  final String? Function(String?)? validator;

  AuthForm({Key? key, this.isPassword = false, required this.prefixIcon, required this.hintText,
    this.isCalendar = false, required this.controller, this.isDropDown = false, this.isPhone = false, required this.validator}) : super(key: key);

  @override
  State<AuthForm> createState() => _AuthFormState();
}

class _AuthFormState extends State<AuthForm> {

  @override
  void initState() {
    super.initState();
    if (widget.isPhone){
      getCountryCode();
    }
  }

  start () async {
    await CountryCodes.init();
  }
  Locale? getCountryCode () {
    start();
    final Locale? deviceLocale = CountryCodes.getDeviceLocale();
    final CountryDetails details = CountryCodes.detailsForLocale();
    return deviceLocale;
  }

  DateTime selectedDate = DateTime(2000,1);
  Future<void> _selectDate(BuildContext context) async {
    final DateTime? picked = await showDatePicker(
        context: context,
        initialDate: selectedDate,
        firstDate: DateTime(1950, 1),
        lastDate: DateTime.now());
    if (picked != null && picked != selectedDate) {
      setState(() {
        selectedDate = picked;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 70.w,
      height: 5.h,
      decoration: BoxDecoration(
        border: Border(
          bottom: BorderSide(
            width: 0.13.w,
            color: Theme.of(context).splashColor,
          ),
        ),
      ),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Icon(
            widget.prefixIcon,
            color: Theme.of(context).primaryColor,
            size: 6.w,
          ),
          widget.isDropDown ? const DropDownBar() :
          Expanded(
            child: TextFormField(
              //autovalidateMode: AutovalidateMode.onUserInteraction,
              validator: widget.validator,
              keyboardType: widget.isPhone ? TextInputType.phone : TextInputType.text,
              inputFormatters: [DialCodeFormatter()],
              controller: widget.controller,
              textAlign: TextAlign.center,
              obscureText: widget.isPasswordVisible,
              style: Theme.of(context).textTheme.bodyText2,
              decoration: InputDecoration(
                hintText : widget.hintText,
                hintStyle: Theme.of(context).textTheme.bodyText1,
                border: InputBorder.none,
              ),
              onTap: () async {
                if (widget.isCalendar){
                  //Dismiss the keyboard
                  FocusScope.of(context).requestFocus(FocusNode());
                  //Call the calendar
                  await _selectDate(context);
                  widget.controller.text = DateFormat('dd-MM-yyyy').format(selectedDate);
                }
              }
            ),
          ),
          //Show Icon only if the form is for a Password
          Visibility(
            visible: widget.isPassword,
            //Maintain the space where the widget is even if it is hid
            maintainAnimation: true,
            maintainState: true,
            maintainSize: true,
            child: InkWell(
              highlightColor : Colors.transparent,
              splashColor: Colors.transparent,
              child: Icon(
                widget.isPasswordVisible ? Icons.visibility : Icons.visibility_off,
                color: Theme.of(context).primaryColor,
                size: 6.w,
              ),
              onTap: () {
                setState(() {
                  widget.isPasswordVisible = !widget.isPasswordVisible;
                });
              },
            ),
          ),
        ],
      ),
    );
  }
}

謝謝你的建議,

克里斯

您應該嘗試下面的TextFormField代碼

TextFormField(
                      textInputAction: TextInputAction.next,
                      style: TextStyle(fontSize: 16, height: 1),
                      inputFormatters: <TextInputFormatter>[FilteringTextInputFormatter.digitsOnly, LengthLimitingTextInputFormatter(10)],
                      keyboardType: TextInputType.phone,
                      validator: (val) {
                        if (val!.isEmpty) {
                          return 'Please enter your mobile number';
                        } else if (val.length < 10) {
                          return 'Mobile number should be 10 digits only';
                        }
                      },
                      controller: bloc.mobileNumberLoginController,
                      decoration: InputDecoration(
                        filled: true,
                        border: OutlineInputBorder(
                            borderSide: BorderSide(
                          color: code != null ? HexColor(code!) : Colors.pink,
                        )),
                        contentPadding: EdgeInsets.only(top: 5, bottom: 5, left: 10, right: 10),
                      ),
                    ),

輸出

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM