简体   繁体   English

Flutter 比较文本和文本字段

[英]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 !!!!"),
                    );
                  },
                );
              }
            }
          }
        });

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")
              ],
            )));
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM