Home > Blockchain >  textfield text keeps getting removed when timer running
textfield text keeps getting removed when timer running

Time:05-29

I am trying to rebuild an app that was once made in native android and i am rebuilding it on flutter
I have set up a timer and a textfield and whenever I write on the textfield, it would be removed every second by the timer, is there any way i could run the time without deleting the text on the textfield here is my code below

import 'dart:async';
import 'dart:io';
import 'dart:math';
import 'math_utils.dart';
import 'package:flutter/material.dart';

class GameScreen extends StatefulWidget {
  @override
  State<GameScreen> createState() => _GameScreenState();
}

class _GameScreenState extends State<GameScreen> {
  int n1 = Random().nextInt(13)   0;
  int n2 = Random().nextInt(13)   0;
  int lives = 3;
  int score = 0;
  FocusNode node = FocusNode();
  int sec = 60;

  @override
  void initState(){
    Timer.periodic(const Duration(seconds: 1), (timer) {
      setState(() {
        sec--;
      });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    TextEditingController controller = TextEditingController();
    return SafeArea(
      child: Scaffold(
        backgroundColor: Colors.black,
        body: Container(
          width: size.width,
          child: SingleChildScrollView(
            child: Container(
              decoration: BoxDecoration(
                color: Colors.black54,
              ),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  Align(
                    alignment: Alignment.centerLeft,
                    child: Padding(
                      padding: EdgeInsets.only(
                        top: getVerticalSize(
                          16.00,
                        ),
                        bottom: getVerticalSize(
                          20.00,
                        ),
                      ),
                      child: Column(
                        mainAxisSize: MainAxisSize.min,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          Align(
                            alignment: Alignment.centerLeft,
                            child: Container(
                              width: size.width,
                              child: Padding(
                                padding: EdgeInsets.only(
                                  left: getHorizontalSize(
                                    16.00,
                                  ),
                                  right: getHorizontalSize(
                                    16.00,
                                  ),
                                ),
                                child: Row(
                                  mainAxisAlignment:
                                  MainAxisAlignment.spaceBetween,
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  mainAxisSize: MainAxisSize.max,
                                  children: [
                                    Padding(
                                      padding: EdgeInsets.only(
                                        top: getVerticalSize(
                                          3.00,
                                        ),
                                      ),
                                      child: Row(
                                        crossAxisAlignment:
                                        CrossAxisAlignment.center,
                                        mainAxisSize: MainAxisSize.min,
                                        children: [
                                          lives >= 1? Image.asset(
                                            "assets/heart.webp",
                                            height: getSize(
                                              32.00,
                                            ),
                                            width: getSize(
                                              32.00,
                                            ),
                                            fit: BoxFit.fill,
                                          ): SizedBox(
                                            height: getSize(
                                              32.00,
                                            ),
                                            width: getSize(
                                              32.00,
                                            ),
                                          ),
                                          lives >= 2? Image.asset(
                                            "assets/heart.webp",
                                            height: getSize(
                                              32.00,
                                            ),
                                            width: getSize(
                                              32.00,
                                            ),
                                            fit: BoxFit.fill,
                                          ): SizedBox(
                                            height: getSize(
                                              32.00,
                                            ),
                                            width: getSize(
                                              32.00,
                                            ),
                                          ),
                                          lives >= 3? Image.asset(
                                            "assets/heart.webp",
                                            height: getSize(
                                              32.00,
                                            ),
                                            width: getSize(
                                              32.00,
                                            ),
                                            fit: BoxFit.fill,
                                          ): SizedBox(
                                            height: getSize(
                                              32.00,
                                            ),
                                            width: getSize(
                                              32.00,
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                    Text(
                                      "Score: $score",
                                      overflow: TextOverflow.ellipsis,
                                      textAlign: TextAlign.center,
                                      style: TextStyle(
                                        color: Colors.white70,
                                        fontSize: getFontSize(
                                          24,
                                        ),
                                        fontFamily: 'Inter',
                                        fontWeight: FontWeight.w400,
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                          ),
                          Container(
                            width: getHorizontalSize(
                              40.00,
                            ),
                            margin: EdgeInsets.only(
                              left: getHorizontalSize(
                                96.00,
                              ),
                              top: getVerticalSize(
                                12.00,
                              ),
                              right: getHorizontalSize(
                                96.00,
                              ),
                            ),
                            child: Text(
                              "$sec",
                              maxLines: null,
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                color: Colors.white70,
                                fontSize: getFontSize(
                                  24,
                                ),
                                fontFamily: 'Inter',
                                fontWeight: FontWeight.w400,
                              ),
                            ),
                          ),
                          Padding(
                            padding: EdgeInsets.only(
                              left: getHorizontalSize(
                                96.00,
                              ),
                              top: getVerticalSize(
                                200.00,
                              ),
                              right: getHorizontalSize(
                                96.00,
                              ),
                            ),
                            child: Text(
                              "${n1} x ${n2}",
                              overflow: TextOverflow.ellipsis,
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                color: Colors.white70,
                                fontSize: getFontSize(
                                  64,
                                ),
                                fontFamily: 'Inter',
                                fontWeight: FontWeight.w400,
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                  TextField(
                    focusNode: node,
                    controller: controller,
                    textAlign: TextAlign.center,
                    onSubmitted: (value) async{
                      setState(() {
                        if(value == (n1 * n2).toString()){
                          score  = 10;
                          n1 = Random().nextInt(13)   0;
                          n2 = Random().nextInt(13)   0;
                          print(true);
                        }
                        else {
                          lives -= 1;
                          print(false);
                        }

                      });
                      controller.clear();
                      Future sleep(final Duration duration) async {
                        await Future.delayed(duration);
                      }
                      await sleep(Duration(milliseconds: 10));
                      node.requestFocus();
                    },
                    autofocus: true,
                    decoration: InputDecoration(
                      border: InputBorder.none
                    ),
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 48
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}

CodePudding user response:

Move the TextEditingController outside the build function.

when setState(() {}) is called, the build function reinvoked, which is reassigning the controller to a new value with empty string, and since it is attached to the TextField, the value in the text field will reset.

  • Related