简体   繁体   English

如何将图标添加到 TextFormField 下面的 errorText?

[英]How to add icon to errorText below TextFormField?

如何使用 Flutter 在 TextFormField 下方的 errorText 添加图标?

Prot_CN, i think the screenshot in your post is using a custom error message with a combination of Icon and Text widget as i am not sure if the built-in feature will allow this (i could be wrong). Prot_CN,我认为您帖子中的屏幕截图使用了带有IconText小部件组合的自定义错误消息,因为我不确定内置功能是否允许这样做(我可能错了)。 If you don't wanna design a custom error message, you could use unicode 26A0 to display a warning symbol with your error message which would look like this,如果您不想设计自定义错误消息,则可以使用 unicode 26A0显示警告符号和错误消息,如下所示,

截屏

Here is the complete code with unicode,这是带有unicode的完整代码,

class MyApp extends StatefulWidget {
  @override
  MyAppState createState() => MyAppState();
}

class MyAppState extends State<MyApp> {
  final _formKey = GlobalKey<FormState>();
  final textController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            appBar: AppBar(
              title: Text('Test'),
            ),
            body: Form(
                key: _formKey,
                child: Column(children: <Widget>[
                  Container(
                      padding: const EdgeInsets.all(20.0),
                      child: TextFormField(
                        controller: textController,
                        decoration: new InputDecoration(
                            errorStyle: TextStyle(fontSize: 18.0),
                            labelText: 'Hint text',
                            filled: true,
                            fillColor: Colors.white,
                            enabledBorder: new OutlineInputBorder(
                              borderRadius: new BorderRadius.circular(25.0),
                              borderSide: new BorderSide(
                                color: Colors.grey,
                              ),
                            ),
                            focusedBorder: new OutlineInputBorder(
                                borderRadius: new BorderRadius.circular(25.0),
                                borderSide: new BorderSide(
                                  color: Colors.blue,
                                )),
                            border: OutlineInputBorder(
                                borderRadius: new BorderRadius.circular(25.0),
                                borderSide: BorderSide(
                                    color: Colors.black, width: 1.0))),
                        style: new TextStyle(color: Colors.black),
                        validator: (value) {
                          if (value.isEmpty) {
                            return '\u26A0 Field is empty.';
                          }
                          return null;
                        },
                      )),
                  Center(
                      child: Padding(
                    padding: EdgeInsets.only(top: 20.0),
                    child: RaisedButton(
                      onPressed: () {
                        // Validate returns true if the form is valid, otherwise false.
                        if (_formKey.currentState.validate()) {
                          // If the form is valid, display a snackbar. In the real world,
                          // you'd often call a server or save the information in a database.

                          Scaffold.of(context).showSnackBar(
                              SnackBar(content: Text('Processing Data')));
                        }
                      },
                      child: Text('Submit'),
                    ),
                  ))
                ]))));
  }
}

Also, here is an example with custom error message similar to your screenshot,此外,这是一个与您的屏幕截图类似的自定义错误消息示例,

截屏

class MyApp extends StatefulWidget {
  @override
  MyAppState createState() => MyAppState();
}

