button
button
▲▼▲▼▲▼
tags : #coding #flutter
references : Widget
▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼
▲▼▲▼▲▼
Design Concept: A minimalist, flat button with only text and no elevation. Ideal for actions that
are less prominent and secondary in nature.
When to Use: For inline actions, dialog buttons, or actions where you don’t want to draw too
much attention.
TextButton(
onPressed: () => print("Action executed"),
child: Text("Proceed", style: TextStyle(color: Colors.blue)),
)
2. ElevatedButton
Design Concept: A raised button with elevation, offering a floating effect that emphasizes
importance. The shadow creates a sense of depth, guiding users toward key actions.
When to Use: For primary actions that demand focus, like submitting forms, or saving data.
ElevatedButton(
onPressed: () => print("Submitted"),
child: Text("Submit"),
style: ElevatedButton.styleFrom(
primary: Colors.green, // Background color
onPrimary: Colors.white, // Text color
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
)
Customization: You can refine the appearance by adjusting colors, padding, shape, and
shadows.
Creative Enhancement: Use gradient backgrounds or add subtle animations (e.g., fading,
ripple effects) to make the button feel more dynamic.
3. OutlinedButton
Design Concept: A button with a border but no solid background. Less visually dominant than
an ElevatedButton but still offers interactivity through its border styling.
When to Use: For secondary or less important actions where you want them to remain visible
but not take center stage.
OutlinedButton(
onPressed: () => print("Cancelled"),
child: Text("Cancel"),
style: OutlinedButton.styleFrom(
side: BorderSide(color: Colors.red, width: 2),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
)
Customization: You can fine-tune the border style, color, and padding to match the theme
of your app.
4. IconButton
Design Concept: A button that uses only an icon, often used for minimalistic actions like
navigation or toggling options.
When to Use: For actions like "back", "delete", or "favorite", typically when space is limited or
when you want to simplify the UI.
IconButton(
onPressed: () => print("Icon Button Clicked"),
icon: Icon(Icons.favorite_border, color: Colors.pink),
)
Customization: You can integrate custom icons, color transitions, and tooltips for clarity.
Creative Enhancement: Add animated icon effects or change the icon’s color dynamically
based on state changes.
5. FloatingActionButton (FAB)
Design Concept: A circular button that "floats" above the UI, used for primary actions that need
high visibility. The FAB offers a natural center for attention and is often used for adding items.
When to Use: For actions like adding new items, starting new processes, or any other actions
that need to be prominent.
FloatingActionButton(
onPressed: () => print("FAB Clicked"),
child: Icon(Icons.add),
backgroundColor: Colors.purple,
)
Customization: FABs can be combined with animated transitions, shadows, and scale
effects for a more interactive feel.
Advanced Customization
Button Styling and Design
Flutter allows you to deeply customize button behavior and appearance. To craft a visually
compelling button, pay attention to key aspects such as shape, elevation, color, and size.
ElevatedButton(
onPressed: () {},
style: ElevatedButton.styleFrom(
primary: Colors.deepPurple, // Background color
onPrimary: Colors.white, // Text color
minimumSize: Size(200, 50), // Button size
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30), // Smooth corners
),
elevation: 5, // Subtle shadow for depth
),
child: Text("Create Account"),
)
Elegant Interactions
Subtle Animations
Enhancing button interactions with animations offers an elegant way to engage users. For
example, an elevated button can be animated to grow in size on tap for added responsiveness.
AnimatedContainer(
duration: Duration(milliseconds: 200),
height: _isPressed ? 60 : 50, // Increase size on press
width: _isPressed ? 220 : 200, // Adjust width accordingly
child: ElevatedButton(
onPressed: () {
setState(() {
_isPressed = !_isPressed;
});
},
child: Text("Press Me"),
),
)
InkWell(
onTap: () => print("Custom Button Pressed"),
splashColor: Colors.blue.withOpacity(0.5),
child: Container(
padding: EdgeInsets.symmetric(vertical: 14, horizontal: 30),
decoration: BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.circular(12),
),
child: Text(
"Tap Me",
style: TextStyle(color: Colors.white),
),
),
)
ElevatedButton(
onPressed: () => print("Action"),
style: ElevatedButton.styleFrom(
primary: Colors.teal,
shadowColor: Colors.black.withOpacity(0.5), // Customize shadow color
elevation: 8,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15),
),
),
child: Text("Action Button"),
)
Behavioral Aspects
Button States
Buttons often need to change state based on user interactions. Flutter provides flexibility in
managing state, such as disabling buttons or providing loading indicators during background
operations.
ElevatedButton(
onPressed: _isLoading ? null : () {},
child: _isLoading ? CircularProgressIndicator() : Text("Submit"),
)
Loading States: You can replace the text with a loading spinner or a progress bar to
indicate that a background task is in progress.
Conclusion
Buttons are essential UI elements that drive user interactions, and in Flutter, the possibilities for
crafting them are endless. By customizing buttons thoughtfully — from appearance to behavior
— you can ensure that your app feels polished, intuitive, and visually captivating. Using subtle
animations, adjusting shadows, and incorporating feedback mechanisms create an elegant
experience that enhances usability.
Feel free to experiment with these principles and create buttons that not only look beautiful but
also enhance user experience through seamless and engaging interactions.
Let me know if you'd like further insights or if there's a specific aspect you'd like to explore!