mainexplain
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key, this.title}) : super(key: key);
final String? title;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(title!), ),
body: const Center(child: Text( 'Hello World',)),
);
}
}
Import Flutter packages:
This line imports the necessary Flutter packages, including
material.dart, which provides widgets for building material design applications.Define the main function and runApp:
The
mainfunction is the entry point of the application. It callsrunAppto start the Flutter application and render theMyAppwidget.MyAppClass:This class represents the top-level widget of your application. It is a stateless widget, so it doesn't have mutable state. It defines the overall structure of the app.
title: The title of the app is set to "Flutter Demo."theme: The theme is set to use the primary color swatch of blue.home: TheMyHomePagewidget is set as the home screen of the app with the title "Flutter Demo Home Page."
MyHomePageClass:This class represents the main content of the app, which is a simple page with an app bar and a "Hello World" message in the center. It also inherits from
StatelessWidget.title: The title for the app bar is passed as a parameter and displayed in the app bar.The
buildmethod creates aScaffoldwidget, which provides the basic material design structure for the screen.The
appBarproperty sets anAppBarwith the title set to the providedtitle.The
bodyproperty sets the main content to be a centeredTextwidget with the text "Hello World."UML
Widget Tree
Last updated