Skip to content

Quick Start on main.dart

Follow these steps to set up a basic Flutter app in main.dart:

  1. Open main.dart

  2. Delete all existing code

  3. Import material.dart: dart import 'package:flutter/material.dart';

  4. Define the main() method: Every Dart program begins with the main() method. Here's a basic setup:

    void main() {
            runApp(MyApp()); // You can change the class name from MyApp to something else
    }
    

  5. Start with a MaterialApp: Always initiate your Flutter app with a MaterialApp. Create a class, such as MyApp (feel free to rename it):
    class MyApp extends StatelessWidget {
        @override
        Widget build(BuildContext context) {
            return MaterialApp(
                home: MainScreen(), // This will be the main screen
                debugShowCheckedModeBanner: false, // Disable the debug banner
                routes: {},
            ); // MaterialApp
        }
    }
    
  6. Create the main screen: You need to define a main screen class inside main.dart. Here is how to create one:
    class MainScreen extends StatelessWidget {
        static String routeName = '/';
    
        @override
        Widget build(BuildContext context) {
            return Scaffold(
                appBar: AppBar(
                    title: const Text('Simple App'),
                ),
                body: const Center(
                    child: Text('This is a sample Text widget'),
                ),
            );
        }
    }
    
  7. Save your changes
  8. Run the app: Execute your app in debug mode on Chrome.