To create a Flutter E-Commerce themed homepage with three distinct buttons (Lihat Daftar Produk, Tambah Produk, and Logout), each having different colors and showing specific Snackbar messages when pressed, follow the steps below. The provided code is a complete main.dart file that you can run directly in your Flutter project.
Step-by-Step Guide
-
Set Up the Flutter Project:
-
Modify main.dart:
- Replace the contents of
lib/main.dart with the following code.
Complete main.dart Code
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// Root of the application
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'E-Commerce App',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Homepage(),
debugShowCheckedModeBanner: false, // Removes the debug banner
);
}
}
class Homepage extends StatelessWidget {
// Function to display Snackbar
void _showSnackbar(BuildContext context, String message) {
final snackBar = SnackBar(
content: Text(message),
duration: Duration(seconds: 2),
);
// Remove any current Snackbars before showing a new one
ScaffoldMessenger.of(context)
..removeCurrentSnackBar()
..showSnackBar(snackBar);
}
@override
Widget build(BuildContext context) {
// Define button styles
final ButtonStyle buttonStyle = ElevatedButton.styleFrom(
minimumSize: Size(double.infinity, 50), // Makes buttons full width
textStyle: TextStyle(fontSize: 16),
);
return Scaffold(
appBar: AppBar(
title: Text('E-Commerce Homepage'),
centerTitle: true,
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
// Align buttons to the center vertically
mainAxisAlignment: MainAxisAlignment.center,
children: [
// Button 1: Lihat Daftar Produk
ElevatedButton.icon(
style: buttonStyle.copyWith(
backgroundColor: MaterialStateProperty.all<Color>(Colors.blue),
),
icon: Icon(Icons.view_list),
label: Text('Lihat Daftar Produk'),
onPressed: () {
_showSnackbar(
context, 'Kamu telah menekan tombol Lihat Daftar Produk');
},
),
SizedBox(height: 20), // Spacing between buttons
// Button 2: Tambah Produk
ElevatedButton.icon(
style: buttonStyle.copyWith(
backgroundColor:
MaterialStateProperty.all<Color>(Colors.green),
),
icon: Icon(Icons.add_shopping_cart),
label: Text('Tambah Produk'),
onPressed: () {
_showSnackbar(
context, 'Kamu telah menekan tombol Tambah Produk');
},
),
SizedBox(height: 20), // Spacing between buttons
// Button 3: Logout
ElevatedButton.icon(
style: buttonStyle.copyWith(
backgroundColor:
MaterialStateProperty.all<Color>(Colors.redAccent),
),
icon: Icon(Icons.logout),
label: Text('Logout'),
onPressed: () {
_showSnackbar(context, 'Kamu telah menekan tombol Logout');
},
),
],
),
),
);
}
}
Explanation of the Code
-
Main Function and MyApp:
- The
main function runs the app by calling runApp(MyApp()).
MyApp is a StatelessWidget that sets up the MaterialApp with a title, theme, and Homepage as the home screen.
debugShowCheckedModeBanner is set to false to remove the debug banner.
-
Homepage Widget:
Homepage is a StatelessWidget representing the main screen.
- It contains an
AppBar with the title "E-Commerce Homepage".
- The body has padding and a
Column that centers three buttons vertically.
-
Buttons:
- Button Style: A base
ButtonStyle is defined to make all buttons full-width with a minimum height and consistent text style.
- Lihat Daftar Produk:
- Uses the
Icons.view_list icon.
- Colored Blue.
- On pressed, shows a
Snackbar with the message "Kamu telah menekan tombol Lihat Daftar Produk".
- Tambah Produk:
- Uses the
Icons.add_shopping_cart icon.
- Colored Green.
- On pressed, shows a
Snackbar with the message "Kamu telah menekan tombol Tambah Produk".
- Logout:
- Uses the
Icons.logout icon.
- Colored RedAccent.
- On pressed, shows a
Snackbar with the message "Kamu telah menekan tombol Logout".
-
Snackbar Function:
_showSnackbar is a helper method to display SnackBar messages.
- It first removes any existing
SnackBar to prevent stacking.
- Then, it shows the new
SnackBar with the provided message and a duration of 2 seconds.
-
Styling and Layout:
SizedBox widgets are used to add vertical spacing between the buttons.
Running the App
-
Ensure Dependencies are Installed:
- Run
flutter pub get to ensure all dependencies are installed.
-
Run the App:
- Use the command:
flutter run
- Select your desired device or emulator.
-
Interact with the Buttons:
- Tap each button to see the corresponding
Snackbar message at the bottom of the screen.
Screenshots
Below are example screenshots of how the app should look:
Homepage with Buttons:
[image: Homepage] open
Snackbar Example:
[image: Snackbar] open
(Note: Replace the image links with actual screenshots if available.)
Customization
- Icons: Feel free to choose different icons from Flutter's IconLibrary.
- Colors: Adjust the colors to match your desired theme or branding.
- Navigation: Currently, buttons only show
Snackbars. You can navigate to different screens by replacing the onPressed functions with Navigator.push methods.
Conclusion
This setup provides a simple yet functional homepage for an E-Commerce Flutter app with three interactive buttons. You can expand upon this foundation by adding actual navigation, product listings, authentication, and more features as needed for your project.