class MyAppState extends State<MyApp> {
  final _formKey = GlobalKey<FormState>();
  final textController = TextEditingController();
  String errorText = '';
  IconData errorIcon;
  double errorContainerHeight = 0.0;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            appBar: AppBar(
              title: Text('Test'),
            ),
            body: Column(children: <Widget>[
                  Container(
                      padding: const EdgeInsets.only(top: 20.0, left: 20.0, right: 20.0),
                      child: TextFormField(
                        controller: textController,
                        decoration: new InputDecoration(
                          suffixIcon: Icon(Icons.arrow_drop_down, size: 35.0,),
                            labelStyle: TextStyle(fontSize: 18.0),
                            labelText: 'Hint text',
                            filled: true,
                            fillColor: Colors.white,
                            enabledBorder: new OutlineInputBorder(
                              borderRadius: new BorderRadius.circular(5.0),
                              borderSide: new BorderSide(
                                color: errorText.isEmpty ? Colors.grey : Colors.red[700],
                              ),
                            ),
                            focusedBorder: new OutlineInputBorder(
                                borderRadius: new BorderRadius.circular(5.0),
                                borderSide: new BorderSide(
                                  color: errorText.isEmpty ? Colors.blue : Colors.red[700],
                                )),
                            border: OutlineInputBorder(
                                borderRadius: new BorderRadius.circular(5.0),
                                borderSide: BorderSide(
                                    color: Colors.black, width: 1.0))),
                        style: new TextStyle(color: Colors.black),
                      )),
                  Container(
                    padding: EdgeInsets.only(left: 20.0, top: 5.0),
                    height: errorContainerHeight,
                      child: Row(
                    children: <Widget>[
                      Icon(errorIcon, size: 20.0, color: Colors.red[700]),
                      Padding(padding: EdgeInsets.only(left: 5.0),child: Text(errorText, style: TextStyle(fontSize: 16.0, color: Colors.red[700])))
                    ],
                  )),
                  Center(
                      child: Padding(
                    padding: EdgeInsets.only(top: 20.0),
                    child: RaisedButton(
                      onPressed: () {
                        // Validate returns true if the form is valid, otherwise false.
                        if (textController.text.isEmpty) {
                            setState(() {
                              errorContainerHeight = 35.0;
                              errorIcon = FontAwesomeIcons.exclamationCircle;
                              errorText = 'Field is empty.';
                            });
                          } else {
                          setState(() {
                            errorContainerHeight = 0.0;
                            errorIcon = null;
                            errorText = '';
                          });
                        }

                      },
                      child: Text('Submit'),
                    ),
                  ))
                ])));
  }
}

Hope this helps.希望这可以帮助。

<style name="TextInputLayoutStyle"
    parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
    <item name="boxStrokeColor">@color/black</item>
    <item name="boxStrokeWidth">2dp</item>
</style>

Layout布局

   <com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/etUsernameLayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    style="@style/TextInputLayoutStyle"
                    app:errorEnabled="true"
                    app:errorIconDrawable="@null"
                    >
                    <com.google.android.material.textfield.TextInputEditText
                        android:id="@+id/username"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="@string/username"
                        android:textSize="@dimen/normalTextSize"
                        android:inputType="textPersonName"
                        android:maxLength="100" />
                </com.google.android.material.textfield.TextInputLayout>

Java爪哇

  usernameTIL.setError("ERROR")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 颤振:在TextFormField图标下划线 - Flutter: Underline below TextFormField icon 如何在 flutter 的 TextFormField 中的图标和文本之间添加分隔线 - How can i add a divider between icon and text in TextFormField in flutter 如何在TextInputLayout的错误文本中添加resolve-action? - How can I add a resolve-action to the errortext in an TextInputLayout? 如何在 Flutter 的 TextFormField 中添加提示? - How to add hint in the TextFormField in Flutter? 如何将图标放在我的 textFormField/ 文本字段的左上角? - How can i put the icon on the left top in my textFormField/ textfield? TextFormField 上的可点击图标 - 禁用 TextFormField 对图标单击的关注(颤振) - Clickable icon on TextFormField - disable TextFormField focus on icon click (Flutter) 如何在Android中更改ErrorText和Datepicker样式 - How to I change ErrorText and Datepicker Style in Android 安卓浓缩咖啡。 如何检查 TextInputLayout 中的 ErrorText - Android Espresso. How to check ErrorText in TextInputLayout Flutter TextFormField onChangeText 它没有显示图标 - Flutter TextFormField onChangeText it is not showing icon Flutter/Dart:如何向 TextFormField 等单选按钮添加验证? - Flutter/Dart :How to add validation to radio button like TextFormField?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM