0
0
Fluttermobile~20 mins

ElevatedButton and TextButton in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Button Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What happens when you press this ElevatedButton?
Consider this Flutter code snippet:
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'),
)
AThe button disappears from the screen after tapping.
BThe button shows a ripple effect and prints 'Pressed!' in the console.
CNothing happens visually or in the console because onPressed is not set.
DThe button changes color permanently and prints 'Clicked!' in the console.
Attempts:
2 left
💡 Hint
Think about what onPressed does and how ElevatedButton shows feedback.
📝 Syntax
intermediate
2: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?
A
TextButton(
  onPressed: () {},
  style: TextButton.styleFrom(primary: Colors.blue),
  child: Text('Submit'),
)
B
TextButton(
  onPressed: () {},
  style: TextButton.styleFrom(textColor: Colors.blue),
  child: Text('Submit'),
)
C
TextButton(
  onPressed: () {},
  child: Text('Submit', style: TextStyle(color: Colors.blue)),
)
D
TextButton(
  onPressed: () {},
  style: TextButton.styleFrom(foregroundColor: Colors.blue),
  child: Text('Submit'),
)
Attempts:
2 left
💡 Hint
Check the latest Flutter style property for text color in TextButton.styleFrom.
lifecycle
advanced
2:00remaining
What happens if you set onPressed to null on an ElevatedButton?
Given this code:
ElevatedButton(
  onPressed: null,
  child: Text('Disabled'),
)

What is the button's behavior and appearance?
Flutter
ElevatedButton(
  onPressed: null,
  child: Text('Disabled'),
)
AThe button is disabled: it looks faded and does not respond to taps.
BThe button is enabled and prints an error when tapped.
CThe button is invisible and takes no space on screen.
DThe button is enabled but does nothing when tapped.
Attempts:
2 left
💡 Hint
Think about how Flutter treats null callbacks for buttons.
navigation
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'),
)
AElevatedButton with onPressed calling Navigator.of(context).pop()
BTextButton with onPressed calling Navigator.pop(context)
CElevatedButton with onPressed calling Navigator.of(context).push(MaterialPageRoute(builder: (_) => DetailScreen()))
DTextButton with onPressed set to null
Attempts:
2 left
💡 Hint
To go to a new screen, you push a route onto the Navigator stack.
🔧 Debug
expert
2:00remaining
Why does this ElevatedButton code cause a runtime error?
Examine this code:
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'),
)
ARuntime error: 'message' is not defined (NameError or NoSuchMethodError).
BCompile-time error: missing semicolon after print statement.
CNo error; prints 'null' in the console.
DThe button does not render because of a syntax error.
Attempts:
2 left
💡 Hint
Check if all variables used inside onPressed are declared.