Lesson 6
🎯 Goal
Build True/False buttons on your own Training Page.
You’ll write the code yourself, using the team’s real code as your guide.
New ideas (just two):
- State is a value the page remembers, like
bool climbed = false;.- If you change state, you must tell Flutter to redraw: wrap it in
setState(() { ... }). Forget this and the screen won’t update.
Task 0: Your page branch
You’ll keep working on this one branch for Lessons 6 to 9.
Do The Pull First Rule from Lesson 3.
Open lib/Training/TrainingPage.dart. Read the whole file. Find the heading LESSON 6: TRUE / FALSE. That’s where your code goes.
Task 1: Learn from the real app
Open Auton.dart (you know where it is!) and find the Climb checkbox.
In your doc, answer:
- Which variable remembers whether Climb is on?
Task 2: Your first toggle
In TrainingPage.dart, add a variable with your other variables:
bool climbed = false;Then add this under the Lesson 6 heading. Fill in the three blanks (____):
buildCheckBoxFull('Climbed?', ____, (bool value) {
____(() {
____ = value;
});
}),Hot restart (you added a variable). Tap it.
✅ Checkpoint: the checkbox switches on and off.
Commit: Lesson 6: add Climbed toggle
Task 3: Build your own button
buildCheckBoxFull is the team’s. Now make your own, from scratch, using a plain ElevatedButton.
The spec:
- It’s called Defense. It remembers a
bool defense, startingfalse. - When
false, it’s red and saysDefense: NO. - When
true, it’s green and saysDefense: YES. - Tapping it flips the value. (
!means NOT:!trueisfalse.)
Try for 10 minutes before you open the hint.
Stuck? Open the hint
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: defense ? Colors.green : Colors.red,
),
onPressed: () {
setState(() {
defense = !defense;
});
},
child: Text(defense ? 'Defense: YES' : 'Defense: NO'),
),The ? : is a shortcut for if/else: “if defense, green, otherwise red.”
✅ Checkpoint: it flips, changes color, and changes words.
Commit.
Task 4: Another toggle, and a total
- Add a third toggle using
buildCheckBoxFull: Left starting zone? (bool leftZone). - Write a function that counts how many of your three values are
true:
int countYes() {
// your code here
}Use int, if, and ++ from Module 1.
3. Show it on the page with a Text: Yes answers: 2 (use $ and ${...}).
✅ Checkpoint: tap the toggles and the number changes every time.
Commit.
Task 5: Make it yours
Pick two and do them:
- The Defense button turns orange after you’ve tapped it more than 3 times.
- Add a message:
All three!appears only whencountYes()is 3. - Add a Reset button that sets all three back to
false. - Change the Defense button’s shape or text size.
To complete this lesson
Paste into this lesson’s tab in your training doc (text, no screenshots):
- Your answers to Task 1
- The link to your last commit for this lesson
- Your code for
countYes() - Which two things did you pick in Task 5?