Building Your First Flutter App: A Step-by-Step Tutorial

MD. Sad Adnan
6 min readMar 3, 2023

--

Flutter is a popular mobile app development framework that allows developers to build beautiful, high-performance mobile apps for both Android and iOS platforms. If you’re new to Flutter and want to get started building your first app, this tutorial is for you. In this tutorial, we’ll walk you through the process of building a simple app that allows users to browse and save their favorite recipes.

Step 1: Setting Up Your Development Environment

Before you can start building your app, you’ll need to set up your development environment. The first step is to download and install Flutter. You can find detailed instructions on the Flutter website for installing Flutter on your operating system of choice.

Once you have Flutter installed, you’ll need to configure your development environment. This typically involves installing the required tools and dependencies, such as Android Studio or Xcode. Again, you can find detailed instructions on the Flutter website for configuring your development environment.

Step 2: Creating a New Flutter Project

Once you have your development environment set up, you’re ready to create your first Flutter project. To do this, open your terminal or command prompt and enter the following command:

flutter create my_app

This command will create a new Flutter project named “my_app” in the current directory.

Step 3: Building the User Interface

Now that you have a new Flutter project, it’s time to start building the user interface. In this tutorial, we’ll be building a simple recipe app that allows users to browse and save their favorite recipes.

To start, open the lib/main.dart file in your project and replace the existing code with the following:

import 'package:flutter/material.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My Recipes',
home: Scaffold(
appBar: AppBar(
title: Text('My Recipes'),
),
body: Center(
child: Text('Welcome to my recipe app!'),
),
),
);
}
}

This code sets up a basic app with an app bar and a centered text widget that displays a welcome message.

Step 4: Adding Navigation

Next, we’ll add some navigation to our app so that users can browse their favorite recipes. We’ll start by creating a new RecipeListScreen widget that will display a list of recipes. Add the following code to your main.dart file:

class RecipeListScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('My Recipes'),
),
body: ListView.builder(
itemCount: 10,
itemBuilder: (context, index) {
return ListTile(
title: Text('Recipe ${index + 1}'),
onTap: () {
// TODO: Navigate to recipe details screen
},
);
},
),
);
}
}

This code sets up a basic ListView that displays 10 recipe items, each with a title and a onTap callback that will be called when the user taps on the item.

Now, let’s modify the MyApp widget to navigate RecipeListScreen when the app starts up. Modify the MyApp widget code as follows:

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My Recipes',
home: RecipeListScreen(),
);
}
}

This code sets the RecipeListScreen as the home screen for our app so that it is displayed when the app starts up.

Step 5: Adding Recipe Details

Now that we have a basic recipe list, let’s add a recipe details screen that displays more information about each recipe. Add the following code to your main.dart file:

class RecipeDetailsScreen extends StatelessWidget {
final int recipeIndex;

RecipeDetailsScreen({required this.recipeIndex});

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Recipe Details'),
),
body: Center(
child: Text('Recipe ${recipeIndex + 1} Details'),
),
);
}
}

This code sets up a new RecipeDetailsScreen widget that takes an integer recipeIndex as a parameter. This index is used to display the details of a specific recipe.

Now, let’s modify the RecipeListScreen to navigate to RecipeDetailsScreen when the user taps on a recipe. Modify the RecipeListScreen widget code as follows:

class RecipeListScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('My Recipes'),
),
body: ListView.builder(
itemCount: 10,
itemBuilder: (context, index) {
return ListTile(
title: Text('Recipe ${index + 1}'),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => RecipeDetailsScreen(recipeIndex: index)),
);
},
);
},
),
);
}
}

This code modifies the onTap callback for each recipe item to navigate to the RecipeDetailsScreen and pass in the index of the selected recipe.

Step 6: Final Touches

Finally, let’s add some final touches to our app. We’ll start by adding a background image to the app bar. Add the following code to your main.dart file:

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My Recipes',
theme: ThemeData(
appBarTheme: AppBarTheme(
backgroundColor: Colors.green,
foregroundColor: Colors.white,
backgroundImage: AssetImage('assets/images/header.jpg'),
),
),
home: RecipeListScreen(),
);
}
}

This code sets a green background color, white foreground color, and a background image for the app bar. Note that the image file must be added to the assets/images folder in your project.

Finally, let’s add some icons to the recipe list screen. Add the following code to your RecipeListScreen widget:

class RecipeListScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('My Recipes'),
actions: [
IconButton(
icon: Icon(Icons.search),
onPressed: () {
// TODO: Implement search
},
),
IconButton(
icon: Icon(Icons.add),
onPressed: () {
// TODO: Implement add recipe
},
),
],
),
body: ListView.builder(
itemCount: 10,
itemBuilder: (context, index) {
return ListTile(
title: Text('Recipe ${index + 1}'),
subtitle: Text('Description of Recipe ${index + 1}'),
trailing: Icon(Icons.favorite_border),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => RecipeDetailsScreen(recipeIndex: index)),
);
},
);
},
),
);
}
}

Step 7: Add navigation

Now that we have a functioning app with multiple pages, we need to add navigation to move between them. In Flutter, navigation is typically done using a widget called Navigator.

First, we need to create routes for each page that we want to navigate to. In our case, we want to navigate between the HomePage and SecondPage.

class Routes {
static const String home = '/';
static const String second = '/second';

static final Map<String, WidgetBuilder> routes = {
home: (context) => HomePage(),
second: (context) => SecondPage(),
};
}

Here, we’ve created a Routes class that defines two routes: home and second. The routes map contains a mapping of route names to WidgetBuilder functions that return the corresponding widget for that route.

Next, we need to modify our MaterialApp to use the routes map we just defined:

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My App',
initialRoute: Routes.home,
routes: Routes.routes,
);
}
}

Now we can use the Navigator widget to navigate between pages. In our HomePage, let's add a button that navigates to the SecondPage when pressed:

class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home'),
),
body: Center(
child: ElevatedButton(
child: Text('Go to Second Page'),
onPressed: () {
Navigator.pushNamed(context, Routes.second);
},
),
),
);
}
}

Here, we’ve added an ElevatedButton that calls Navigator.pushNamed when pressed, passing in the route name for the SecondPage.

Finally, let’s add a button to the SecondPage that navigates back to the HomePage:

class SecondPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Second Page'),
),
body: Center(
child: ElevatedButton(
child: Text('Go back'),
onPressed: () {
Navigator.pop(context);
},
),
),
);
}
}

Here, we’ve added a ElevatedButton that calls Navigator.pop when pressed, which pops the current route of the navigator and returns to the previous route.

With this, we’ve added basic navigation to our app!

Conclusion

In this tutorial, we’ve covered the basics of building a Flutter app from scratch. We’ve gone through the process of setting up a project, creating a layout, adding interactivity, adding navigation, and more.

While this tutorial only scratches the surface of what Flutter can do, it should give you a good starting point for building your own apps. With Flutter’s rich widget library and powerful tools, you can build beautiful, performant, and responsive apps that run on both iOS and Android. So go forth and start building!

--

--

MD. Sad Adnan
MD. Sad Adnan

Written by MD. Sad Adnan

Love Programming, Developing Solutions of Real Life Problems and Reading Books.

Responses (1)