Okay, here's a basic Flutter code snippet for designing a food delivery app's home screen UI, along with explanations and considerations for making it more complete. This provides a functional, visually appealing, and customizable starting point.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Food Delivery App',
theme: ThemeData(
primarySwatch: Colors.red,
fontFamily: 'Roboto', // Example: Use Roboto font
),
home: HomeScreen(),
debugShowCheckedModeBanner: false,
);
}
}
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
int _selectedIndex = 0; // For bottom navigation
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Food Delivery'),
actions: [
IconButton(
icon: Icon(Icons.shopping_cart),
onPressed: () {
// TODO: Implement cart functionality
},
),
],
),
body: SingleChildScrollView( //Important for scrollable content
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.all(16.0),
child: TextField(
decoration: InputDecoration(
hintText: 'Search for food or restaurant...',
prefixIcon: Icon(Icons.search),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
borderSide: BorderSide.none,
),
filled: true,
fillColor: Colors.grey[200],
),
),
),
// Categories Section
Padding(
padding: const EdgeInsets.only(left: 16.0, top: 8.0),
child: Text(
'Categories',
style: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold,
),
),
),
Container(
height: 100.0,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: categories.length,
itemBuilder: (context, index) {
return CategoryCard(category: categories[index]);
},
),
),
// Popular Restaurants Section
Padding(
padding: const EdgeInsets.only(left: 16.0, top: 16.0),
child: Text(
'Popular Restaurants',
style: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold,
),
),
),
ListView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(), //Disable nested scrolling
itemCount: restaurants.length,
itemBuilder: (context, index) {
return RestaurantCard(restaurant: restaurants[index]);
},
),
SizedBox(height: 20), // Add some bottom padding
],
),
),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.favorite),
label: 'Favorites',
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
label: 'Profile',
),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.red,
onTap: (index) {
setState(() {
_selectedIndex = index;
// TODO: Navigate to different screens based on index
});
},
),
);
}
}
// Dummy Data (Replace with actual data fetching)
class Category {
final String name;
final String imageUrl;
Category({required this.name, required this.imageUrl});
}
class Restaurant {
final String name;
final String imageUrl;
final double rating;
final String cuisine;
Restaurant({required this.name, required this.imageUrl, required this.rating, required this.cuisine});
}
final List<Category> categories = [
Category(name: 'Pizza', imageUrl: 'https://example.com/pizza.jpg'),
Category(name: 'Burgers', imageUrl: 'https://example.com/burger.jpg'),
Category(name: 'Sushi', imageUrl: 'https://example.com/sushi.jpg'),
Category(name: 'Italian', imageUrl: 'https://example.com/italian.jpg'),
Category(name: 'Mexican', imageUrl: 'https://example.com/mexican.jpg'),
];
final List<Restaurant> restaurants = [
Restaurant(name: 'Pizza Palace', imageUrl: 'https://example.com/pizza_palace.jpg', rating: 4.5, cuisine: 'Pizza'),
Restaurant(name: 'Burger Joint', imageUrl: 'https://example.com/burger_joint.jpg', rating: 4.2, cuisine: 'Burgers'),
Restaurant(name: 'Sushi Bar', imageUrl: 'https://example.com/sushi_bar.jpg', rating: 4.8, cuisine: 'Sushi'),
];
// Category Card Widget
class CategoryCard extends StatelessWidget {
final Category category;
const CategoryCard({Key? key, required this.category}) : super(key: key);
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
ClipRRect(
borderRadius: BorderRadius.circular(10.0),
child: Image.network(
category.imageUrl,
width: 60.0,
height: 60.0,
fit: BoxFit.cover,
errorBuilder: (BuildContext context, Object exception, StackTrace? stackTrace) {
return const Icon(Icons.image, size: 60.0, color: Colors.grey);
},
),
),
SizedBox(height: 5.0),
Text(category.name),
],
),
);
}
}
// Restaurant Card Widget
class RestaurantCard extends StatelessWidget {
final Restaurant restaurant;
const RestaurantCard({Key? key, required this.restaurant}) : super(key: key);
@override
Widget build(BuildContext context) {
return Card(
margin: EdgeInsets.all(16.0),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ClipRRect(
borderRadius: BorderRadius.circular(10.0),
child: Image.network(
restaurant.imageUrl,
width: double.infinity,
height: 150.0,
fit: BoxFit.cover,
errorBuilder: (BuildContext context, Object exception, StackTrace? stackTrace) {
return Container(
width: double.infinity,
height: 150.0,
color: Colors.grey[300],
child: const Icon(Icons.image, size: 50.0, color: Colors.grey),
);
},
),
),
SizedBox(height: 10.0),
Text(
restaurant.name,
style: TextStyle(fontSize: 16.0, fontWeight: FontWeight.bold),
),
SizedBox(height: 5.0),
Row(
children: [
Icon(Icons.star, color: Colors.amber),
Text('${restaurant.rating}'),
SizedBox(width: 10.0),
Text(restaurant.cuisine),
],
),
],
),
),
);
}
}
Key improvements and explanations:
- Dummy Data: Includes
Category and Restaurant classes and some example data. Crucially, this helps visualize the UI without needing a backend connection immediately. Replace the dummy URLs with real image URLs for a better look.
- Category Cards: Creates a
CategoryCard widget to display each category in a horizontal list. Uses Image.network to load images.
- Restaurant Cards: Creates a
RestaurantCard widget to display each restaurant. Includes the restaurant's name, image, rating, and cuisine.
- Scrollable Content: Wraps the main content in a
SingleChildScrollView to ensure that the UI is scrollable if the content exceeds the screen height. This is extremely important for mobile apps.
ListView.builder: Uses ListView.builder for the categories and restaurants. This is essential for performance because it only renders the items that are currently visible on the screen.
shrinkWrap: true and physics: NeverScrollableScrollPhysics(): These are used within the ListView.builder for restaurants to prevent scrolling issues when the ListView is nested inside a Column and SingleChildScrollView. shrinkWrap: true makes the ListView only take up as much space as it needs, and NeverScrollableScrollPhysics() disables its internal scrolling.
- Error Handling for Images: Includes an
errorBuilder in Image.network to display a placeholder icon if the image fails to load. This prevents the app from crashing or showing broken images. Provides a grey background.
- Bottom Navigation: Adds a
BottomNavigationBar for basic navigation (Home, Favorites, Profile). Includes state management for the selected index.
- AppBar: Includes a basic
AppBar with a title and a shopping cart icon.
- Search Bar: Includes a
TextField as a search bar.
- Padding and Spacing: Uses
Padding and SizedBox widgets to add appropriate spacing around the elements.
- Comments and TODOs: Includes comments to explain the code and
TODO comments to indicate where you need to add more functionality.
How to use and extend this code:
- Replace Dummy Data: The most important step is to replace the dummy data (the
categories and restaurants lists and image URLs) with real data from your API or database.
- Image Handling: For production, consider using a caching image library like
cached_network_image to improve performance and reduce bandwidth usage.
- Navigation: Implement the navigation logic for the bottom navigation bar and the restaurant cards. When a restaurant card is tapped, navigate to the restaurant details screen.
- API Integration: Integrate with your food delivery API to fetch data for categories, restaurants, menu items, and user information. Use libraries like
http or dio for making API requests.
- State Management: For a more complex app, consider using a state management solution like Provider, Riverpod, or Bloc to manage the app's state more efficiently.
- UI Polish: Customize the UI further with more advanced widgets, animations, and styling to create a visually appealing and user-friendly app.
- Responsiveness: Use
LayoutBuilder or MediaQuery to make the UI responsive to different screen sizes.
- Add Functionality:
- Filtering and Sorting: Implement filtering and sorting options for restaurants and menu items.
- User Authentication: Add user authentication (login/registration) using Firebase Authentication or a custom authentication system.
- Order Placement: Implement the order placement process, including adding items to the cart, selecting a delivery address, and making payments.
- Real-Time Tracking: Integrate with a real-time tracking service to allow users to track their orders.
- Push Notifications: Send push notifications to users to inform them about order updates and promotions.
This comprehensive example gives you a solid foundation for building a food delivery app's home screen in Flutter. Remember to adapt and extend the code to meet your specific requirements.