- 从零开始学Flutter开发
- 谭东
- 434字
- 2025-02-23 20:47:44
4.2 命名规范
大部分编程语言都有自己的命名特点,不过整体来说大同小异。本节我们介绍Flutter 的命名规范。
Flutter 的命名规范其实就是Dart 语言的命名规范,具体来说有以下三种方式。
UpperCamelCase:单词首字母大写的驼峰式命名方式,如StudentName。
lowerCamelCase:第一个单词首字母小写的驼峰式命名方式,如studentName。
lowercase_with_underscores:单词全部小写,中间用下画线“_”连接的命名方式,如student_name。
这三种命名方式一般都在哪种情况下使用呢?下面我们分别介绍。一般情况下,类、注解、枚举类型、typedef、参数类型使用UpperCamelCase 方式命名,示例如下。

lowerCamelCase 命名方式一般用在变量、方法和参数、常量等的命名上,示例如下。

最后我们来看一下lowercase_with_underscores 命名方式。lowercase_with_underscores 命名方式一般用在库(libraries)、包(packages)、源文件(source files)的命名上,具体示例如下。

同时,在Flutter 导入类库的时候,as 关键字后面的命名也要采用lowercase_with_underscores方式,示例如下。

Flutter 命名时还有一点需要注意:不要使用前缀字母。

为了保持代码的整洁及层次,我们可以在某些地方使用空行。
如果某个方法、常量、变量、类不想被其他外部类访问或调用的话,可以在相应的名称前加上下画线前缀,示例如下。
