Flutter主题命名插件thematic_names的使用
Flutter主题命名插件thematic_names的使用
插件介绍
thematic_names
是一个用于生成主题独特名称的Dart包。该插件可以生成多个主题的独特名称,例如火山名、行星名等。
功能特性
- 生成无限数量的主题独特名称:每次达到1000个名称时,会在名称后添加一个数字以保持唯一性。
- 易于使用且经过良好测试:确保了代码的质量和可靠性。
- 支持多种语言:可以根据需求选择不同的语言来生成名称。
使用方法
首先,确保在你的pubspec.yaml
文件中添加了对thematic_names
包的依赖:
dependencies:
thematic_names: ^latest_version
然后执行flutter pub get
来安装包。
获取一组基础的1000个独特火山名称
final names = Names.volcanos();
输出所有可用的主题名称
print(names);
生成4个名称
for (var i = 0; i < 4; ++i) {
print(names.next.title);
}
原则
这些规则适用于每个主题名称集合:
- 每个集合包含恰好1000个独特的主题名称。
- 所有名称按字母顺序排列,忽略大小写。
- 名称中不包含数字。
- 名称中不包含尾随空格。
- 名称中不包含连续的空格。
- 名称中的任何新词都以大写字母开头,除了语言相关的例外情况。排除词包括:[‘a’, ‘and’, ‘by’, ‘in’, ‘of’, ‘on’, ‘or’]。
- 名称只包含在该语言中被接受的字母表。
- 名称长度从2个字符到不超过120个字符。
- 名称中的任何单词至少包含两个字符,语言相关的例外情况。排除词见原则6。
- 名称的第一个词中没有这样的词:[‘above’, ‘ahead’, ‘behind’, ‘below’, ‘bottom’, ‘down’, ‘east’, ‘far’, ‘great’, ‘greater’, ‘left’, ‘lower’, ‘near’, ‘nearest’, ‘new’, ‘newest’, ‘north’, ‘next’, ‘old’, ‘oldest’, ‘previos’, ‘previuos’, ‘right’, ‘south’, ‘top’, ‘up’, ‘upper’, ‘west’]。
新功能
查看变更日志了解最新更新。
欢迎使用
如果您遇到任何问题,可以在这里打开一个issue。如果您认为插件缺少某个功能,请在Github上提出一个ticket,我会尽快处理。建议和请求非常受欢迎。
贡献指南
- 文档化未记录的内容:如果你发现我们的代码库中有未记录的类、属性或方法,请花几分钟时间记录一些有用的笔记,帮助其他开发者。
- 优化代码:虽然主要由我负责重构代码,但任何你愿意做出的贡献都将被欢迎!
- 建设性的代码审查:如果你发现更有效的方法来实现某些功能,请告诉我。你的改进建议非常重要。
- 分享你的例子:如果你尝试过我们的用例或者自己编写了一些示例,请将它们添加到
example
目录中。你的实际经验可以帮助丰富我们的资源库。 - 修复拼写错误/语法错误。
- 报告难以实现的bug和场景。
- 通过pull request实现新功能。
可能的TODO列表
一旦开始使用thematic_names
,很容易选择要贡献的功能。但是,如果你已经从这个包中得到了你需要的一切,但还有一些空闲时间,让我在这里写下我计划的一些事情:
- 为摘要编写原则和测试。
- 提供带有外部自定义名称集的示例。
- 其他语言的名称和摘要。
希望这些笔记能帮助你选择合适的“功能”,并成为开源社区的贡献者。
准备好了吗?
准备好为开源项目贡献力量了吗?点击下方链接了解更多:
- 制作第一个贡献的指南
- 如何参与开源
- Google的夏季指导
- CodeTriangle - 为开源项目贡献的免费社区工具
创建示例
下面是使用thematic_names
生成星系名称的完整示例代码:
// 忽略打印警告
import 'dart:io';
import 'package:thematic_names/thematic_names.dart';
void main() {
// 获取一组1000个独特的星系名称
final names = Names.stars(languageCode: SupportedLanguage.english);
// 输出所有1000个名称
final titles = names.all.map((r) => r.title);
File('all_stars_names.txt').writeAsStringSync(titles.join(', '));
// 生成4个名称
for (var i = 0; i < 4; ++i) {
print(names.next.title);
}
}
更多关于Flutter主题命名插件thematic_names的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter主题命名插件thematic_names的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter项目中使用thematic_names
插件的示例代码。thematic_names
插件用于帮助开发者为Flutter应用的主题颜色命名,使得代码更具可读性和维护性。
首先,确保你已经在pubspec.yaml
文件中添加了thematic_names
依赖:
dependencies:
flutter:
sdk: flutter
thematic_names: ^latest_version # 请替换为最新的版本号
然后运行flutter pub get
来获取依赖。
接下来,你可以在你的Flutter应用中配置和使用thematic_names
。以下是一个简单的示例:
- 创建一个主题文件:
// themes.dart
import 'package:flutter/material.dart';
import 'package:thematic_names/thematic_names.dart';
final ThemeData lightTheme = ThemeData(
// 使用thematic_names提供的命名约定
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.blue,
brightness: Brightness.light,
).copyWith(
primary: thematicColorOf(ThematicPalette.primary),
secondary: thematicColorOf(ThematicPalette.secondary),
background: thematicColorOf(ThematicPalette.background),
surface: thematicColorOf(ThematicPalette.surface),
onBackground: thematicColorOf(ThematicPalette.onBackground),
onSurface: thematicColorOf(ThematicPalette.onSurface),
error: thematicColorOf(ThematicPalette.error),
),
textTheme: TextTheme(
headline1: TextStyle(
color: thematicColorOf(ThematicPalette.onBackground),
fontSize: 24,
fontWeight: FontWeight.bold,
),
// 其他文本样式...
),
);
final ThemeData darkTheme = ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.blue.shade900,
brightness: Brightness.dark,
).copyWith(
primary: thematicColorOf(ThematicPalette.primary),
secondary: thematicColorOf(ThematicPalette.secondary),
background: thematicColorOf(ThematicPalette.background),
surface: thematicColorOf(ThematicPalette.surface),
onBackground: thematicColorOf(ThematicPalette.onBackground),
onSurface: thematicColorOf(ThematicPalette.onSurface),
error: thematicColorOf(ThematicPalette.error),
),
textTheme: TextTheme(
headline1: TextStyle(
color: thematicColorOf(ThematicPalette.onSurface),
fontSize: 24,
fontWeight: FontWeight.bold,
),
// 其他文本样式...
),
);
- 在MaterialApp中使用主题:
// main.dart
import 'package:flutter/material.dart';
import 'themes.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: lightTheme,
darkTheme: darkTheme,
themeMode: ThemeMode.system, // 或者 ThemeMode.light / ThemeMode.dark
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Thematic Names Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Hello, Flutter!',
style: Theme.of(context).textTheme.headline1,
),
],
),
),
);
}
}
在这个示例中,我们使用了thematic_names
提供的thematicColorOf
函数和ThematicPalette
枚举来命名主题颜色。这使得主题颜色的命名更加一致和可读。
请确保你查阅thematic_names
的最新文档,因为插件的API可能会随着版本的更新而变化。