import 'dart:io'; import 'package:android_intent_plus/android_intent.dart'; import 'package:dio/dio.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_baidu_mapapi_base/flutter_baidu_mapapi_base.dart'; import 'package:flutter_baidu_mapapi_utils/flutter_baidu_mapapi_utils.dart'; import 'package:flutter_bmflocation/bdmap_location_flutter_plugin.dart'; import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.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/utils/font_weight.dart'; import 'package:huixiang/view_widget/icon_text.dart'; import 'package:huixiang/view_widget/my_appbar.dart'; import 'package:huixiang/view_widget/request_permission.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; class StoreSelectorPage extends StatefulWidget { @override State createState() { return _StoreSelectorPage(); } } class _StoreSelectorPage extends State { ApiService apiService; LocationFlutterPlugin aMapFlutterLocation; @override void dispose() { super.dispose(); aMapFlutterLocation.stopLocation(); } @override void initState() { super.initState(); SharedPreferences.getInstance().then((value) { apiService = ApiService(Dio(), context: context, token: value.getString('token')); }); if (aMapFlutterLocation == null) { aMapFlutterLocation = LocationFlutterPlugin(); aMapFlutterLocation.onResultCallback().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 = BMFCoordinate(double.tryParse(event["latitude"]), double.tryParse(event["longitude"])); } else { latLng = BMFCoordinate(event["latitude"], event["longitude"]); } BMFCalculateUtils.coordConvert( coordinate: latLng, fromType: BMF_COORD_TYPE.COMMON, toType: BMF_COORD_TYPE.BD09LL) .then((value) { this.latLng = value; saveLatLng( value, event["province"], event["city"], event["district"]); queryStore("${value.latitude}", "${value.longitude}", event["province"], event["city"], event["district"]); }); } }); } aMapFlutterLocation.prepareLoc({ "coorType": "bd09ll", "isNeedAddres": false, "isNeedAltitude": false, "isNeedLocationPoiList": false, "isNeedLocationDescribe": false, "isNeedNewVersionRgc": false, "scanspan": 0, "openGps": true, "locationMode": 2, }, { "locationMode": "kCLLocationAccuracyBest", "locationTimeout": 10, "reGeocodeTimeout": 10, "activityType": "CLActivityTypeAutomotiveNavigation", "BMKLocationCoordinateType": "BMKLocationCoordinateTypeBMK09LL", "BMKLocationCoordinateType": "BMKLocationCoordinateTypeBMK09LL", "isNeedNewVersionRgc": false, }); startLocation(); } saveLatLng(BMFCoordinate 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 storeList; BMFCoordinate latLng; startLocation() async { if (!(await Permission.locationWhenInUse.serviceStatus.isEnabled)) { enableLocation(); return; } if (await Permission.location.isPermanentlyDenied) { requestDialog(); } else if (await Permission.location.isGranted) { EasyLoading.show(status: S.of(context).zhengzaijiazai); aMapFlutterLocation.startLocation(); Future.delayed(Duration(seconds: 6), () { EasyLoading.dismiss(); }); } else if (await Permission.location.isUndetermined) { await Permission.location.request(); } else { if (Platform.isIOS) { //去设置中心 requestDialog(); } else { await Permission.location.request(); } } } enableLocation() { showCupertinoDialog( context: context, builder: (context) { return RequestPermission( "assets/image/icon_permission_location_bg.webp", S.of(context).nindingweigongnengweikaiqi, S.of(context).weilexiangnintuijianfujindemendianxinxi, S.of(context).dakaidingwei, (result) async { if (result) { final AndroidIntent intent = AndroidIntent( action: 'action_location_source_settings', package: "com.zsw.huixiang"); await intent.launch(); // startLocation(); } }, heightRatioWithWidth: 0.82, ); }, ); } requestDialog() { showCupertinoDialog( context: context, builder: (context) { return RequestPermission( "assets/image/icon_permission_location_bg.webp", S.of(context).nindingweiquanxianweiyunxu, S.of(context).weilexiangnintuijianfujindemendianxinxi, S.of(context).kaiqiquanxian, (result) async { if (result) { await openAppSettings(); if (await Permission.location.isGranted) { startLocation(); } } }, heightRatioWithWidth: 0.82, ); }); } getLatLng() async { SharedPreferences.getInstance().then( (value) => { if (value.containsKey("latitude") && value.containsKey("longitude") && value.containsKey("province") && value.containsKey("city") && value.containsKey("district")) { latLng = BMFCoordinate(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": "", "district": "", "latitude": latitude, "longitude": longitude, "province": "", "searchKey": "" }); if (baseData != null && baseData.isSuccess) { storeList = baseData.data; if (mounted) setState(() {}); } } @override Widget build(BuildContext context) { return Scaffold( appBar: MyAppBar( title: S.of(context).mendianxuanzhe, titleColor: Colors.black, titleSize: 18.sp, background: Color(0xFFFAFAFA), leadingColor: Colors.black, ), body: Container( margin: EdgeInsets.only(top: 18.h), 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) { SmartDialog.showToast(S.of(context).qingxuanzeyigemendian, alignment: Alignment.center); return; } Store store = storeList[groupValue]; Navigator.of(context).pop({ "id": store.id, "address": store.address, }); }, child: Container( padding: EdgeInsets.only(top: 16.h, bottom: 16.h), 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.sp, color: Color(0xFFFFFFFF), fontWeight: MyFontWeight.semi_bold, ), ), ), ), ], ), ), ); } Widget buildStoreItem(Store store, position) { return Container( margin: EdgeInsets.only(left: 16.w, right: 16.w, top: 8.h, bottom: 8.h), 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.circular(8), ), child: Row( children: [ Expanded( child: Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( store.storeName, style: TextStyle( fontSize: 16.sp, fontWeight: MyFontWeight.semi_bold, color: Color(0xFF353535), ), ), SizedBox( height: 12.h, ), IconText( store.address, isMax: true, textAxisAlignment: CrossAxisAlignment.start, textStyle: TextStyle( color: Color(0xFF353535), fontSize: 14.sp, ), leftIcon: Icons.location_on, iconColor: Color(0xFF32A060), iconSize: 16, ), SizedBox( height: 4.h, ), IconText( (store.openStartTime == null && store.openEndTime == null) ? S.of(context).quantian : "${store.openStartTime}-${store.openEndTime}", textStyle: TextStyle( color: Color(0xFF353535), fontSize: 14.sp, ), 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; }