What if you could add perfect buttons with just one line of code?
Why ElevatedButton and TextButton in Flutter? - Purpose & Use Cases
Imagine you want to add buttons to your app by drawing them pixel by pixel or placing images for each button manually.
You have to handle all the touch areas, colors, shadows, and states yourself.
This manual way is slow and tricky.
You might forget to change the button color when pressed or miss making it look clickable.
It's easy to make buttons that don't look or feel right on different devices.
Flutter's ElevatedButton and TextButton give you ready-made buttons that look good and behave correctly.
They handle colors, shadows, and touch feedback automatically.
You just write a few lines and get nice buttons that work everywhere.
GestureDetector(onTap: () { print('Clicked'); }, child: Container(width: 100, height: 40, color: Colors.blue))ElevatedButton(onPressed: () { print('Clicked'); }, child: Text('Click me'))You can quickly add beautiful, responsive buttons that follow design rules without extra work.
When making a login screen, you use ElevatedButton for the main "Sign In" button and TextButton for "Forgot Password?" links, ensuring clear and consistent user actions.
Manual button creation is slow and error-prone.
ElevatedButton and TextButton provide ready-to-use, stylish buttons.
They save time and improve app look and feel automatically.
Practice
Solution
Step 1: Understand button types in Flutter
ElevatedButton has a background and shadow, TextButton is flat with no background.Step 2: Identify the button with background and shadow
ElevatedButton is designed to have elevation and background color by default.Final Answer:
ElevatedButton -> Option DQuick Check:
ElevatedButton = background + shadow [OK]
- Confusing TextButton as having background
- Thinking IconButton has background by default
- Using deprecated FlatButton
Solution
Step 1: Check correct property names
TextButton uses onPressed for tap events and child for its label widget.Step 2: Verify syntax correctness
TextButton(onPressed: () => print('Clicked'), child: Text('Click')) correctly uses onPressed with a lambda and child with Text widget.Final Answer:
TextButton(onPressed: () => print('Clicked'), child: Text('Click')) -> Option CQuick Check:
onPressed + child: Text = correct syntax [OK]
- Using onClick instead of onPressed
- Passing string directly as child instead of Text widget
- Calling print inside onPressed instead of passing a function
ElevatedButton(onPressed: () {}, child: Text('Press'))
TextButton(onPressed: () {}, child: Text('Press'))Solution
Step 1: Understand default styles of ElevatedButton and TextButton
ElevatedButton shows background color and shadow; TextButton is flat with no background.Step 2: Analyze the given code
Both have onPressed defined, so both are enabled and visible; their styles differ as per their types.Final Answer:
ElevatedButton has background and shadow; TextButton is flat with only text -> Option AQuick Check:
ElevatedButton = raised, TextButton = flat [OK]
- Thinking empty onPressed disables button
- Assuming TextButton has background by default
- Confusing visual styles of both buttons
ElevatedButton(
onPressed: print('Clicked'),
child: Text('Tap me'),
)Solution
Step 1: Check onPressed assignment
onPressed expects a function to call when pressed, not the result of a function call.Step 2: Identify the mistake
print('Clicked') is called immediately; should be wrapped in a lambda like () => print('Clicked').Final Answer:
onPressed should be a function, not a function call -> Option BQuick Check:
onPressed = function, not function call [OK]
- Calling print inside onPressed instead of passing a function
- Using string instead of Text widget for child
- Assuming ElevatedButton lacks onPressed
Solution
Step 1: Understand button disabling in Flutter
Setting onPressed to null disables the button visually and functionally.Step 2: Choose button style and disabling method
TextButton is flat; disabling it by setting onPressed to null after press fits the requirement.Step 3: Eliminate incorrect options
ElevatedButton is raised, not flat; child cannot disable button; onPressed non-null keeps button enabled.Final Answer:
TextButton with onPressed set to null after press -> Option AQuick Check:
Disable button by onPressed = null [OK]
- Thinking child null disables button
- Using ElevatedButton for flat style
- Not setting onPressed to null to disable
Ready to prove it?
Pass the test and this pattern moves unknown
