Flutter 入门

默认使用Material

1
2
// 导入必要的模块
import 'package:flutter/material.dart';

启动当前项目

1
2
3
4
5
// 入口函数为main
void main()
{
runApp(const MyApp());
}

创建APP入口–MyApp()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 无状态入口
class MyApp extends StatelessWidget {
const MyApp({super.key});

@override
Widget build(BuildContext context){
return MaterialApp(
title: 'Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Demo'),
);
}
}

创建MyHomePage

1
2
3
4
5
6
7
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;

@override
State<MyHomePage> createState() => _MyHomePageState();
}
1
2
3
4
5
6
7
8
class MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar...
);
}
}