
上QQ阅读APP看本书,新人免费读10天
设备和账号都新为新人
4.1 项目结构规范
前面我们讲过Flutter 的项目结构,默认新建项目后,项目结构完全遵循官方标准项目结构要求。开发时也要按照这个标准要求设置目录结构,不能更改。
如果我们需要新建项目内的资源文件目录,并引入一些打包进去的图片文件、音频文件、视频文件、字体文件等,可以参照官方例子,在项目根目录下新建assets 目录,并把项目资源文件存放在其中。如果有需要,还可以在assets 目录里再次分类,建立audios、fonts、images、videos 等资源目录。assets 目录结构如图4-1 所示。

图4-1 assets 目录结构
另外,我们还可以直接在项目根目录下创建多个类别资源文件夹,如创建fonts 文件夹,用于存放字体文件资源,与用于存放图片、图标资源的assets 文件夹并列,大致情况如图4-2 所示。

图4-2 在项目根目录下创建多个类别资源文件夹
以上两种资源目录创建方式都可以使用,需要根据实际需求进行选择。这里要注意的是,对于已定义的资源目录,我们需要先在pubspec.yaml 文件中进行路径配置,然后才可以在Flutter代码里使用,如图4-3 所示。

图4-3 路径配置
前面讲过,main.dart 文件位于lib 目录下,是整个应用的入口文件,名称不可以修改,位置也不可以修改。在lib 目录下创建其他类时,可以按照功能来创建,如创建api、conf、ui、utils、widgets 等类,lib 目录结构如图4-4 所示。

图4-4 lib 目录结构
如果想引入第三方库,可以在Dart Pub 仓库中搜索,然后在pubspec.yaml 文件中为其配置路径,如图4-5 所示。

图4-5 为第三方库配置路径