将字符串与字符串列表进行比较,如果匹配,则切换布尔值。

huangapple go评论47阅读模式
英文:

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 &#39;package:flutter/material.dart&#39;;

class Test extends StatefulWidget {
  const Test({Key? key}) : super(key: key);

  @override
  State&lt;Test&gt; createState() =&gt; _TestState();
}

class _TestState extends State&lt;Test&gt; {
  bool activated = false;
  TextEditingController textController = TextEditingController();

  List&lt;String&gt; listOfStrings = [
    &#39;String01&#39;,
    &#39;String02&#39;,
    &#39;String03&#39;,
    &#39;String04&#39;,
    &#39;String05&#39;,
    &#39;String06&#39;,
    &#39;String07&#39;,
    &#39;String08&#39;,
    &#39;String09&#39;,
    &#39;String10&#39;,
  ];

  @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) =&gt; element == textController.text)

huangapple
  • 本文由 发表于 2023年2月7日 02:37:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365314.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定