gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 07:03:29 26 4
gpt4 key购买 nike

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

此外,这个图标应该有一个弹出窗口,当错误发生时默认打开,如下所示image .

我该怎么做?

最佳答案

你可以尝试这样的事情,但下次请解释你的问题(添加一些代码)因为人们不能留在这里理解你想要什么......如果你需要帮助你需要清楚:

主要

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;
}
}

关于android - 如何在 Flutter 的 TextField 中显示错误图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56005180/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com