Challenge - 5 Problems
Button Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What happens when you press this ElevatedButton?
Consider this Flutter code snippet:
What will be the visible effect and console output when the button is tapped?
ElevatedButton(
onPressed: () => print('Pressed!'),
child: Text('Click me'),
)What will be the visible effect and console output when the button is tapped?
Flutter
ElevatedButton( onPressed: () => print('Pressed!'), child: Text('Click me'), )
Attempts:
2 left
💡 Hint
Think about what onPressed does and how ElevatedButton shows feedback.
✗ Incorrect
ElevatedButton shows a ripple effect on tap and runs the onPressed callback, printing 'Pressed!' in the console.
📝 Syntax
intermediate2:00remaining
Which option correctly creates a TextButton with a blue text color?
You want a TextButton with the label 'Submit' and the text color blue. Which code snippet is correct?
Attempts:
2 left
💡 Hint
Check the latest Flutter style property for text color in TextButton.styleFrom.
✗ Incorrect
In recent Flutter versions, TextButton.styleFrom uses foregroundColor to set the text color. Using primary or textColor causes errors or is deprecated.
❓ lifecycle
advanced2:00remaining
What happens if you set onPressed to null on an ElevatedButton?
Given this code:
What is the button's behavior and appearance?
ElevatedButton(
onPressed: null,
child: Text('Disabled'),
)What is the button's behavior and appearance?
Flutter
ElevatedButton( onPressed: null, child: Text('Disabled'), )
Attempts:
2 left
💡 Hint
Think about how Flutter treats null callbacks for buttons.
✗ Incorrect
Setting onPressed to null disables the button. It appears faded and does not respond to user taps.
advanced
2:00remaining
Which button triggers navigation to a new screen when tapped?
You want to navigate to a new screen called DetailScreen when a button is tapped. Which code snippet correctly does this?
Flutter
ElevatedButton(
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(builder: (_) => DetailScreen()));
},
child: Text('Go'),
)Attempts:
2 left
💡 Hint
To go to a new screen, you push a route onto the Navigator stack.
✗ Incorrect
Navigator.of(context).push(...) pushes a new screen. pop() removes the current screen. Setting onPressed to null disables the button.
🔧 Debug
expert2:00remaining
Why does this ElevatedButton code cause a runtime error?
Examine this code:
Assuming 'message' is not declared anywhere, what error occurs when tapping the button?
ElevatedButton(
onPressed: () => print(message),
child: Text('Press'),
)Assuming 'message' is not declared anywhere, what error occurs when tapping the button?
Flutter
ElevatedButton( onPressed: () => print(message), child: Text('Press'), )
Attempts:
2 left
💡 Hint
Check if all variables used inside onPressed are declared.
✗ Incorrect
Using an undeclared variable 'message' causes a runtime error when the button is pressed because the callback tries to access an undefined identifier.