FlatButton对点击没有响应



我正在制作一个名为Destini的简单应用程序。它基本上就像一个带有一系列问题的游戏。例如,如果我问你喜欢茶还是咖啡,如果你回答咖啡,它会问我我喜欢什么类型的咖啡作为下一个问题,如果你喜欢茶,它会问我喜欢什么类型的茶,所以下一个问题取决于用户的回答。这里在我的应用程序,如果我点击按钮,它是不移动到下一个问题。我在我的项目中做了3个dart文件,其中一个包含问题列表。如果你能帮我找出错误,那就太有帮助了。

Main.dart

import 'package:destiini/story_brain.dart';
import 'package:flutter/material.dart';

//TODO: Step 15 - Run the app and see if you can see the screen update with the first story. Delete this TODO if it looks as you expected.
void main() => runApp(Destini());
class Destini extends StatelessWidget {
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark(),
home: StoryPage(),
);
}
}
//TODO: Step 9 - Create a new storyBrain object from the StoryBrain class.
StoryBrain storyBrain = new StoryBrain();
class StoryPage extends StatefulWidget {
_StoryPageState createState() => _StoryPageState();
}
class _StoryPageState extends State<StoryPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
decoration: BoxDecoration(
image:DecorationImage(
image: AssetImage('images/background.png'),
fit: BoxFit.cover,
),
),
//TODO: Step 1 - Add background.png to this Container as a background image.
padding: EdgeInsets.symmetric(vertical: 50.0, horizontal: 15.0),
constraints: BoxConstraints.expand(),
child: SafeArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
flex: 12,
child: Center(
child: Text(
//TODO: Step 10 - use the storyBrain to get the first story title and display it in this Text Widget.
storyBrain.getStory(),
style: TextStyle(
fontSize: 25.0,
),
),
),
),
Expanded(
flex: 2,
child: FlatButton(
onPressed: () {
//Choice 1 made by user.
setState(() {
storyBrain.nextStory(1);
});
//TODO: Step 18 - Call the nextStory() method from storyBrain and pass the number 1 as the choice made by the user.
},
color: Colors.red,
child: Text(
//TODO: Step 13 - Use the storyBrain to get the text for choice 1.
storyBrain.getChoice1(),
style: TextStyle(
fontSize: 20.0,
),
),
),
),
SizedBox(
height: 20.0,
),
Expanded(
flex: 2,
//TODO: Step 26 - Use a Flutter Visibility Widget to wrap this FlatButton.
//TODO: Step 28 - Set the "visible" property of the Visibility Widget to equal the output from the buttonShouldBeVisible() method in the storyBrain.
child: FlatButton(
onPressed: () {
setState(() {
storyBrain.nextStory(2);
});
//Choice 2 made by user.
//TODO: Step 19 - Call the nextStory() method from storyBrain and pass the number 2 as the choice made by the user.
},
color: Colors.blue,
child: Text(
storyBrain.getChoice2(),
//TODO: Step 14 - Use the storyBrain to get the text for choice 2.
style: TextStyle(
fontSize: 20.0,
),
),
),
),
],
),
),
),
);
}
}
//TODO: Step 24 - Run the app and try to figure out what code you need to add to this file to make the story change when you press on the choice buttons.
//TODO: Step 29 - Run the app and test it against the Story Outline to make sure you've completed all the steps. The code for the completed app can be found here: https://github.com/londonappbrewery/destini-challenge-completed/

Story.dart

class Story {
String storyTitle;
String choice1;
String choice2;
Story({this.storyTitle, this.choice1, this.choice2});
}

Story_brain.dart

