Reputation: 13
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.
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)),
],
)
],
),
))
],
)),
);
}
}
Upvotes: 1
Views: 650
Reputation: 378
Since your variable is of type int, you need to use ${}, to perform the assignment.
you can assign the value of a variable to a Text, like this:
Text ("${score}");
Upvotes: 2