Creating Our First Flutter App: Displaying "Hello World" Text

Creating Our First Flutter App: Displaying "Hello World" Text

ยท

4 min read

Hey there, Today, I want to walk you through the process of creating your very first app using Flutter. Flutter is a fantastic framework for building cross-platform mobile applications, and it's gaining popularity among developers of all ages. So, let's dive right in and get started on this exciting journey together!

Setting Up Your Development Environment

Before we jump into coding, let's make sure our development environment is set up properly. Here's what you'll need:

  1. Flutter SDK: Start by downloading the Flutter SDK from the official website (flutter.dev). Follow the installation instructions specific to your operating system.

  2. Code Editor: Choose a code editor that suits your preferences. Some popular options include Visual Studio Code, Android Studio, and IntelliJ IDEA. Personally, I prefer Visual Studio Code due to its simplicity and rich ecosystem.

  3. Android/iOS Emulator: To run and test our Flutter app, we'll need an emulator. If you're developing on macOS, you can use the default iOS Simulator that comes with Xcode. For Android, you can use the Android Emulator or connect a physical device.

Once you have everything set up, open your code editor and let's get started with our first Flutter app!

Creating a New Flutter Project

To create a new Flutter project, open your terminal or command prompt and navigate to the directory where you want to create your app. Then, run the following command:

flutter create hello_world_app

This command will generate a new Flutter project called "hello_world_app." Feel free to replace this with any name you prefer. Once the project is created, navigate into the project directory:

cd hello_world_app

Writing the Code

Now that we have our Flutter project set up, let's write some code to display the classic "Hello World" text on the screen.

  1. Open the lib/main.dart file in your code editor. This is the entry point for our app.

  2. Replace the existing code with the following:

     import 'package:flutter/material.dart';
    
     void main() {
       runApp(HelloWorldApp());
     }
    
     class HelloWorldApp extends StatelessWidget {
       @override
       Widget build(BuildContext context) {
         return MaterialApp(
           title: 'Hello World App',
           home: Scaffold(
             appBar: AppBar(
               title: Text('Hello World'),
             ),
             body: Center(
               child: Text(
                 'Hello World!',
                 style: TextStyle(fontSize: 24.0),
               ),
             ),
           ),
         );
       }
     }
    

    Here's a breakdown of what the code does:

    • We import the flutter/material.dart package, which provides the basic building blocks for our app's user interface.

    • The main() function is the entry point for our app. In this case, it calls the runApp() function to run our HelloWorldApp widget.

    • We define a new class called HelloWorldApp, which extends StatelessWidget. This means our app's UI is static and won't change over time.

    • Inside the build() method of HelloWorldApp, we return a MaterialApp widget. This widget configures various aspects of our app, such as its title and theme.

    • The home property of MaterialApp sets the home screen of our app. In this case, we use a Scaffold widget as the home screen.

    • The Scaffold widget provides a basic app layout with an app bar and a body. We set the app bar's title to "Hello World" using the AppBar widget.

    • Inside the body, we use the Center widget to center our Text widget vertically and horizontally.

    • The Text widget displays the "Hello World!" text and applies a font size of 24 pixels using the TextStyle class.

Running the App

Now that our code is ready, let's run our Flutter app and see the "Hello World" text in action!

  1. Ensure your emulator or physical device is connected and running.

  2. In your terminal or command prompt, navigate to your project directory (hello_world_app).

  3. Run the following command to launch the app:

     flutter run
    
  4. Wait for the app to compile and install on your emulator/device. Once it's ready, you should see the "Hello World" text displayed on the screen.

    Congratulations! You've just created and run your first Flutter app. How cool is that? ๐ŸŽ‰

    Conclusion

    In this blog, we took our first steps into the world of Flutter by creating a simple "Hello World" app. We learned how to set up our development environment, create a new Flutter project, write code to display text on the screen and run the app on an emulator or device.

    Flutter is an incredibly powerful framework, and this is just the beginning of what you can achieve. I encourage you to explore the Flutter documentation and experiment with different widgets and layouts to create even more exciting apps.

    Remember, the journey to becoming a proficient Flutter developer takes time and practice. So, keep coding, and exploring, and don't hesitate to seek help from the Flutter community whenever you get stuck.

Did you find this article valuable?

Support Flutter 0 to 1 by becoming a sponsor. Any amount is appreciated!

ย