[英]Flutter update Text Widget
I am new with Flutter and I face a problem, I followed this flappy bird tutorial made in Dart I am trying to update a Text widget with a score integer and I am incrementing score each time a barrier passes by, the barriers moves in startGame and it updates score in startGame but it doesn't update Text.我是 Flutter 的新手,我遇到了一个问题,我按照 Dart 制作的飞扬小鸟教程进行操作它会更新 startGame 中的分数,但不会更新 Text。
How do I make it so Text gets updated accordingly?我怎样才能使文本得到相应的更新?
Code:代码:
import 'dart:async';
import 'package:flappy_bird_project/barriers.dart';
import 'package:flappy_bird_project/bird.dart';
import 'package:flutter/material.dart';
class HomePage extends StatefulWidget {
@override
_HomepageState createState() => _HomepageState();
}
class _HomepageState extends State<HomePage> {
static double birdYaxis = 0;
double time = 0;
double height = 0;
double initialHeight = birdYaxis;
bool gameHasStarted = false;
static double barrierXone = 1;
double barrierXtwo = barrierXone + 1.5;
int score = 0;
int highscore = 10;
void jump() {
setState(() {
time = 0;
initialHeight = birdYaxis;
});
}
void startGame() {
gameHasStarted = true;
Timer.periodic(Duration(milliseconds: 60), (timer) {
time += 0.05;
height = -4.9 * time * time + 2.8 * time;
setState(() {
birdYaxis = initialHeight - height;
});
setState(() {
if (barrierXone < -2) {
barrierXone += 3.5;
} else {
barrierXone -= 0.05;
}
});
setState(() {
if (barrierXtwo < -2) {
barrierXtwo += 3.5;
score++;
} else {
barrierXtwo -= 0.05;
}
});
setState(() {
if (barrierXone == 0 || barrierXtwo == 0) {
score++;
}
});
if (birdYaxis > 1) {
timer.cancel();
gameHasStarted = false;
}
});
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
if (gameHasStarted) {
jump();
} else {
startGame();
}
},
child: Scaffold(
body: Column(
children: [
Expanded(
flex: 2,
child: Stack(
children: [
AnimatedContainer(
alignment: Alignment(0, birdYaxis),
duration: Duration(milliseconds: 0),
color: Colors.blue,
child: MyBird(),
),
Container(
alignment: Alignment(0, -0.3),
child: gameHasStarted
? Text(" ")
: Text(
"T A P T O P L A Y",
style: TextStyle(fontSize: 20, color: Colors.white),
),
),
AnimatedContainer(
alignment: Alignment(barrierXone, 1.1),
duration: Duration(milliseconds: 0),
child: MyBarrier(
size: 200.0,
),
),
AnimatedContainer(
alignment: Alignment(barrierXone, -1.1),
duration: Duration(milliseconds: 0),
child: MyBarrier(
size: 200.0,
),
),
AnimatedContainer(
alignment: Alignment(barrierXtwo, 1.1),
duration: Duration(milliseconds: 0),
child: MyBarrier(
size: 150.0,
),
),
AnimatedContainer(
alignment: Alignment(barrierXtwo, -1.1),
duration: Duration(milliseconds: 0),
child: MyBarrier(
size: 250.0,
),
)
],
),
),
Container(
height: 15,
color: Colors.green,
),
Expanded(
child: Container(
color: Colors.brown,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("SCORE",
style: TextStyle(color: Colors.white, fontSize: 35)),
SizedBox(
height: 20,
),
Text(score.toString(),
style: TextStyle(color: Colors.white, fontSize: 35)),
],
),
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("BEST",
style: TextStyle(color: Colors.white, fontSize: 35)),
SizedBox(
height: 20,
),
Text(highscore.toString(),
style: TextStyle(color: Colors.white, fontSize: 35)),
],
)
],
),
))
],
)),
);
}
}
Since your variable is of type int, you need to use ${} , to perform the assignment.由于您的变量是 int 类型,因此您需要使用${}来执行分配。
you can assign the value of a variable to a Text, like this:您可以将变量的值分配给文本,如下所示:
Text ("${score}");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.