创建项目

使用下面命令创建一个flutter项目

flutter create e_book_clone

组件抽取

抽取文件home.dart

  1. 创建 lib/pages/home 文件夹
  2. 抽取home.dart文件
home/main.dart
import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
const HomePage({super.key});

@override
State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: 0,
toolbarHeight: 0,
backgroundColor: Theme.of(context).colorScheme.surface,
),
body: const Center(child: Text('Home')),
);
}
}

抽取文件app.dart

app.dart
import 'package:e_book_clone/pges/home/home.dart';
import 'package:flutter/material.dart';

class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
useMaterial3: true,
),
home: const HomePage(),
);
}
}

抽取文件main.dart

main.dart
import 'package:e_book_clone/app.dart';
import 'package:flutter/material.dart';

void main() {
runApp(const MyAPP());
}