You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

337 lines
11 KiB

import 'package:amap_flutter_location/amap_flutter_location.dart';
import 'package:amap_flutter_location/amap_location_option.dart';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:huixiang/generated/l10n.dart';
import 'package:huixiang/retrofit/data/base_data.dart';
import 'package:huixiang/retrofit/data/store.dart';
import 'package:huixiang/retrofit/retrofit_api.dart';
import 'package:huixiang/view_widget/icon_text.dart';
import 'package:huixiang/view_widget/loading_view.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:amap_flutter_base/amap_flutter_base.dart';
class StoreSelectorPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _StoreSelectorPage();
}
}
class _StoreSelectorPage extends State<StoreSelectorPage> {
ApiService apiService;
AMapFlutterLocation aMapFlutterLocation;
@override
void dispose() {
super.dispose();
aMapFlutterLocation.stopLocation();
aMapFlutterLocation.destroy();
}
@override
void initState() {
super.initState();
SharedPreferences.getInstance().then((value) {
apiService =
ApiService(Dio(), context: context, token: value.getString('token'));
});
if (aMapFlutterLocation == null) {
AMapFlutterLocation.setApiKey("f39d1daa020a56f208eb2519f63e9534",
"feaae7986201b571cace1b83728be5bb");
aMapFlutterLocation = AMapFlutterLocation();
aMapFlutterLocation.onLocationChanged().listen((event) {
if (event != null &&
event["latitude"] != null &&
event["longitude"] != null) {
print("location: $event");
aMapFlutterLocation.stopLocation();
if (event["latitude"] is String && event["longitude"] is String) {
latLng = LatLng(double.tryParse(event["latitude"]),
double.tryParse(event["longitude"]));
} else {
latLng = LatLng(event["latitude"], event["longitude"]);
}
if (Navigator.of(context).canPop()) {
Navigator.of(context).pop();
}
saveLatLng(
latLng, event["province"], event["city"], event["district"]);
queryStore("${event["latitude"]}", "${event["longitude"]}",
event["province"], event["city"], event["district"]);
}
});
}
aMapFlutterLocation.setLocationOption(AMapLocationOption(
needAddress: true,
onceLocation: true,
locationInterval: 100000,
locationMode: AMapLocationMode.Hight_Accuracy,
desiredAccuracy: DesiredAccuracy.ThreeKilometers,
pausesLocationUpdatesAutomatically: true,
));
startLocation();
}
saveLatLng(LatLng latLng, province, city, district) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString("latitude", "${latLng.latitude}");
await prefs.setString("longitude", "${latLng.longitude}");
await prefs.setString("province", province);
await prefs.setString("city", city);
await prefs.setString("district", district);
}
List<Store> storeList;
LatLng latLng;
startLocation() async {
if (await Permission.locationWhenInUse.serviceStatus.isEnabled) {
bool isShown = await Permission.contacts.shouldShowRequestRationale;
if (isShown) {
Fluttertoast.showToast(
msg: "shouldShowRequestRationale",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0);
}
if (await Permission.location.isPermanentlyDenied) {
//openAppSettings
getLatLng();
} else if (await Permission.location.isGranted) {
showCupertinoDialog(
context: context,
barrierDismissible: true,
builder: (context) {
return LoadingView();
});
aMapFlutterLocation.startLocation();
} else {
PermissionStatus permissionStatus = await Permission.location.request();
if (permissionStatus.isGranted) {
startLocation();
} else {
getLatLng();
}
}
} else {
//enabledLocation
getLatLng();
}
}
getLatLng() async {
SharedPreferences.getInstance().then(
(value) => {
if (value.containsKey("latitude") &&
value.containsKey("longitude") &&
value.containsKey("province") &&
value.containsKey("city") &&
value.containsKey("district"))
{
latLng = LatLng(double.tryParse(value.getString("latitude")),
double.tryParse(value.getString("longitude"))),
queryStore(
value.getString("latitude"),
value.getString("longitude"),
value.getString("province"),
value.getString("city"),
value.getString("district"),
),
}
else
{
queryStore("", "", "", "", ""),
}
},
);
}
queryStore(latitude, longitude, province, city, district) async {
BaseData baseData = await apiService.queryStore({
"city": city,
"district": district,
"latitude": latitude,
"longitude": longitude,
"province": province,
"searchKey": ""
});
if (baseData != null && baseData.isSuccess) {
storeList = (baseData.data as List<dynamic>)
.map((e) => Store.fromJson(e))
.toList();
if (mounted) setState(() {});
} else {
Fluttertoast.showToast(msg: baseData.msg);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
S.of(context).mendianxuanzhe,
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 18,
),
),
centerTitle: false,
backgroundColor: Color(0xFFFAFAFA),
elevation: 0,
leading: GestureDetector(
onTap: () {
Navigator.of(context).pop();
},
child: Container(
alignment: Alignment.centerRight,
margin: EdgeInsets.only(left: 10),
padding: EdgeInsets.all(6),
child: Icon(
Icons.arrow_back_ios,
color: Colors.black,
size: 24,
),
),
),
titleSpacing: 2,
leadingWidth: 56,
),
body: Container(
margin: EdgeInsets.only(top: 18),
child: Column(
children: [
Expanded(
child: ListView.builder(
itemCount: storeList != null ? storeList.length : 0,
physics: BouncingScrollPhysics(),
itemBuilder: (context, position) {
return GestureDetector(
onTap: () {
setState(() {
groupValue = position;
});
},
child: buildStoreItem(storeList[position], position),
);
}),
),
InkWell(
onTap: () {
if (groupValue == null) {
Fluttertoast.showToast(msg: "请选择一个门店");
return;
}
Store store = storeList[groupValue];
Navigator.of(context).pop({
"id": store.id,
"address": store.address,
});
},
child: Container(
padding: EdgeInsets.only(top: 16, bottom: 16),
decoration: BoxDecoration(
color: Color(0xFF32A060),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(4),
topRight: Radius.circular(4))),
alignment: Alignment.center,
child: Text(
S.of(context).queren,
style: TextStyle(
fontSize: 16,
color: Color(0xFFFFFFFF),
fontWeight: FontWeight.bold),
),
),
),
],
),
),
);
}
Widget buildStoreItem(Store store, position) {
return Container(
margin: EdgeInsets.only(left: 16, right: 16, top: 8, bottom: 8),
padding: EdgeInsets.all(16),
decoration: BoxDecoration(
color: Colors.white,
boxShadow: [
BoxShadow(
color: Colors.black.withAlpha(12),
offset: Offset(0, 3),
blurRadius: 14,
spreadRadius: 0)
],
borderRadius: BorderRadius.all(Radius.circular(8))),
child: Row(
children: [
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
store.storeName,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
color: Color(0xFF353535),
),
),
SizedBox(
height: 12,
),
IconText(
store.address,
isMax: true,
textAxisAlignment: CrossAxisAlignment.start,
textStyle: TextStyle(color: Color(0xFF353535), fontSize: 14),
leftIcon: Icons.location_on,
iconColor: Color(0xFF32A060),
iconSize: 16,
),
SizedBox(
height: 4,
),
IconText(
(store.openStartTime == null && store.openEndTime == null)
? "全天"
: "${store.openStartTime}-${store.openEndTime}",
textStyle: TextStyle(color: Color(0xFF353535), fontSize: 14),
leftIcon: Icons.access_time,
iconColor: Color(0xFF32A060),
iconSize: 16,
),
],
),
),
Radio(
value: position,
groupValue: groupValue,
activeColor: Colors.green,
onChanged: (value) {
setState(() {
groupValue = value;
});
},
)
],
),
);
}
int groupValue = 0;
}