textfield文本在计时器运行时不断被删除



我正试图重建一个应用程序,曾经在本地android和我重建它在扑动
我已经设置了一个计时器和一个文本字段,每当我写在文本字段,它会被删除每秒钟计时器,是否有任何方式我可以运行的时间,而不删除文本字段上的文本下面是我的代码

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

将TextEditingController移出build函数。

当setState((){})被调用时,重新调用构建函数,这是将控制器重新分配给一个空字符串的新值,并且由于它附加到TextField,文本字段中的值将重置。

最新更新