木鱼配置记录-数据库
This commit is contained in:
@@ -5,8 +5,11 @@ import 'guess/guess_page.dart';
|
||||
import 'muyu/muyu_page.dart';
|
||||
import 'navigation/app_navigation.dart';
|
||||
import 'paper/paper.dart';
|
||||
import 'storage/db_storage/db_storage.dart';
|
||||
|
||||
void main() {
|
||||
void main() async{
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await DbStorage.instance.open();
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
|
||||
@@ -6,4 +6,12 @@ class MeritRecord {
|
||||
final String audio;
|
||||
|
||||
MeritRecord(this.id, this.timestamp, this.value, this.image, this.audio);
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
"id":id,
|
||||
"timestamp": timestamp,
|
||||
"value": value,
|
||||
"image": image,
|
||||
"audio": audio,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter_first_station/muyu/animate_text.dart';
|
||||
import 'package:flutter_first_station/muyu/options/select_audio.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
import '../storage/db_storage/db_storage.dart';
|
||||
import '../storage/sp_storage.dart';
|
||||
import 'models/audio_option.dart';
|
||||
import 'models/image_option.dart';
|
||||
@@ -62,6 +63,7 @@ class _MuyuPageState extends State<MuyuPage>
|
||||
_counter = config['counter']??0;
|
||||
_activeImageIndex = config['activeImageIndex']??0;
|
||||
_activeAudioIndex = config['activeAudioIndex']??0;
|
||||
_records = await DbStorage.instance.meritRecordDao.query();
|
||||
setState(() {
|
||||
|
||||
});
|
||||
@@ -155,6 +157,7 @@ class _MuyuPageState extends State<MuyuPage>
|
||||
);
|
||||
_counter += _cruRecord!.value;
|
||||
saveConfig();
|
||||
DbStorage.instance.meritRecordDao.insert(_cruRecord!);
|
||||
// 添加功德记录
|
||||
_records.add(_cruRecord!);
|
||||
});
|
||||
|
||||
47
lib/storage/db_storage/dao/merit_record_dao.dart
Normal file
47
lib/storage/db_storage/dao/merit_record_dao.dart
Normal file
@@ -0,0 +1,47 @@
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
import '../../../muyu/models/merit_record.dart';
|
||||
|
||||
class MeritRecordDao {
|
||||
final Database database;
|
||||
|
||||
MeritRecordDao(this.database);
|
||||
|
||||
static String tableName = 'merit_record';
|
||||
|
||||
static String tableSql = """
|
||||
CREATE TABLE $tableName (
|
||||
id VARCHAR(64) PRIMARY KEY,
|
||||
value INTEGER,
|
||||
image TEXT,
|
||||
audio TEXT,
|
||||
timestamp INTEGER
|
||||
)""";
|
||||
|
||||
static Future<void> createTable(Database db) async{
|
||||
return db.execute(tableSql);
|
||||
}
|
||||
|
||||
Future<int> insert(MeritRecord record) {
|
||||
return database.insert(
|
||||
tableName,
|
||||
record.toJson(),
|
||||
conflictAlgorithm: ConflictAlgorithm.replace,
|
||||
);
|
||||
}
|
||||
|
||||
Future<List<MeritRecord>> query() async {
|
||||
List<Map<String, Object?>> data = await database.query(
|
||||
tableName,
|
||||
);
|
||||
|
||||
return data
|
||||
.map((e) => MeritRecord(
|
||||
e['id'].toString(),
|
||||
e['timestamp'] as int,
|
||||
e['value'] as int,
|
||||
e['image'].toString(),
|
||||
e['audio'].toString(),
|
||||
))
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
35
lib/storage/db_storage/db_storage.dart
Normal file
35
lib/storage/db_storage/db_storage.dart
Normal file
@@ -0,0 +1,35 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
|
||||
import 'dao/merit_record_dao.dart';
|
||||
|
||||
|
||||
class DbStorage {
|
||||
DbStorage._();
|
||||
|
||||
static DbStorage? _storage;
|
||||
|
||||
static DbStorage get instance {
|
||||
_storage = _storage ?? DbStorage._();
|
||||
return _storage!;
|
||||
}
|
||||
|
||||
late Database _db;
|
||||
|
||||
late MeritRecordDao _meritRecordDao;
|
||||
MeritRecordDao get meritRecordDao => _meritRecordDao;
|
||||
|
||||
Future<void> open() async {
|
||||
String databasesPath = await getDatabasesPath();
|
||||
String dbPath = path.join(databasesPath, 'first_station.db');
|
||||
_db = await openDatabase(dbPath, version: 1, onCreate: _onCreate);
|
||||
_meritRecordDao = MeritRecordDao(_db);
|
||||
}
|
||||
|
||||
void _onCreate(Database db, int version) async {
|
||||
await MeritRecordDao.createTable(db);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,9 +8,11 @@ import Foundation
|
||||
import audioplayers_darwin
|
||||
import path_provider_foundation
|
||||
import shared_preferences_foundation
|
||||
import sqflite
|
||||
|
||||
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
|
||||
AudioplayersDarwinPlugin.register(with: registry.registrar(forPlugin: "AudioplayersDarwinPlugin"))
|
||||
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
|
||||
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
|
||||
SqflitePlugin.register(with: registry.registrar(forPlugin: "SqflitePlugin"))
|
||||
}
|
||||
|
||||
16
pubspec.lock
16
pubspec.lock
@@ -397,6 +397,22 @@ packages:
|
||||
url: "https://pub.flutter-io.cn"
|
||||
source: hosted
|
||||
version: "1.9.1"
|
||||
sqflite:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: sqflite
|
||||
sha256: "3a82c9a216b46b88617e3714dd74227eaca20c501c4abcc213e56db26b9caa00"
|
||||
url: "https://pub.flutter-io.cn"
|
||||
source: hosted
|
||||
version: "2.2.8+2"
|
||||
sqflite_common:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: sqflite_common
|
||||
sha256: e77abf6ff961d69dfef41daccbb66b51e9983cdd5cb35bf30733598057401555
|
||||
url: "https://pub.flutter-io.cn"
|
||||
source: hosted
|
||||
version: "2.4.5"
|
||||
stack_trace:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
||||
@@ -34,6 +34,7 @@ dependencies:
|
||||
uuid: ^3.0.7
|
||||
intl: ^0.18.1
|
||||
shared_preferences: ^2.1.1
|
||||
sqflite: ^2.2.8+2
|
||||
# The following adds the Cupertino Icons font to your application.
|
||||
# Use with the CupertinoIcons class for iOS style icons.
|
||||
cupertino_icons: ^1.0.2
|
||||
|
||||
Reference in New Issue
Block a user