如何在Flutter中的TextField中显示错误图标

时间:2019-05-06 12:24:08

标签: android dart flutter textedit

当用户在TextField中输入无效输入时,我需要显示一个错误图标。

此外,此图标还应该具有一个弹出窗口,默认情况下会在发生错误时将其打开,例如以下image

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您可以尝试类似的操作,但是请在下一次重复您的问题(添加一些代码)时,导致人们无法留在这里以了解您想要的内容...如果您需要帮助,则需要明确说明:

主要

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

主页

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  ErrorIcon errorIcon = new ErrorIcon(false);

  @override
  Widget build(BuildContext context) {
    debugPrint("Rebuilding main widget");

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Padding(
          //Add padding around textfield
          padding: EdgeInsets.symmetric(horizontal: 25.0),
          child: TextField(
            onChanged: (text) {
              // DEFINE YOUR RULES HERE
              if (text.contains("1")) {
                setState(() {
                  errorIcon = new ErrorIcon(true);
                });
              } else {
                setState(() {
                  errorIcon = new ErrorIcon(false);
                });
              }
            },
            decoration: InputDecoration(
              hintText: "Enter Username",
              icon: errorIcon,
            ),
          ),
        ),
      ),
    );
  }
}

错误图标的错误小部件

class ErrorIcon extends StatefulWidget {
  bool _isError = false;

  ErrorIcon(this._isError);

  bool get isError => _isError;

  @override
  _ErrorIconState createState() => _ErrorIconState();
}

class _ErrorIconState extends State<ErrorIcon> {
  @override
  Widget build(BuildContext context) {
    Widget out;

    debugPrint("Rebuilding ErrorWidget");
    widget.isError
        ? out = new Icon(
            Icons.error,
            color: Color(Colors.red.value),
          )
        : out = new Icon(null);

    return out;
  }
}

请注意,此代码可以改进,因此我可以让您使用它:)

编辑 干净的代码,我很开心,所以只需一点重构:

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  ErrorIcon _errorWidget = new ErrorIcon(false);

  set errorWidget(ErrorIcon value) {
    setState(() {
      _errorWidget = value;
    });
  }

  @override
  Widget build(BuildContext context) {
    debugPrint("Rebuilding main widget");

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Padding(
          //Add padding around textfield
          padding: EdgeInsets.symmetric(horizontal: 25.0),
          child: TextField(
            onChanged: (text) {
              // DEFINE YOUR RULES HERE
              text.contains("1")
                  ? errorWidget = new ErrorIcon(true)
                  : errorWidget = new ErrorIcon(false);
            },
            decoration: InputDecoration(
              hintText: "Enter Username",
              icon: _errorWidget,
            ),
          ),
        ),
      ),
    );
  }
}

class ErrorIcon extends StatelessWidget {
  bool _isError;

  ErrorIcon(this._isError);

  bool get isError => _isError;

  @override
  Widget build(BuildContext context) {
    Widget out;

    debugPrint("Rebuilding ErrorWidget");
    isError
        ? out = new Icon(
            Icons.error,
            color: Color(Colors.red.value),
          )
        : out = new Icon(null);

    return out;
  }
}