sqllite database save magnetic declination data
This commit is contained in:
BIN
assets/magnetic_declination.db
Normal file
BIN
assets/magnetic_declination.db
Normal file
Binary file not shown.
@ -71,100 +71,14 @@ class RegionSelector extends StatelessWidget {
|
||||
],
|
||||
)),
|
||||
),
|
||||
const SizedBox(
|
||||
height: 20,
|
||||
),
|
||||
Text(
|
||||
'磁偏角 : ${regionProvider.tempDeclination}°',
|
||||
style: const TextStyle(fontSize: 24),
|
||||
)
|
||||
],
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
// class RegionSelector extends StatefulWidget {
|
||||
// const RegionSelector({Key key}) : super(key: key);
|
||||
//
|
||||
// @override
|
||||
// State<RegionSelector> createState() => _RegionSelectorState();
|
||||
// }
|
||||
//
|
||||
// class _RegionSelectorState extends State<RegionSelector> {
|
||||
// final Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
|
||||
//
|
||||
// // final rp = RegionProvider()
|
||||
// String provinceName;
|
||||
// String cityName;
|
||||
//
|
||||
// @override
|
||||
// initState() {
|
||||
// // provinceName = Provider.of<RegionProvider>(context).provinceName;
|
||||
// // cityName = Provider.of<RegionProvider>(context).cityName;
|
||||
// _prefs.then((prefs) {
|
||||
// if (prefs.getString('REGION') != null) {
|
||||
// final region =
|
||||
// RegionData.fromJson(jsonDecode(prefs.getString('REGION')));
|
||||
// provinceName = region.provinceName;
|
||||
// cityName = region.cityName;
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
//
|
||||
// // 拼接城市
|
||||
// String spliceCityName(String pname, String cname) {
|
||||
// if (pname == '') return '未选择城市';
|
||||
// StringBuffer sb = StringBuffer();
|
||||
// sb.write(pname);
|
||||
// if (cname == '') return sb.toString();
|
||||
// sb.write(' - ');
|
||||
// sb.write(cname);
|
||||
// return sb.toString();
|
||||
// }
|
||||
//
|
||||
// @override
|
||||
// Widget build(BuildContext context) {
|
||||
// return SizedBox(
|
||||
// height: 100,
|
||||
// child: Consumer<RegionProvider>(
|
||||
// builder: (context, regionProvider, child) => Column(
|
||||
// children: [
|
||||
// InkWell(
|
||||
// onTap: () {
|
||||
// Pickers.showAddressPicker(context,
|
||||
// addAllItem: false,
|
||||
// initProvince: regionProvider.provinceName,
|
||||
// initCity: regionProvider.cityName,
|
||||
// onConfirm: (p, c, t) {
|
||||
// regionProvider.updateRegion(p, c);
|
||||
// setState(() {
|
||||
// provinceName = p;
|
||||
// cityName = c;
|
||||
// });
|
||||
// });
|
||||
// },
|
||||
// child: Container(
|
||||
// constraints: const BoxConstraints(minHeight: 42),
|
||||
// padding: const EdgeInsets.fromLTRB(10, 0, 12, 0),
|
||||
// decoration: BoxDecoration(
|
||||
// border: Border.all(color: Colors.grey),
|
||||
// borderRadius: BorderRadius.circular(6),
|
||||
// ),
|
||||
// child: Row(
|
||||
// // mainAxisSize: MainAxisSize.min,
|
||||
// mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
// crossAxisAlignment: CrossAxisAlignment.center,
|
||||
// children: [
|
||||
// Text(spliceCityName(regionProvider.provinceName,
|
||||
// regionProvider.cityName)),
|
||||
// // SizedBox(width: 8),
|
||||
// Row(
|
||||
// children: [
|
||||
// InkWell(
|
||||
// child: Icon(Icons.close,
|
||||
// size: 20, color: Colors.grey[500]),
|
||||
// onTap: () {}),
|
||||
// Icon(Icons.keyboard_arrow_down,
|
||||
// size: 28, color: Colors.grey[500]),
|
||||
// ],
|
||||
// )
|
||||
// ],
|
||||
// )),
|
||||
// ),
|
||||
// ],
|
||||
// )));
|
||||
// }
|
||||
// }
|
||||
|
@ -10,17 +10,7 @@ import 'package:provider/provider.dart';
|
||||
void main() {
|
||||
///屏幕刷新率和显示率不一致时的优化
|
||||
// GestureBinding.instance.resamplingEnabled = true;
|
||||
runApp(
|
||||
MultiProvider(
|
||||
providers: [
|
||||
ChangeNotifierProvider(
|
||||
create: (context) => RegionProvider(),
|
||||
),
|
||||
ChangeNotifierProvider(create: (context) => TokenProvider()),
|
||||
],
|
||||
child: const MyApp(),
|
||||
),
|
||||
);
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
@ -29,7 +19,14 @@ class MyApp extends StatelessWidget {
|
||||
// This widget is the root of your application.
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
return MultiProvider(
|
||||
providers: [
|
||||
ChangeNotifierProvider(
|
||||
create: (context) => RegionProvider(),
|
||||
),
|
||||
ChangeNotifierProvider(create: (context) => TokenProvider()),
|
||||
],
|
||||
child: MaterialApp(
|
||||
title: '风水罗盘',
|
||||
theme: ThemeData(
|
||||
primarySwatch: createMaterialColor(const Color(0xCFA77300)),
|
||||
@ -46,6 +43,7 @@ class MyApp extends StatelessWidget {
|
||||
Locale('zh'),
|
||||
Locale('en'),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -21,10 +21,10 @@ class DioUtils {
|
||||
bool isProxy = false;
|
||||
|
||||
//网络代理地址
|
||||
String proxyIp = "192.168.0.107";
|
||||
String proxyIp = "10.0.2.2";
|
||||
|
||||
//网络代理端口
|
||||
String proxyPort = "8888";
|
||||
String proxyPort = "7890";
|
||||
|
||||
DioUtils._internal() {
|
||||
BaseOptions options = new BaseOptions();
|
||||
@ -135,8 +135,8 @@ class DioUtils {
|
||||
// _dio.options.headers["content-type"]="multipart/form-data";
|
||||
//发起post请求
|
||||
try {
|
||||
Response response = await _dio.post(url,
|
||||
data: form ?? jsonMap, cancelToken: cancelTag);
|
||||
Response response =
|
||||
await _dio.post(url, data: form ?? jsonMap, cancelToken: cancelTag);
|
||||
//响应数据
|
||||
dynamic responseData = response.data;
|
||||
|
||||
@ -216,8 +216,8 @@ class DioUtils {
|
||||
// _dio.options.headers["content-type"]="multipart/form-data";
|
||||
//发起post请求
|
||||
try {
|
||||
Response response = await _dio.post(url,
|
||||
data: form ?? jsonMap, cancelToken: cancelTag);
|
||||
Response response =
|
||||
await _dio.post(url, data: form ?? jsonMap, cancelToken: cancelTag);
|
||||
//响应数据
|
||||
dynamic responseData = response.data;
|
||||
|
||||
@ -316,8 +316,10 @@ class LoginInfo {
|
||||
String msg;
|
||||
String token;
|
||||
|
||||
LoginInfo({this.success = true, this.code = 200, this.token, this.msg="登陆成功"});
|
||||
LoginInfo.error({this.success = false, this.code = 201, this.msg="请求异常"});
|
||||
LoginInfo(
|
||||
{this.success = true, this.code = 200, this.token, this.msg = "登陆成功"});
|
||||
|
||||
LoginInfo.error({this.success = false, this.code = 201, this.msg = "请求异常"});
|
||||
}
|
||||
|
||||
class RegisterInfo {
|
||||
@ -325,7 +327,8 @@ class RegisterInfo {
|
||||
int code;
|
||||
String msg;
|
||||
|
||||
RegisterInfo({this.success = true, this.code = 200, this.msg = "注册成功"});
|
||||
|
||||
RegisterInfo({this.success = true, this.code = 200, this.msg="注册成功"});
|
||||
RegisterInfo.error({this.success = false, this.code = 201, this.msg="请求异常"});
|
||||
RegisterInfo.error(
|
||||
{this.success = false, this.code = 201, this.msg = "请求异常"});
|
||||
}
|
||||
|
@ -268,7 +268,7 @@ class _CompassState extends State<CompassPage> {
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: Text('选择城市校准磁偏角'),
|
||||
title: const Text('选择城市校准磁偏角'),
|
||||
content: RegionSelector(),
|
||||
actions: <Widget>[
|
||||
TextButton(
|
||||
|
@ -188,8 +188,8 @@ class _LoginPageState extends State<LoginPage> {
|
||||
if (userName.trim().length != 11) {
|
||||
Fluttertoast.showToast(
|
||||
msg: "请输入11位手机号",
|
||||
backgroundColor: Colors.red,
|
||||
textColor: Colors.orange,
|
||||
backgroundColor: Colors.orange,
|
||||
textColor: Colors.white,
|
||||
fontSize: 20.0);
|
||||
return;
|
||||
}
|
||||
@ -218,7 +218,7 @@ class _LoginPageState extends State<LoginPage> {
|
||||
if (responseInfo.success) {
|
||||
Map<String, dynamic> res = {"token": responseInfo.token.toString()};
|
||||
LoginBean loginBean = LoginBean.fromMap(res);
|
||||
TokenHelper.getInstance.loginBean = loginBean;
|
||||
// TokenHelper.getInstance.loginBean = loginBean;
|
||||
// 将token添加到状态管理器中
|
||||
Provider.of<TokenProvider>(context, listen: false).setToken(loginBean);
|
||||
Fluttertoast.showToast(msg: "登录成功");
|
||||
|
@ -21,13 +21,13 @@ class _PersonalLoginPageState extends State<PersonalLoginPage> {
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text("个人中心"),
|
||||
title: const Text("个人中心"),
|
||||
),
|
||||
body: Container(
|
||||
width: double.infinity,
|
||||
child: Column(
|
||||
children: [
|
||||
Padding(padding: EdgeInsets.only(top: 80)),
|
||||
const Padding(padding: EdgeInsets.only(top: 80)),
|
||||
SizedBox(
|
||||
height: 120,
|
||||
child: ClipRRect(
|
||||
@ -35,15 +35,15 @@ class _PersonalLoginPageState extends State<PersonalLoginPage> {
|
||||
child: Image.asset("assets/images/ic_launcher.png"),
|
||||
),
|
||||
),
|
||||
Padding(padding: EdgeInsets.only(top: 30)),
|
||||
const Padding(padding: EdgeInsets.only(top: 30)),
|
||||
ListTile(
|
||||
title: Text("关于我们"),
|
||||
title: const Text("关于我们"),
|
||||
trailing: const Icon(Icons.arrow_forward_ios_sharp),
|
||||
leading: const Icon(Icons.account_circle),
|
||||
onTap: () {},
|
||||
),
|
||||
ListTile(
|
||||
title: Text("退出登陆"),
|
||||
title: const Text("退出登陆"),
|
||||
trailing: const Icon(Icons.arrow_forward_ios_sharp),
|
||||
leading: const Icon(Icons.cancel_rounded),
|
||||
onTap: () async {
|
||||
@ -74,7 +74,7 @@ class _PersonalLoginPageState extends State<PersonalLoginPage> {
|
||||
});
|
||||
|
||||
if (isExit) {
|
||||
TokenHelper.getInstance.clear();
|
||||
// TokenHelper.getInstance.clear();
|
||||
//在状态管理器中清除token
|
||||
Provider.of<TokenProvider>(context, listen: false)
|
||||
.clearToken();
|
||||
|
@ -1,7 +1,13 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_pickers/address_picker/locations_data.dart';
|
||||
import 'package:path/path.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
|
||||
import '../models/region.dart';
|
||||
|
||||
@ -14,6 +20,10 @@ class RegionProvider extends ChangeNotifier {
|
||||
String _cityName = '';
|
||||
String _tempProvinceName = '';
|
||||
String _tempCityName = '';
|
||||
double _declination = 0.0;
|
||||
double _tempDeclination = 0.0;
|
||||
|
||||
Database db;
|
||||
|
||||
String get provinceName => _provinceName;
|
||||
|
||||
@ -23,39 +33,93 @@ class RegionProvider extends ChangeNotifier {
|
||||
|
||||
String get tempCityName => _tempCityName;
|
||||
|
||||
double get tempDeclination => _tempDeclination;
|
||||
|
||||
double get declination => _declination;
|
||||
|
||||
final Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
|
||||
|
||||
loadRegionData() {
|
||||
_prefs.then((prefs) {
|
||||
loadRegionData() async {
|
||||
db = await loadDatabase();
|
||||
final prefs = await _prefs;
|
||||
if (prefs.getString('REGION') != null) {
|
||||
final region =
|
||||
RegionData.fromJson(jsonDecode(prefs.getString('REGION')));
|
||||
final region = RegionData.fromJson(jsonDecode(prefs.getString('REGION')));
|
||||
_provinceName = region.provinceName;
|
||||
_cityName = region.cityName;
|
||||
_tempProvinceName = region.provinceName;
|
||||
_tempCityName = region.cityName;
|
||||
notifyListeners();
|
||||
}
|
||||
});
|
||||
// WidgetsFlutterBinding.ensureInitialized();
|
||||
List<String> cityCode = Address.getCityCodeByName(
|
||||
provinceName: _provinceName, cityName: _cityName);
|
||||
if (cityCode.length == 2) {
|
||||
List<Map<String, Object>> decMap = await db.rawQuery(
|
||||
"SELECT declination FROM magnetic_declination WHERE cityId == ${cityCode[1]}");
|
||||
_declination = decMap[0]['declination'];
|
||||
_tempDeclination = decMap[0]['declination'];
|
||||
} else {
|
||||
_declination = 0.0;
|
||||
_tempDeclination = 0.0;
|
||||
}
|
||||
|
||||
updateRegion(pname, cname) {
|
||||
// _provinceName = pname;
|
||||
// _cityName = cname;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<Database> loadDatabase() async {
|
||||
var databasesPath = await getDatabasesPath();
|
||||
var path = join(databasesPath, "magnetic_declination.db");
|
||||
var exists = await databaseExists(path);
|
||||
if (!exists) {
|
||||
print("Creating new copy from asset");
|
||||
try {
|
||||
await Directory(dirname(path)).create(recursive: true);
|
||||
} catch (_) {}
|
||||
// Copy from asset
|
||||
ByteData data =
|
||||
await rootBundle.load(join("assets", "magnetic_declination.db"));
|
||||
List<int> bytes =
|
||||
data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
|
||||
// Write and flush the bytes written
|
||||
await File(path).writeAsBytes(bytes, flush: true);
|
||||
} else {
|
||||
print("Opening existing database");
|
||||
}
|
||||
// open the database
|
||||
Database _db = await openDatabase(path, readOnly: true);
|
||||
return _db;
|
||||
}
|
||||
|
||||
// 存储临时的省市名称,不会保存本地
|
||||
void updateRegion(pname, cname) async {
|
||||
_tempProvinceName = pname;
|
||||
_tempCityName = cname;
|
||||
|
||||
List<String> cityCode = Address.getCityCodeByName(
|
||||
provinceName: _tempProvinceName, cityName: _tempCityName);
|
||||
print(cityCode);
|
||||
if (cityCode.length == 2) {
|
||||
List<Map<String, Object>> decMap = await db.rawQuery(
|
||||
"SELECT declination FROM magnetic_declination WHERE cityId == ${cityCode[1]}");
|
||||
print(decMap);
|
||||
_tempDeclination = decMap[0]['declination'];
|
||||
} else {
|
||||
_tempDeclination = 0.0;
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
//更新磁偏角
|
||||
void updateDeclination(pname, cname) {}
|
||||
|
||||
//将临时变量保存到正式变量,并保存本地
|
||||
saveRegion() {
|
||||
void saveRegion() {
|
||||
_provinceName = _tempProvinceName;
|
||||
_cityName = _tempCityName;
|
||||
_declination = _tempDeclination;
|
||||
print(_tempCityName);
|
||||
final region = <String, dynamic>{
|
||||
"provinceName": _tempProvinceName,
|
||||
'cityName': _tempCityName
|
||||
'cityName': _tempCityName,
|
||||
// 'declination': _tempDeclination
|
||||
};
|
||||
_prefs.then((prefs) {
|
||||
prefs.setString('REGION', jsonEncode(region));
|
||||
@ -64,16 +128,18 @@ class RegionProvider extends ChangeNotifier {
|
||||
}
|
||||
|
||||
// 覆盖临时的变量
|
||||
resetTemp() async {
|
||||
void resetTemp() async {
|
||||
_tempProvinceName = _provinceName;
|
||||
_tempCityName = _cityName;
|
||||
_tempDeclination = _declination;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
// 将临时的省市名设为空
|
||||
clearTemp() {
|
||||
void clearTemp() {
|
||||
_tempProvinceName = '';
|
||||
_tempCityName = '';
|
||||
_tempDeclination = 0.0;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ class TokenProvider extends ChangeNotifier {
|
||||
|
||||
bool get isLogin => _loginBean != null;
|
||||
|
||||
loadToken() {
|
||||
void loadToken() {
|
||||
Map<String, dynamic> map = SPUtil.getObject("token_bean");
|
||||
if (map != null) {
|
||||
//加载缓存
|
||||
@ -21,13 +21,15 @@ class TokenProvider extends ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
clearToken() {
|
||||
void clearToken() {
|
||||
_loginBean = null;
|
||||
SPUtil.remove("token_bean");
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
setToken(LoginBean lb) {
|
||||
void setToken(LoginBean lb) {
|
||||
_loginBean = lb;
|
||||
SPUtil.saveObject("token_bean", lb);
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
23
pubspec.lock
23
pubspec.lock
@ -192,7 +192,7 @@ packages:
|
||||
source: hosted
|
||||
version: "2.0.2"
|
||||
path:
|
||||
dependency: transitive
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: path
|
||||
url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/"
|
||||
@ -315,6 +315,20 @@ packages:
|
||||
url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/"
|
||||
source: hosted
|
||||
version: "1.8.2"
|
||||
sqflite:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: sqflite
|
||||
url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/"
|
||||
source: hosted
|
||||
version: "2.0.2+1"
|
||||
sqflite_common:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: sqflite_common
|
||||
url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/"
|
||||
source: hosted
|
||||
version: "2.2.1+1"
|
||||
stack_trace:
|
||||
dependency: transitive
|
||||
description:
|
||||
@ -343,6 +357,13 @@ packages:
|
||||
url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/"
|
||||
source: hosted
|
||||
version: "1.1.0"
|
||||
synchronized:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: synchronized
|
||||
url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/"
|
||||
source: hosted
|
||||
version: "3.0.0+2"
|
||||
term_glyph:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
@ -32,6 +32,8 @@ dependencies:
|
||||
url: https://gitee.com/liang-fu/flutter_serial_port_api.git
|
||||
flutter_pickers: ^2.1.9
|
||||
provider: ^6.0.3
|
||||
sqflite: ^2.0.2+1
|
||||
path: ^1.8.1
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
@ -52,6 +54,7 @@ flutter:
|
||||
- assets/index.html
|
||||
- assets/test.js
|
||||
- assets/images/
|
||||
- assets/magnetic_declination.db
|
||||
|
||||
fonts:
|
||||
- family: iconfont # 从 iconfont.json 中获取
|
||||
|
Reference in New Issue
Block a user