英文:
Comparing a String to a list of strings and then if there is a match, switching a bool
问题
我有一个TextFormField小部件,一个灰色容器来设置状态,以及一个根据与之相关的布尔值而更改颜色的容器,显示在我的页面上。我还有一个字符串列表(未显示)。
import 'package:flutter/material.dart';
class Test extends StatefulWidget {
const Test({Key? key}) : super(key: key);
@override
State<Test> createState() => _TestState();
}
class _TestState extends State<Test> {
bool activated = false;
TextEditingController textController = TextEditingController();
List<String> listOfStrings = [
'String01',
'String02',
'String03',
'String04',
'String05',
'String06',
'String07',
'String08',
'String09',
'String10',
];
@override
void dispose() {
textController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
SizedBox(
height: 20,
),
Container(
padding: EdgeInsets.symmetric(horizontal: 10),
alignment: Alignment.center,
child: TextFormField(
autofocus: false,
controller: textController,
style: TextStyle(
color: Colors.white,
fontSize: 12,
),
textInputAction: TextInputAction.done,
),
),
SizedBox(
height: 20,
),
InkWell(
onTap: () {
if (listOfStrings.contains(textController.text)) {
setState(() {
activated = true;
listOfStrings.remove(textController.text);
});
}
},
child: Container(
height: 20,
width: 60,
color: Colors.blueGrey,
)),
SizedBox(
height: 60,
),
Container(
width: 20,
height: 20,
color: activated ? Colors.green : Colors.red,
),
],
),
);
}
}
我尝试的是,当将字符串输入到TextFormField中时,我希望将答案与列表进行比较,如果匹配,将布尔值切换为true,并从列表中删除字符串。
你需要在InkWell部件的onTap
回调中添加逻辑来比较文本字段的内容与字符串列表中的内容。如果匹配,将布尔值activated
设置为true
,并从列表中删除匹配的字符串。
英文:
I had a TextFormField widget, a grey container to set the state and a container that will change color according to the bool assosiated with it displayed on my page.
I also have a list of strings (not displayed).
import 'package:flutter/material.dart';
class Test extends StatefulWidget {
const Test({Key? key}) : super(key: key);
@override
State<Test> createState() => _TestState();
}
class _TestState extends State<Test> {
bool activated = false;
TextEditingController textController = TextEditingController();
List<String> listOfStrings = [
'String01',
'String02',
'String03',
'String04',
'String05',
'String06',
'String07',
'String08',
'String09',
'String10',
];
@override
void dispose() {
textController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
SizedBox(
height: 20,
),
Container(
padding: EdgeInsets.symmetric(horizontal: 10),
alignment: Alignment.center,
child: TextFormField(
autofocus: false,
controller: textController,
style: TextStyle(
color: Colors.white,
fontSize: 12,
),
textInputAction: TextInputAction.done,
),
),
SizedBox(
height: 20,
),
InkWell(
onTap: () {
if (textController == listOfStrings) {
activated = true;
}
},
child: Container(
height: 20,
width: 60,
color: Colors.blueGrey,
)),
SizedBox(
height: 60,
),
Container(
width: 20,
height: 20,
color: activated ? Colors.green : Colors.red,
),
],
),
);
}
}
what I'm trying to do is that when a string is entered into the textformfield, I want it to compare the answer to the list and if there is a match, switch the bool to true and delete the string from the list.
I know I have to use an if statement in a setsate which is the grey contaner. (is it possible to setstate with enter on the keyboard instead?) but im not sure what that if statement should be to compare to the list of strings
thanks so much and any help would be greatly appreciated
答案1
得分: 1
这是您第一个问题的答案:
if (listOfStrings.contains(textController.text)) {
activated = true;
}
对于您的第二个问题:
是的,您可以在按下TextFormField上的Enter键时更改bool值。
您只需要检查:
onFieldSubmitted: (String s) {
//在这里,您可以写入输入后要执行的任何操作
}
从列表中移除特定值:
listOfStrings.removeWhere((element) => element == textController.text)
英文:
Here is the answer of your first question
if (listOfStrings.contains(textController.text)) {
activated = true;
}
For your second question
Yes, you can change bool value when you press enter on TextFormField
You just need to check
onFieldSubmitted: (String s) {
//Here you can write whatever you want to do after entered
}
Remove the particular value from list
listOfStrings.removeWhere((element) => element == textController.text)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论