Search code examples
flutterflutter-dependencies

Flutter comparing text and textfield


I was trying to compare text with textfield and if my first keyboard hit match with first element of text, i want to increase score but the code below doesn't work any idea why it's not working ?

var textField = TextField(
        controller: myController,
        decoration: InputDecoration(
            enabledBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(50),
                borderSide: BorderSide(color: Colors.black))),
        autocorrect: false,
        textAlign: TextAlign.center,
        onChanged: (text) {
          for (int x = 0; x < text.length; x++) {
            if (myController.text[x] == textP1[x]) {//textP1 is my original text
              score++;
              if (score == textP1.length) {
                showDialog(
                  context: context,
                  builder: (context) {
                    return AlertDialog(
                      content: Text("YOU WON !!!!"),
                    );
                  },
                );
              }
            }
          }
        });

Solution

  • Maybe you need to reset your score just before the loop starts.

    
    class SampleState extends StatefulWidget {
      @override
      _SampleStateState createState() => _SampleStateState();
    }
    
    class _SampleStateState extends State<SampleState> {
      final textP1 = "suman";
      var myController = TextEditingController();
    
      var score = 0;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: new AppBar(
                centerTitle: true,
                title: new Text('Sign in',
                    style: TextStyle(fontWeight: FontWeight.bold))),
            body: new Container(
                child: Column(
                  children: <Widget>[
                    new TextField(
                        controller: myController,
                        decoration: InputDecoration(
                            enabledBorder: OutlineInputBorder(
                                borderRadius: BorderRadius.circular(50),
                                borderSide: BorderSide(color: Colors.black))),
                        autocorrect: false,
                        textAlign: TextAlign.center,
                        onChanged: (text) {
                          score = 0;
                          for (int x = 0; x < text.length; x++) {
                            if (myController.text[x] == textP1[x]) {
                              //textP1 is my original text
                             setState(() {
                               score++;
                             });
                              print(score);
                              if (score == textP1.length) {
                                showDialog(
                                  context: context,
                                  builder: (context) {
                                    return AlertDialog(
                                      content: Text("YOU WON !!!!"),
                                    );
                                  },
                                );
                              }
                            }
                          }
                        }),
                    Text("Score: $score")
                  ],
                )));
      }
    }