DEV Community

Ibrahim
Ibrahim

Posted on

Getting Started with Flutter: A Beginner's Guide

Hello!

I wanted my first post to be a beginner's guide kinda post. I'll try to be more active and delve into deeper topics later!

Flutter stands out in the realm of mobile development with its distinctive approach; it utilizes a unique set of widgets that empower developers to craft amazing applications, such as Google Ads, Google Pay, and Alibaba (according to Google).

For today's tutorial, I want you to install IntelliJ IDEA, as it's the platform I'm using. You can download it from the official website and follow the installation instructions for your operating system, as well as starting your first Flutter project.
Image description

In this tutorial, we'll create a simple Flutter app that demonstrates basic interactivity. Our app will display a message, and when a button is pressed, the message will change. Let's dive in!

import 'package:flutter/material.dart';

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

class MyApp extends StatefulWidget { // MyApp is a stateful widget
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> { // _MyAppState is the state class
  String message = 'Hello, World!'; // Initialize message variable

  void changeMessage() { // Function to change the message
    setState(() {
      message = 'Flutter is amazing!'; // Update message when button is pressed
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('My First Flutter App'), // Set app title
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                message, // Display the message
                style: TextStyle(fontSize: 24),
              ),
              SizedBox(height: 20),
              ElevatedButton(
                onPressed: changeMessage, // Call changeMessage function when button is pressed
                child: Text('Change Message'), // Button text
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. We start by importing the necessary packages and defining the main function to run our app.
  2. The MyApp class is a stateful widget that manages the app's state.
  3. Inside the MyApp class, the _MyAppState class holds the state data, including the message to be displayed.
  4. The changeMessage function updates the message when the button is pressed.
  5. In the build method, we define the app's UI structure using MaterialApp, Scaffold, AppBar, Text, and ElevatedButton widgets.
  6. The Text widget displays the current message, and the ElevatedButton triggers the changeMessage function when pressed.

With just a few lines of code, we've created a fully functional Flutter app with basic interactivity. Feel free to experiment and customize further as you continue your Flutter journey!

apologies for any mistakes :>

Top comments (0)