import 'story.dart';
class StoryBrain {
List<Story> storyData = [
Story(
storyTitle:
'Your car has blown a tire on a winding road in the middle of nowhere with no cell phone reception. You decide to hitchhike. A rusty pickup truck rumbles to a stop next to you. A man with a wide brimmed hat with soulless eyes opens the passenger door for you and asks: "Need a ride, boy?".',
choice1: 'I'll hop in. Thanks for the help!',
choice2: 'Better ask him if he's a murderer first.'),
Story(
storyTitle: 'He nods slowly, unphased by the question.',
choice1: 'At least he's honest. I'll climb in.',
choice2: 'Wait, I know how to change a tire.'),
Story(
storyTitle:
'As you begin to drive, the stranger starts talking about his relationship with his mother. He gets angrier and angrier by the minute. He asks you to open the glovebox. Inside you find a bloody knife, two severed fingers, and a cassette tape of Elton John. He reaches for the glove box.',
choice1: 'I love Elton John! Hand him the cassette tape.',
choice2: 'It's him or me! You take the knife and stab him.'),
Story(
storyTitle:
'What? Such a cop out! Did you know traffic accidents are the second leading cause of accidental death for most adult age groups?',
choice1: 'Restart',
choice2: ''),
Story(
storyTitle:
'As you smash through the guardrail and careen towards the jagged rocks below you reflect on the dubious wisdom of stabbing someone while they are driving a car you are in.',
choice1: 'Restart',
choice2: ''),
Story(
storyTitle:
'You bond with the murderer while crooning verses of "Can you feel the love tonight". He drops you off at the next town. Before you go he asks you if you know any good places to dump bodies. You reply: "Try the pier".',
choice1: 'Restart',
choice2: '')
];
String getStory()
{
return storyData[storyNumber].storyTitle;
}
String getChoice1()
{
return storyData[storyNumber].choice1;
}
String getChoice2()
{
return storyData[storyNumber].choice2;
}
int storyNumber = 0;
void nextStory(int choiceNumber){
if(storyNumber==0 && choiceNumber==1)
{
storyNumber==2;
}
else if(storyNumber==2 && choiceNumber==1)
{
storyNumber==5;
}
else if(storyNumber==2 && choiceNumber==2)
{
storyNumber==4;
}
else if(storyNumber==0 && choiceNumber==2)
{
storyNumber==1;
}
else if (choiceNumber == 1 && storyNumber == 1) {
storyNumber = 2;
}
else if(storyNumber==1 && choiceNumber==2)
{
storyNumber==3;
}
else if(storyNumber==3||storyNumber==4||storyNumber==5)
{
restart();
}
}
void restart()
{
storyNumber=0;
}
}
//TODO: Step 23 - Use the storyNumber property inside getStory(), getChoice1() and getChoice2() so that it gets the updated story and choices rather than always just the first (0th) one.

//TODO: Step 25 - Change the storyNumber property into a private property so that only story_brain.dart has access to it. You can do this by right clicking on the name (storyNumber) and selecting Refactor -> Rename to make the change across all the places where it's used.
//TODO: Step 16 - Create a property called storyNumber which starts with a value of 0. This will be used to track which story the user is currently viewing.
//TODO: Step 17 - Create a method called nextStory(), it should not have any outputs but it should have 1 input called choiceNumber which will be the choice number (int) made by the user.
//TODO: Step 20 - Download the story plan here: https://drive.google.com/uc?export=download&id=1KU6EghkO9Hf2hRM0756xFHgNaZyGCou3
//TODO: Step 21 - Using the story plan, update nextStory() to change the storyNumber depending on the choice made by the user. e.g. if choiceNumber was equal to 1 and the storyNumber is 0, the storyNumber should become 2.
//TODO: Step 22 - In nextStory() if the storyNumber is equal to 3 or 4 or 5, that means it's the end of the game and it should call a method called restart() that resets the storyNumber to 0.
//TODO: Step 27 - Create a method called buttonShouldBeVisible() which checks to see if storyNumber is 0 or 1 or 2 (when both buttons should show choices) and return true if that is the case, else it should return false.

if条件的结构在这里显示

按此方式更改变量的值

if(storyNumber==0 && choiceNumber==1)
{
storyNumber=2; // <-- change == to =
}
else if(storyNumber==2 && choiceNumber==1)
{
storyNumber=5; // <-- change == to =
} ...

最新更新