选择罗盘图片

This commit is contained in:
cxc
2022-07-05 16:44:07 +08:00
parent cb01d55de0
commit 22251e8e12
6 changed files with 356 additions and 166 deletions

View File

@ -1,4 +1,5 @@
import 'package:fengshui_compass/bottom_navigation_widget.dart';
import 'package:fengshui_compass/states/compass_image.dart';
import 'package:fengshui_compass/states/region.dart';
import 'package:fengshui_compass/states/token.dart';
import 'package:fengshui_compass/utils/color.dart';
@ -25,6 +26,7 @@ class MyApp extends StatelessWidget {
create: (context) => RegionProvider(),
),
ChangeNotifierProvider(create: (context) => TokenProvider()),
ChangeNotifierProvider(create: (context) => CompassImageProvider())
],
child: MaterialApp(
title: '风水罗盘',

View File

@ -57,13 +57,13 @@ class _BirthCalState extends State<BirthCalPage> {
lunar_month = date.month;
lunar_day = date.day;
_controllerDate.text = "${lunar_year}${lunar_month}${lunar_day}";
_controllerDate.text = "${lunar_year}${lunar_month}${lunar_day} $hour时$minute分";
} else {
year = date.year;
month = date.month;
day = date.day;
_controllerDate.text = "${year}${month}${day}";
_controllerDate.text = "${year}${month}${day} $hour时$minute分";
}
// _controllerDate.text = year.toString()+"年"+month.toString()+"月"+day.toString()+"日";
});
@ -99,7 +99,7 @@ class _BirthCalState extends State<BirthCalPage> {
}
_showDateTimePicker() {
Pickers.showDatePicker(context, mode: DateMode.YMDHMS, onConfirm: (p) {
Pickers.showDatePicker(context, mode: DateMode.YMDHM, onConfirm: (p) {
setState(() {
hour = p.hour;
minute = p.minute;
@ -141,7 +141,7 @@ class _BirthCalState extends State<BirthCalPage> {
month = json.decode(tmp)['cMonth'];
day = json.decode(tmp)['cDay'];
_controllerDate.text = "${lunar_year}${lunar_month}${lunar_day}";
_controllerDate.text = "${lunar_year}${lunar_month}${lunar_day} $hour时$minute分";
});
});
}
@ -167,7 +167,7 @@ class _BirthCalState extends State<BirthCalPage> {
lunar_month = json.decode(tmp)['lMonth'];
lunar_day = json.decode(tmp)['lDay'];
_controllerDate.text = "${year}${month}${day}";
_controllerDate.text = "${year}${month}${day} $hour时$minute分";
});
});
}
@ -345,6 +345,7 @@ class _BirthCalState extends State<BirthCalPage> {
borderRadius: BorderRadius.circular(30)),
alignment: Alignment.center,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Row(
mainAxisSize: MainAxisSize.min,
@ -394,7 +395,7 @@ class _BirthCalState extends State<BirthCalPage> {
backgroundColor: MaterialStateProperty.all(
isLunar
? Colors.transparent
: Color(0xCFA77300)),
: const Color(0xCFA77300)),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius:
@ -432,7 +433,7 @@ class _BirthCalState extends State<BirthCalPage> {
day = json.decode(tmp)['cDay'];
_controllerDate.text =
"${lunar_year}${lunar_month}${lunar_day} $hour时$minute分";
"$lunar_year年$lunar_month月$lunar_day $hour时$minute分";
});
});
}
@ -446,7 +447,7 @@ class _BirthCalState extends State<BirthCalPage> {
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
isLunar
? Color(0xCFA77300)
? const Color(0xCFA77300)
: Colors.transparent),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
@ -456,7 +457,7 @@ class _BirthCalState extends State<BirthCalPage> {
))
],
),
const Padding(padding: EdgeInsets.only(top: 80)),
const Padding(padding: EdgeInsets.only(top: 40)),
Row(
children: [
const Padding(
@ -464,7 +465,7 @@ class _BirthCalState extends State<BirthCalPage> {
left: 10,
right: 10,
),
child: Text("出生日期:", style: TextStyle(fontSize: 18)),
child: Text("出生时间:", style: TextStyle(fontSize: 18)),
),
Expanded(
child: TextField(
@ -510,7 +511,7 @@ class _BirthCalState extends State<BirthCalPage> {
// icon: Icon(Icons.timelapse_outlined))
// ],
// ),
Padding(padding: EdgeInsets.only(top: 80)),
const Padding(padding: EdgeInsets.only(top: 40)),
// 切换奇门类型,单选框
Row(
children: [

View File

@ -8,10 +8,13 @@ import 'package:fengshui_compass/pages/login_page.dart';
import 'package:fengshui_compass/states/region.dart';
import 'package:flutter/material.dart';
import 'package:flutter_serial_port_api/flutter_serial_port_api.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart';
import 'package:provider/provider.dart';
import 'package:stream_transform/stream_transform.dart';
import '../components/region_selector.dart';
import '../states/compass_image.dart';
import '../utils/recv_parse.dart';
class CompassPage extends StatefulWidget {
@ -44,6 +47,9 @@ class _CompassState extends State<CompassPage> {
var listb = [];
var listc = [];
//从相册选择的图片名称
String selectedImageName;
void initDevice() {}
@override
@ -290,6 +296,11 @@ class _CompassState extends State<CompassPage> {
});
}
void initCompassImage() async {
final directory =
await getApplicationDocumentsDirectory(); // AppData directory
}
@override
Widget build(BuildContext context) {
return Container(
@ -329,164 +340,185 @@ class _CompassState extends State<CompassPage> {
constraints: const BoxConstraints(minHeight: 600),
child: Consumer<RegionProvider>(
builder: (builder, regionProvider, child) {
return Stack(
alignment: Alignment.center,
children: [
// 罗盘
Column(
children: [
const Padding(padding: EdgeInsets.only(top: 145)),
Row(
children: [
Spacer(flex: 1),
Container(
width: 700,
height: 700,
child: Stack(
children: [
Transform.rotate(
angle: myaw * pi / 360 +
regionProvider.declination,
child: const Image(
image: AssetImage(
"assets/images/compass_rotated.png"),
fit: BoxFit.fill),
),
Align(
alignment: FractionalOffset(w_x, w_y),
child: const Image(
image:
AssetImage("assets/images/water.png"),
),
),
CrossPaint(),
],
),
),
const Spacer(flex: 1),
],
)
],
),
// 最上面一行, lock azimuth login
Positioned(
top: 5,
child: Column(
children: [
const Image(
width: 15,
height: 15,
image: AssetImage("assets/images/arrow.png"),
fit: BoxFit.contain,
),
Text(
// "${azimuth.toStringAsFixed(2)}",
(myaw + regionProvider.declination)
.toStringAsFixed(2),
style: const TextStyle(
color: Colors.amber, fontSize: 36),
),
],
)),
//磁偏角调整按钮
Positioned(
top: 5,
right: 8,
child: IconButton(
onPressed: () => selectRegion(),
icon: const Icon(Icons.settings, color: Colors.amber),
)),
Positioned(
top: 45,
right: 8,
child: IconButton(
onPressed: () => selectRegion(),
icon: const Icon(Icons.photo, color: Colors.amber),
)),
// 最下面一行ranging value openlaser
Positioned(
bottom: 80,
left: 50,
child: IconButton(
onPressed: raging,
icon: const Icon(
MyIcons.icon_celiang,
size: 34,
),
color: Colors.amber)),
const Positioned(
width: 180,
height: 90,
bottom: 60,
child: Image(
image: AssetImage("assets/images/range_input.png"),
fit: BoxFit.contain,
)),
Positioned(
width: 180,
height: 90,
bottom: 60,
child: Align(
alignment: Alignment.centerRight,
child: Padding(
padding: const EdgeInsets.only(right: 15),
child: Text(
"${distance} m",
style: const TextStyle(
color: Colors.amber, fontSize: 28),
),
),
)),
Positioned(
bottom: 60,
right: 40,
height: 120,
width: 100,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
return Consumer<CompassImageProvider>(
builder: (builder, compassImageProvider, child) {
return Stack(
alignment: Alignment.center,
children: [
// 罗盘
Column(
children: [
IconButton(
color: Colors.amber,
onPressed: () {
if (isUpClose) {
openUpLaser();
} else {
closeUpLaser();
}
setState(() {
isUpClose = !isUpClose;
});
},
icon: Icon(
isUpClose
? MyIcons.icon_shangdeng
: MyIcons.icon_shangdnegguanbi,
size: 36)),
IconButton(
color: Colors.amber,
onPressed: () {
if (isSideClose) {
openSideLaser();
} else {
closeSideLaser();
}
setState(() {
isSideClose = !isSideClose;
});
},
icon: Icon(
isSideClose
? MyIcons.icon_zuoyoudneg
: MyIcons.icon_zuoyoudengguanbi,
size: 32,
))
const Padding(padding: EdgeInsets.only(top: 145)),
Row(
children: [
Spacer(flex: 1),
Container(
width: 700,
height: 700,
child: Stack(
children: [
Transform.rotate(
angle: (myaw + regionProvider.declination) *
2 *
pi /
360,
child: Image(
width: 700,
height: 700,
// alignment: Alignment.bottomLeft,
// image: compassImageProvider.rotateImage,
image: compassImageProvider
.rotateImage ??
const AssetImage(
"assets/images/compass_rotated.png"),
fit: BoxFit.contain),
),
Align(
alignment: FractionalOffset(w_x, w_y),
child: const Image(
image:
AssetImage("assets/images/water.png"),
),
),
CrossPaint(),
],
),
),
const Spacer(flex: 1),
],
)
],
),
)
],
);
// 最上面一行, lock azimuth login
Positioned(
top: 5,
child: Column(
children: [
const Image(
width: 15,
height: 15,
image: AssetImage("assets/images/arrow.png"),
fit: BoxFit.contain,
),
Text(
// "${azimuth.toStringAsFixed(2)}",
(myaw + regionProvider.declination)
.toStringAsFixed(2),
style: const TextStyle(
color: Colors.amber, fontSize: 36),
),
],
)),
//磁偏角调整按钮
Positioned(
top: 5,
right: 8,
child: IconButton(
onPressed: () => selectRegion(),
icon: const Icon(Icons.settings, color: Colors.amber),
)),
Positioned(
top: 5,
left: 8,
child: IconButton(
onPressed: () {
ImagePicker()
.pickImage(source: ImageSource.gallery)
.then((res) {
if (res == null) {
return;
}
compassImageProvider.setSelectedRotateImage(res);
});
},
icon: const Icon(Icons.photo, color: Colors.amber),
)),
// 最下面一行ranging value openlaser
Positioned(
bottom: 80,
left: 50,
child: IconButton(
onPressed: raging,
icon: const Icon(
MyIcons.icon_celiang,
size: 34,
),
color: Colors.amber)),
const Positioned(
width: 180,
height: 90,
bottom: 60,
child: Image(
image: AssetImage("assets/images/range_input.png"),
fit: BoxFit.contain,
)),
Positioned(
width: 180,
height: 90,
bottom: 60,
child: Align(
alignment: Alignment.centerRight,
child: Padding(
padding: const EdgeInsets.only(right: 15),
child: Text(
"${distance} m",
style: const TextStyle(
color: Colors.amber, fontSize: 28),
),
),
)),
Positioned(
bottom: 60,
right: 40,
height: 120,
width: 100,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
IconButton(
color: Colors.amber,
onPressed: () {
if (isUpClose) {
openUpLaser();
} else {
closeUpLaser();
}
setState(() {
isUpClose = !isUpClose;
});
},
icon: Icon(
isUpClose
? MyIcons.icon_shangdeng
: MyIcons.icon_shangdnegguanbi,
size: 36)),
IconButton(
color: Colors.amber,
onPressed: () {
if (isSideClose) {
openSideLaser();
} else {
closeSideLaser();
}
setState(() {
isSideClose = !isSideClose;
});
},
icon: Icon(
isSideClose
? MyIcons.icon_zuoyoudneg
: MyIcons.icon_zuoyoudengguanbi,
size: 32,
))
],
),
)
],
);
});
},
),
)),

View File

@ -0,0 +1,69 @@
import 'dart:io';
import 'package:flutter/widgets.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
class CompassImageProvider extends ChangeNotifier {
CompassImageProvider() {
loadCompassImage();
}
final Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
ImageProvider _rotateImage;
ImageProvider get rotateImage => _rotateImage;
// 加载罗盘图片
void loadCompassImage() async {
final prefs = await _prefs;
final appDataDirectory = await getApplicationDocumentsDirectory();
String pathString = prefs.getString('rotate_image');
if (pathString != null) {
File file = File(join(appDataDirectory.path, pathString));
var isFileExist = await file.exists();
if (isFileExist) {
_rotateImage = FileImage(file);
} else {
_rotateImage = null;
}
} else {
_rotateImage = null;
}
notifyListeners();
}
void setSelectedRotateImage(XFile res) async {
final appDataDirectory = await getApplicationDocumentsDirectory();
print(res.path);
String pathString = res.name;
if (pathString == null) {
return;
}
// imageCache.clear();
// res.saveTo(join(appDataDirectory.path, res.name));
res.saveTo(
join(appDataDirectory.path, 'rotate_image${extension(res.name)}'));
final prefs = await _prefs;
// final appDataDirectory = await getApplicationDocumentsDirectory();
// File file = File(join(appDataDirectory.path, pathString));
// File file =
// File(join(appDataDirectory.path, 'rotate_image${extension(res.name)}'));
File file = File(res.path);
print(file);
var isFileExist = await file.exists();
print(isFileExist);
if (isFileExist) {
_rotateImage = FileImage(file);
} else {
_rotateImage = null;
}
prefs.setString('rotate_image', 'rotate_image${extension(res.name)}');
// prefs.setString('rotate_image', pathString);
// imageCache.clearLiveImages();
notifyListeners();
}
}

View File

@ -43,6 +43,13 @@ packages:
url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/"
source: hosted
version: "1.16.0"
cross_file:
dependency: transitive
description:
name: cross_file
url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/"
source: hosted
version: "0.3.3+1"
cupertino_icons:
dependency: "direct main"
description:
@ -102,6 +109,13 @@ packages:
url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/"
source: hosted
version: "2.1.9"
flutter_plugin_android_lifecycle:
dependency: transitive
description:
name: flutter_plugin_android_lifecycle
url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/"
source: hosted
version: "2.0.6"
flutter_serial_port_api:
dependency: "direct main"
description:
@ -128,6 +142,13 @@ packages:
url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/"
source: hosted
version: "7.1.8"
http:
dependency: transitive
description:
name: http
url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/"
source: hosted
version: "0.13.4"
http_parser:
dependency: transitive
description:
@ -135,6 +156,41 @@ packages:
url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/"
source: hosted
version: "4.0.1"
image_picker:
dependency: "direct main"
description:
name: image_picker
url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/"
source: hosted
version: "0.8.5+3"
image_picker_android:
dependency: transitive
description:
name: image_picker_android
url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/"
source: hosted
version: "0.8.5+1"
image_picker_for_web:
dependency: transitive
description:
name: image_picker_for_web
url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/"
source: hosted
version: "2.1.8"
image_picker_ios:
dependency: transitive
description:
name: image_picker_ios
url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/"
source: hosted
version: "0.8.5+5"
image_picker_platform_interface:
dependency: transitive
description:
name: image_picker_platform_interface
url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/"
source: hosted
version: "2.5.0"
intl:
dependency: transitive
description:
@ -198,6 +254,27 @@ packages:
url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/"
source: hosted
version: "1.8.1"
path_provider:
dependency: "direct main"
description:
name: path_provider
url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/"
source: hosted
version: "2.0.11"
path_provider_android:
dependency: transitive
description:
name: path_provider_android
url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/"
source: hosted
version: "2.0.16"
path_provider_ios:
dependency: transitive
description:
name: path_provider_ios
url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/"
source: hosted
version: "2.0.10"
path_provider_linux:
dependency: transitive
description:
@ -205,6 +282,13 @@ packages:
url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/"
source: hosted
version: "2.1.7"
path_provider_macos:
dependency: transitive
description:
name: path_provider_macos
url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/"
source: hosted
version: "2.0.6"
path_provider_platform_interface:
dependency: transitive
description:

View File

@ -34,6 +34,8 @@ dependencies:
provider: ^6.0.3
sqflite: ^2.0.2+1
path: ^1.8.1
image_picker: ^0.8.5+3
path_provider: ^2.0.11
dev_dependencies:
flutter_test: