Nomad09
Nomad09

Reputation: 199

Comparing a String to a list of strings and then if there is a match, switching a bool

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

Upvotes: 0

Views: 80

Answers (1)

Rohan Jariwala
Rohan Jariwala

Reputation: 2050

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)

Upvotes: 1

Related Questions