马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
敲一遍,你会懂的
包:
- dependencies:
- flutter:
- sdk: flutter
- cupertino_icons: ^1.0.8
- hive: ^2.2.3
- hive_flutter: ^1.1.0
- dev_dependencies:
- flutter_test:
- sdk: flutter
- hive_generator: ^2.0.1
- build_runner: ^2.4.7
复制代码 类:(config.dart)
- import 'package:hive/hive.dart';
- part 'config.g.dart'; // 生成的文件
- @HiveType(typeId: 0)
- class Config extends HiveObject {
- @HiveField(0)
- String language;
- @HiveField(1)
- String theme;
- @HiveField(2)
- List<LanguageConfig> languages;
- Config({required this.language, required this.theme, required this.languages});
- }
- @HiveType(typeId: 1)
- class LanguageConfig extends HiveObject {
- @HiveField(0)
- String language;
- @HiveField(1)
- String filePath;
- @HiveField(2)
- bool willTranslate;
- LanguageConfig({required this.language, required this.filePath, required this.willTranslate});
- }
复制代码 生成代码:
- flutter packages pub run build_runner build
复制代码 服务类:(hive_service.dart)
- import 'package:hive/hive.dart';
- import 'config.dart';
- class HiveService {
- static const String configBoxName = 'configBox';
- // 打开 Box
- static Future<Box<Config>> openBox() async {
- return await Hive.openBox<Config>(configBoxName);
- }
- // 保存配置
- static Future<void> saveConfig(Config config) async {
- final box = await openBox();
- await box.put('config', config);
- }
- // 获取配置
- static Future<Config?> getConfig() async {
- final box = await openBox();
- return box.get('config');
- }
- }
复制代码 ui app.dart)
- import 'package:flutter/material.dart';
- import 'hive_service.dart';
- import 'config.dart';
- class MyApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- home: ConfigScreen(),
- );
- }
- }
- class ConfigScreen extends StatefulWidget {
- @override
- _ConfigScreenState createState() => _ConfigScreenState();
- }
- class _ConfigScreenState extends State<ConfigScreen> {
- Config? config;
- @override
- void initState() {
- super.initState();
- _loadConfig();
- }
- Future<void> _loadConfig() async {
- Config? savedConfig = await HiveService.getConfig();
- setState(() {
- config = savedConfig ??
- Config(language: "en", theme: "light", languages: [
- LanguageConfig(language: "English", filePath: "/path/en", willTranslate: true),
- LanguageConfig(language: "Chinese", filePath: "/path/zh", willTranslate: false),
- ]);
- });
- }
- Future<void> _saveConfig() async {
- if (config != null) {
- await HiveService.saveConfig(config!);
- ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("配置已保存")));
- }
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(title: Text("Hive 配置存储 Demo")),
- body: config == null
- ? Center(child: CircularProgressIndicator())
- : Column(
- children: [
- ListTile(
- title: Text("当前语言: ${config!.language}"),
- trailing: DropdownButton<String>(
- value: config!.language,
- items: ["en", "zh"].map((lang) {
- return DropdownMenuItem(value: lang, child: Text(lang));
- }).toList(),
- onChanged: (val) {
- setState(() {
- config!.language = val!;
- });
- },
- ),
- ),
- ListTile(
- title: Text("当前主题: ${config!.theme}"),
- trailing: DropdownButton<String>(
- value: config!.theme,
- items: ["light", "dark"].map((theme) {
- return DropdownMenuItem(value: theme, child: Text(theme));
- }).toList(),
- onChanged: (val) {
- setState(() {
- config!.theme = val!;
- });
- },
- ),
- ),
- ElevatedButton(
- onPressed: _saveConfig,
- child: Text("保存配置"),
- ),
- ],
- ),
- );
- }
- }
复制代码 入口:(main.dart)
- import 'package:flutter/material.dart';
- import 'package:hive_flutter/hive_flutter.dart';
- import 'app.dart';
- import 'config.dart';
- void main() async {
- WidgetsFlutterBinding.ensureInitialized();
- // 初始化 Hive
- await Hive.initFlutter();
- // 注册适配器
- Hive.registerAdapter(ConfigAdapter());
- Hive.registerAdapter(LanguageConfigAdapter());
- runApp(MyApp());
- }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |