Using ElevatedButton and TextButton in Flutter apps has minimal impact on frame rate and memory when used properly. These buttons are lightweight widgets optimized by Flutter's rendering engine. They support smooth animations at 60fps or higher on modern devices. However, excessive nesting or complex styling can slightly affect rendering speed and battery life.
ElevatedButton and TextButton in Flutter - Build, Publish & Deploy
- Reuse button styles with
ButtonStyleto avoid rebuilding styles every frame. - Use const constructors for buttons when possible to reduce widget rebuilds.
- Limit heavy animations inside buttons; prefer simple color or elevation changes.
- Keep button trees shallow and avoid unnecessary wrappers.
ElevatedButton and TextButton are part of Flutter's material library, which adds a moderate size to the app bundle (~1-2MB). This is usually acceptable for most apps. Using these buttons does not significantly affect startup time. To keep app size small, avoid importing unused material components and consider tree shaking by Flutter's build system.
Both buttons behave consistently across iOS and Android because Flutter renders them using its own engine. However, the visual style follows Material Design by default, which is native to Android. On iOS, these buttons may look less native compared to Cupertino buttons. For a native iOS feel, consider using CupertinoButton or platform adaptive widgets.
- Accessibility: Ensure buttons have semantic labels for screen readers.
- Touch Target Size: Buttons should be at least 48x48 logical pixels for easy tapping.
- Visual Feedback: Provide clear pressed and disabled states to meet platform guidelines.
- Privacy: Buttons must not trigger unexpected permissions or data collection.
- Performance: Avoid janky animations or slow responses that can cause app rejection.
Your app takes 5 seconds to load a screen with multiple ElevatedButtons and TextButtons. What is likely wrong?
- Excessive widget rebuilds due to non-const buttons and styles.
- Heavy animations or complex styling inside buttons causing slow rendering.
- Too many nested widgets increasing build time.
- Not using Flutter's widget caching or const constructors.
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
