Flutter / UI Elements / Button
Button
-
Code
ElevatedButton
TextButtonimport 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: Text('Flutter Button Example'), ), body: Center( child: ElevatedButton( onPressed: () { // Handle button press print('Button Pressed!'); }, child: Text('Press me'), ), ), ), ); } }
OutlinedButton:import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: Text('Flutter Button Example'), ), body: Center( child: TextButton( onPressed: () { // Handle button press print('Button Pressed!'); }, child: Text('Press me'), ), ), ), ); } } import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: Text('Flutter Button Example'), ), body: Center( child: OutlinedButton( onPressed: () { // Handle button press print('Button Pressed!'); }, child: Text('Press me'), ), ), ), ); } } import 'package:flutter/material.dart'; class MyButtonDemo extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('Button Demo')), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ ElevatedButton( onPressed: () { // Add onPressed action here }, child: Text('Elevated Button'), ), SizedBox(height: 20), TextButton( onPressed: () { // Add onPressed action here }, child: Text('Text Button'), ), SizedBox(height: 20), OutlinedButton( onPressed: () { // Add onPressed action here }, child: Text('Outlined Button'), ), ], ), ), ); } } -
Usage
1. Column Layout
Column( mainAxisAlignment: MainAxisAlignment.center, children: [ ElevatedButton( onPressed: () { // Add onPressed action here }, child: Text('Button 1'), ), SizedBox(height: 20), ElevatedButton( onPressed: () { // Add onPressed action here }, child: Text('Button 2'), ), ], ) 2. Row Layout
Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ ElevatedButton( onPressed: () { // Add onPressed action here }, child: Text('Button 1'), ), ElevatedButton( onPressed: () { // Add onPressed action here }, child: Text('Button 2'), ), ], ) 3. Grid Layout
GridView.count( crossAxisCount: 2, children: [ ElevatedButton( onPressed: () { // Add onPressed action here }, child: Text('Button 1'), ), ElevatedButton( onPressed: () { // Add onPressed action here }, child: Text('Button 2'), ), // Add more buttons as needed ], ) 4. Wrap Layout
Wrap( spacing: 10.0, runSpacing: 10.0, children: [ ElevatedButton( onPressed: () { // Add onPressed action here }, child: Text('Button 1'), ), ElevatedButton( onPressed: () { // Add onPressed action here }, child: Text('Button 2'), ), // Add more buttons as needed ], ) -
Reusable Button
Custom class
Usageimport 'package:flutter/material.dart'; class CustomButton extends StatelessWidget { final String text; final VoidCallback onPressed; final Color? color; final Color? textColor; final double borderRadius; final double padding; const CustomButton({ Key? key, required this.text, required this.onPressed, this.color, this.textColor, this.borderRadius = 8.0, this.padding = 16.0, }) : super(key: key); @override Widget build(BuildContext context) { return ElevatedButton( onPressed: onPressed, style: ElevatedButton.styleFrom( primary: color ?? Theme.of(context).primaryColor, onPrimary: textColor ?? Colors.white, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(borderRadius), ), padding: EdgeInsets.all(padding), ), child: Text(text), ); } } class MyWidget extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( body: Center( child: CustomButton( text: 'Press me', onPressed: () { // Add onPressed action here }, color: Colors.blue, textColor: Colors.white, borderRadius: 10.0, padding: 20.0, ), ), ); } }