feat:初始化工程

This commit is contained in:
2025-11-14 14:56:52 +08:00
commit 3055f99aab
130 changed files with 5200 additions and 0 deletions

130
lib/main.dart Normal file
View File

@@ -0,0 +1,130 @@
import 'package:blog_app/test.dart';
import 'package:flutter/material.dart';
import 'package:markdown_widget/markdown_widget.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Markdown导航'),
),
body: const MarkdownWithToc(),
),
);
}
}
class MarkdownWithToc extends StatefulWidget {
const MarkdownWithToc({super.key});
@override
State<MarkdownWithToc> createState() => _MarkdownWithTocState();
}
class _MarkdownWithTocState extends State<MarkdownWithToc> {
final tocController = TocController();
bool _showToc = false;
// ... markdownData 同上
Widget _buildTocPanel() => AnimatedOpacity(
opacity: _showToc ? 1.0 : 0.0,
duration: const Duration(milliseconds: 300),
child: Visibility(
visible: _showToc,
child: Align(
alignment: Alignment.bottomRight,
child: Container(
width: 250,
height: 400,
margin: const EdgeInsets.only(bottom: 60, right: 60), // 调整位置在按钮左侧
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(12),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.2),
blurRadius: 8,
offset: const Offset(0, 4),
),
],
border: Border.all(color: Colors.grey[300]!),
),
child: Column(
children: [
// 标题栏
Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: Colors.grey[100],
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(12),
topRight: Radius.circular(12),
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text(
'目录',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16,
),
),
IconButton(
icon: const Icon(Icons.close, size: 20),
onPressed: () => setState(() => _showToc = false),
),
],
),
),
Expanded(
child: TocWidget(
controller: tocController,
),
),
],
),
),
),
),
);
Widget buildMarkdown() => MarkdownWidget(
data: markdownData,
tocController: tocController,
);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('文档'),
),
body: Stack(
children: [
// 主内容
buildMarkdown(),
// 悬浮TOC面板
_buildTocPanel(),
],
),
floatingActionButton: FloatingActionButton(
onPressed: () => setState(() => _showToc = !_showToc),
child: Icon(_showToc ? Icons.close : Icons.list),
mini: true,
backgroundColor: _showToc ? Colors.grey : Theme.of(context).primaryColor,
),
);
}
}

109
lib/test.dart Normal file
View File

@@ -0,0 +1,109 @@
final String markdownData = '''
# 一、简介
Spring Boot Starter是一组预定义的依赖项集合旨在简化Maven或Gradle等构建工具中的依赖管理。每个Starter都包含了实现特定功能所需的库和组件以及相应的配置文件。开发者只需在项目中引入相应的Starter依赖即可快速搭建起具备该功能的项目骨架。
Starter=依赖+自动配置+配置文件
# 二、实现原理
## 2.1 传统实现
例如引入Spring中的jpa则需要以下步骤
1. 在Maven中引入数据库依赖
2. 在Maven中引入jpa依赖
3. 在配置文件中配置属性
4. 调试程序
每次新建项目都需要重复此流程,操作繁琐。
## 2.2 自定义Starter实现
1. 新建Maven项目在pom.xml文件中定义需要的依赖项。
2. 创建自动配置类 AutoConfigurationTest添加@configuration注解使其能够被SpringBoot自动扫描到。
3. 添加自动装配机制在src/main/resources/META-INF文件夹下创建spring.factories文件添加以下配置
```bash
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.springbootstartercustom.AutoConfigurationTest
```
这里文件夹和文件名一定要正确因此SpringFactoriesLoader中就是这么定义的。
4. 在配置文件中自定义属性(可选)。
5. 安装打包到maven仓库中。
6. 其他项目通过pom.xml文件引入该starter。
## 2.3 Starter实现原理
加载依赖->扫描自动配置类->加载配置文件
# 三、高级特性
## 3.1 可插拔Starter
所谓可插拔就是可以自行决定是否需要加载该starter的功能。例如可以通过注解的方式决定是否加载。
1. 定义注解
```java
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface EnableAutoConfigTest {
}
```
2. 在自动配置类 AutoConfiguration中增加条件注解
```java
@Configuration
@ConditionalOnBean(annotation = EnableAutoConfigTest.class)
public class AutoConfigurationTest {
}
```
3. 在相应位置添加@EnableAutoConfigTest注解该stater才会生效。
## 3.2 自定义配置文件
所谓自定义配置文件就是可以在引入stater后可以通过修改配置文件覆盖原来的配置属性从而灵活配置stater功能。
1. 引用spring-boot-configuration-processor
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
```
2. 定义Properties配置类
```java
@ConfigurationProperties(prefix = "test")
public class TestProperties {
private String name = "test";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
```
3. 在自动配置类 AutoConfigurationTest中引用
```java
@Configuration(proxyBeanMethods = false)
@Import({TestProperties.class})
public class AutoConfigurationTest {
@Resource
private TestProperties testProperties;
}
```
4. 在引入的stater工程中修改配置文件
```yaml
test:
name: test1
```
''';