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.
704 lines
24 KiB
704 lines
24 KiB
import 'dart:io'; |
|
import 'dart:ui'; |
|
|
|
import 'package:amap_flutter_location/amap_flutter_location.dart'; |
|
import 'package:amap_flutter_location/amap_location_option.dart'; |
|
import 'package:android_intent_plus/android_intent.dart'; |
|
import 'package:dio/dio.dart'; |
|
import 'package:flutter/cupertino.dart'; |
|
import 'package:flutter/foundation.dart'; |
|
import 'package:flutter/gestures.dart'; |
|
import 'package:flutter/material.dart'; |
|
import 'package:flutter/services.dart'; |
|
import 'package:huixiang/generated/l10n.dart'; |
|
import 'package:huixiang/main.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/event_type.dart'; |
|
import 'package:huixiang/view_widget/classic_header.dart'; |
|
import 'package:huixiang/view_widget/custom_image.dart'; |
|
import 'package:huixiang/view_widget/item_title.dart'; |
|
import 'package:amap_flutter_base/amap_flutter_base.dart'; |
|
import 'package:amap_flutter_map/amap_flutter_map.dart'; |
|
import 'package:fluttertoast/fluttertoast.dart'; |
|
import 'package:huixiang/view_widget/loading_view.dart'; |
|
import 'package:huixiang/view_widget/request_permission.dart'; |
|
import 'package:permission_handler/permission_handler.dart'; |
|
import 'package:pull_to_refresh/pull_to_refresh.dart'; |
|
import 'package:shared_preferences/shared_preferences.dart'; |
|
import 'dart:typed_data'; |
|
import 'package:flutter/rendering.dart'; |
|
import 'package:flutter_screenutil/flutter_screenutil.dart'; |
|
|
|
class UnionPage extends StatefulWidget { |
|
@override |
|
State<StatefulWidget> createState() { |
|
return _UnionPage(); |
|
} |
|
} |
|
|
|
class _UnionPage extends State<UnionPage> with AutomaticKeepAliveClientMixin, WidgetsBindingObserver { |
|
//默认设置为不使用自定义地图,如果需要直接显示,在初始化是设置为true |
|
CustomStyleOptions _customStyleOptions = CustomStyleOptions(false); |
|
|
|
//加载自定义地图样式 |
|
void _loadCustomData() async { |
|
if (null == _customStyleOptions) { |
|
_customStyleOptions = CustomStyleOptions(false); |
|
} |
|
ByteData styleByteData = |
|
await rootBundle.load('assets/map_style/style.data'); |
|
_customStyleOptions.styleData = styleByteData.buffer.asUint8List(); |
|
ByteData styleExtraByteData = |
|
await rootBundle.load('assets/map_style/style_extra.data'); |
|
_customStyleOptions.styleExtraData = |
|
styleExtraByteData.buffer.asUint8List(); |
|
//如果需要加载完成后直接展示自定义地图,可以通过setState修改CustomStyleOptions的enable为true |
|
setState(() { |
|
_customStyleOptions.enabled = true; |
|
}); |
|
} |
|
|
|
AMapFlutterLocation aMapFlutterLocation; |
|
RefreshController refreshController = |
|
RefreshController(initialRefresh: false); |
|
|
|
@override |
|
void dispose() { |
|
super.dispose(); |
|
WidgetsBinding.instance.removeObserver(this); |
|
aMapFlutterLocation.stopLocation(); |
|
aMapFlutterLocation.destroy(); |
|
} |
|
|
|
bool iskeyboardShow = false; |
|
|
|
@override |
|
void didChangeMetrics() { |
|
super.didChangeMetrics(); |
|
WidgetsBinding.instance.addPostFrameCallback((_) { |
|
setState(() { |
|
print("object: ${MediaQuery.of(context).viewInsets.bottom}"); |
|
if(MediaQuery.of(context).viewInsets.bottom == 0){ |
|
if (iskeyboardShow) { |
|
iskeyboardShow = false; |
|
//关闭键盘 软键盘关闭了, 清除输入控件的焦点, 否则重新进入页面会导致软键盘再弹出问题 |
|
FocusScope.of(context).requestFocus(FocusNode()); |
|
} |
|
} else { |
|
iskeyboardShow = true; |
|
} |
|
}); |
|
}); |
|
} |
|
|
|
ApiService apiService; |
|
|
|
@override |
|
void initState() { |
|
super.initState(); |
|
|
|
WidgetsBinding.instance.addObserver(this); |
|
|
|
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"); |
|
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"]); |
|
} |
|
saveLatLng( |
|
latLng, event["province"], event["city"], event["district"]); |
|
queryStore( |
|
"${event["latitude"]}", |
|
"${event["longitude"]}", |
|
event["province"], |
|
event["city"], |
|
event["district"], |
|
editingController.text); |
|
if (_mapController != null) |
|
_mapController.moveCamera( |
|
CameraUpdate.newCameraPosition(CameraPosition( |
|
target: latLng, |
|
zoom: 15.0, |
|
)), |
|
); |
|
} |
|
}); |
|
|
|
eventBus.on<EventType>().listen((event) { |
|
print("object: UnionPage"); |
|
setState(() {}); |
|
}); |
|
} |
|
aMapFlutterLocation.setLocationOption(AMapLocationOption( |
|
needAddress: true, |
|
onceLocation: true, |
|
locationMode: AMapLocationMode.Hight_Accuracy, |
|
desiredAccuracy: DesiredAccuracy.ThreeKilometers, |
|
pausesLocationUpdatesAutomatically: true, |
|
)); |
|
|
|
_loadCustomData(); |
|
|
|
getLatLng(); |
|
startLocation(); |
|
} |
|
|
|
LatLng latLng; |
|
|
|
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); |
|
} |
|
|
|
getLatLng() async { |
|
SharedPreferences.getInstance().then( |
|
(value) => { |
|
apiService = ApiService(Dio(), |
|
context: context, token: value.getString('token')), |
|
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"), |
|
editingController.text, |
|
), |
|
setState(() { |
|
if (_mapController != null) { |
|
_mapController.moveCamera( |
|
CameraUpdate.newCameraPosition( |
|
CameraPosition( |
|
target: latLng, |
|
zoom: 15.0, |
|
), |
|
), |
|
); |
|
} |
|
}) |
|
} |
|
else |
|
{ |
|
queryStore("", "", "", "", "", editingController.text), |
|
} |
|
}, |
|
); |
|
} |
|
|
|
List<Store> storeList; |
|
bool isFirst = true; |
|
|
|
queryStore(latitude, longitude, province, city, district, searchKey) async { |
|
BaseData baseData = await apiService.queryStore({ |
|
// "city": city, |
|
// "district": district, |
|
// "province": province, |
|
"latitude": latitude, |
|
"longitude": longitude, |
|
"searchKey": searchKey |
|
}).catchError((error) { |
|
refreshController.refreshFailed(); |
|
}); |
|
|
|
if (Navigator.canPop(context) && !isFirst) Navigator.of(context).pop(); |
|
|
|
isFirst = false; |
|
|
|
if (baseData != null && baseData.isSuccess) { |
|
storeList = (baseData.data as List<dynamic>) |
|
.map((e) => Store.fromJson(e)) |
|
.toList(); |
|
|
|
buildMarker(); |
|
|
|
refreshController.refreshCompleted(); |
|
setState(() {}); |
|
} else { |
|
refreshController.refreshFailed(); |
|
Fluttertoast.showToast(msg: baseData.msg); |
|
} |
|
} |
|
|
|
RepaintBoundary repaintBoundary; |
|
|
|
buildMarker() async { |
|
markers.clear(); |
|
|
|
BitmapDescriptor bitmapDescriptor = await BitmapDescriptor.fromAssetImage( |
|
ImageConfiguration( |
|
bundle: DefaultAssetBundle.of(context), |
|
devicePixelRatio: MediaQuery.of(context)?.devicePixelRatio ?? 1.0, |
|
locale: Localizations.localeOf(context), |
|
textDirection: Directionality.of(context), |
|
size: Size(35.w, 35.h), |
|
platform: defaultTargetPlatform, |
|
), |
|
"assets/image/icon_map_marker.png"); |
|
|
|
markers.addAll(storeList.map((element) => Marker( |
|
position: LatLng(double.tryParse(element.latitude), |
|
double.tryParse(element.longitude)), |
|
anchor: Offset(0.5, 0.9), |
|
clickable: false, |
|
icon: bitmapDescriptor, |
|
infoWindowEnable: true, |
|
))); |
|
setState(() {}); |
|
} |
|
|
|
showLoadingDialog() { |
|
showCupertinoDialog( |
|
context: context, |
|
barrierDismissible: true, |
|
builder: (context) { |
|
return LoadingView(); |
|
}); |
|
} |
|
|
|
List<Marker> markers = []; |
|
|
|
@override |
|
Widget build(BuildContext context) { |
|
super.build(context); |
|
AMapApiKey aMapApiKeys = AMapApiKey( |
|
androidKey: 'f39d1daa020a56f208eb2519f63e9534', |
|
iosKey: 'feaae7986201b571cace1b83728be5bb'); |
|
return GestureDetector( |
|
onTap: () { |
|
FocusScope.of(context).requestFocus(FocusNode()); |
|
}, |
|
child: Scaffold( |
|
resizeToAvoidBottomInset: true, |
|
body: NestedScrollView( |
|
physics: PageScrollPhysics(), |
|
headerSliverBuilder: (context, inner) { |
|
return [ |
|
SliverOverlapAbsorber( |
|
sliver: buildSliverAppBar(AMapWidget( |
|
initialCameraPosition: CameraPosition( |
|
target: LatLng(30.553111, 114.342366), |
|
zoom: 12.0, |
|
), |
|
onMapCreated: onMapCreated, |
|
apiKey: aMapApiKeys, |
|
touchPoiEnabled: true, |
|
markers: markers.toSet(), |
|
scrollGesturesEnabled: true, |
|
customStyleOptions: _customStyleOptions, |
|
onPoiTouched: (poiTouch) { |
|
FocusScope.of(context).requestFocus(FocusNode()); |
|
}, |
|
gestureRecognizers: <Factory<OneSequenceGestureRecognizer>>[ |
|
Factory<OneSequenceGestureRecognizer>( |
|
() => EagerGestureRecognizer()), |
|
].toSet(), |
|
)), |
|
handle: |
|
NestedScrollView.sliverOverlapAbsorberHandleFor(context), |
|
) |
|
]; |
|
}, |
|
body: Builder( |
|
builder: (context) { |
|
return CustomScrollView( |
|
physics: NeverScrollableScrollPhysics(), |
|
slivers: [ |
|
SliverOverlapInjector( |
|
handle: NestedScrollView.sliverOverlapAbsorberHandleFor( |
|
context), |
|
), |
|
SliverToBoxAdapter( |
|
child: Container( |
|
height: MediaQuery.of(context).size.height - |
|
102.h - |
|
MediaQuery.of(context).padding.top, |
|
child: SmartRefresher( |
|
controller: refreshController, |
|
enablePullUp: false, |
|
enablePullDown: true, |
|
physics: BouncingScrollPhysics(), |
|
header: MyHeader(), |
|
onRefresh: () { |
|
startLocation(); |
|
}, |
|
child: ListView.builder( |
|
itemCount: |
|
storeList == null ? 0 : storeList.length, |
|
padding: EdgeInsets.only( |
|
top: 8.h, |
|
bottom: 84.h + (375.h - 88.h) + 4.h), |
|
physics: NeverScrollableScrollPhysics(), |
|
itemBuilder: (context, position) { |
|
return GestureDetector( |
|
onTap: () { |
|
Navigator.of(context).pushNamed( |
|
'/router/union_detail_page', |
|
arguments: { |
|
"id": storeList[position].id |
|
}); |
|
}, |
|
child: buildStoreItem( |
|
storeList[position], position), |
|
); |
|
}), |
|
), |
|
), |
|
), |
|
], |
|
); |
|
}, |
|
)), |
|
), |
|
); |
|
} |
|
|
|
startLocation() async { |
|
if (Platform.isAndroid) { |
|
if (!(await Permission.locationWhenInUse.serviceStatus.isEnabled)) { |
|
enableLocation(); |
|
refreshController.refreshCompleted(); |
|
return; |
|
} |
|
} |
|
|
|
print("object: isGranted: ${await Permission.location.isGranted}"); |
|
print("object: isDenied: ${await Permission.location.isDenied}"); |
|
print("object: isLimited: ${await Permission.location.isLimited}"); |
|
print("object: isPermanentlyDenied: ${await Permission.location.isPermanentlyDenied}"); |
|
print("object: isRestricted: ${await Permission.location.isRestricted}"); |
|
print("object: isUndetermined: ${await Permission.location.isUndetermined}"); |
|
print("object: shouldShowRequestRationale: ${await Permission.location.shouldShowRequestRationale}"); |
|
print("object: status: ${await Permission.location.status}"); |
|
print("object: value: -------------------------"); |
|
|
|
if (await Permission.location.isPermanentlyDenied) { |
|
print("object: requestDialog"); |
|
requestDialog(); |
|
refreshController.refreshCompleted(); |
|
} else if (await Permission.location.isGranted) { |
|
print("object: showLoadingDialog"); |
|
showLoadingDialog(); |
|
aMapFlutterLocation.startLocation(); |
|
} else { |
|
if (Platform.isAndroid) { |
|
await Permission.location.request(); |
|
} else if (Platform.isIOS) { |
|
//去设置中心 |
|
iosLocationSetting(); |
|
} |
|
refreshController.refreshCompleted(); |
|
} |
|
} |
|
|
|
iosLocationSetting() async { |
|
showCupertinoDialog( |
|
context: context, |
|
builder: (context) { |
|
return RequestPermission( |
|
"assets/image/icon_permission_location_bg.png", |
|
S.of(context).nindingweigongnengweikaiqi, |
|
S.of(context).weilexiangnintuijianfujindemendianxinxi, |
|
S.of(context).queding, |
|
(result) async { |
|
if (result) { |
|
|
|
} |
|
}, |
|
heightRatioWithWidth: 0.84, |
|
); |
|
}); |
|
} |
|
|
|
enableLocation() { |
|
showCupertinoDialog( |
|
context: context, |
|
builder: (context) { |
|
return RequestPermission( |
|
"assets/image/icon_permission_location_bg.png", |
|
"您定位功能开关未开启,请点击去打開定位", |
|
"为了向您推荐附近的门店信息,推荐您在使用期间让我们使用位置信息", |
|
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.84, |
|
); |
|
}); |
|
} |
|
|
|
requestDialog() { |
|
showCupertinoDialog( |
|
context: context, |
|
builder: (context) { |
|
return RequestPermission( |
|
"assets/image/icon_permission_location_bg.png", |
|
"您未开启位置权限,请点击开启", |
|
"为了向您推荐附近的门店信息,推荐您在使用期间让我们使用位置信息", |
|
S.of(context).kaiqiquanxian, (result) async { |
|
if (result) { |
|
if (Platform.isAndroid) { |
|
final AndroidIntent intent = AndroidIntent( |
|
action: 'action_application_details_settings', |
|
data: 'package:com.zsw.huixiang'); |
|
await intent.launch(); |
|
if (await Permission.location.isGranted) { |
|
startLocation(); |
|
} |
|
} |
|
} |
|
}); |
|
}); |
|
} |
|
|
|
AMapController _mapController; |
|
TextEditingController editingController = TextEditingController(); |
|
|
|
void onMapCreated(AMapController controller) { |
|
_mapController = controller; |
|
} |
|
|
|
Widget buildSliverAppBar(AMapWidget map) { |
|
return SliverAppBar( |
|
// 滑上去时搜索隐藏 |
|
// floating: true, |
|
// snap: true, |
|
pinned: true, |
|
backgroundColor: Color(0xFFFAFAFA), |
|
elevation: 0, |
|
automaticallyImplyLeading: false, |
|
title: Container( |
|
height: 36.h, |
|
margin: EdgeInsets.fromLTRB(16.w, 0, 16.w, 0), |
|
padding: EdgeInsets.fromLTRB(0, 6.h, 0, 6.h), |
|
decoration: BoxDecoration( |
|
color: Colors.white, |
|
borderRadius: BorderRadius.all(Radius.circular(4)), |
|
boxShadow: [ |
|
BoxShadow( |
|
color: Colors.black.withAlpha(12), |
|
offset: Offset(0, 3), |
|
blurRadius: 14, |
|
spreadRadius: 0, |
|
) |
|
]), |
|
child: TextField( |
|
textInputAction: TextInputAction.search, |
|
onEditingComplete: () { |
|
startLocation(); |
|
}, |
|
controller: editingController, |
|
cursorHeight: 30.h, |
|
decoration: InputDecoration( |
|
contentPadding: EdgeInsets.symmetric(vertical: 12.h), |
|
prefixIcon: Icon( |
|
Icons.search, |
|
size: 24, |
|
color: Colors.black, |
|
), |
|
suffixIcon: InkWell( |
|
onTap: () { |
|
editingController.clear(); |
|
}, |
|
child: Icon( |
|
Icons.close, |
|
size: 19, |
|
color: Colors.grey, |
|
), |
|
), |
|
border: InputBorder.none, |
|
), |
|
), |
|
), |
|
flexibleSpace: FlexibleSpaceBar( |
|
background: Container( |
|
child: map, |
|
), |
|
), |
|
expandedHeight: 375.h, |
|
bottom: PreferredSize( |
|
preferredSize: Size(double.infinity, 52.h), |
|
child: Container( |
|
padding: EdgeInsets.only(top: 6.h), |
|
color: Color(0xFFFAFAFA), |
|
child: ItemTitle( |
|
text: S.of(context).jingbilianmenghuiyuandian, |
|
imgPath: "assets/image/icon_union_store.png", |
|
), |
|
), |
|
), |
|
); |
|
} |
|
|
|
Widget buildStoreItem(Store store, position) { |
|
return Container( |
|
margin: EdgeInsets.fromLTRB(16.w, 8.h, 16.w, 8.h), |
|
padding: EdgeInsets.fromLTRB(20.w, 20.h, 20.w, 20.h), |
|
decoration: BoxDecoration( |
|
color: Colors.white, |
|
borderRadius: BorderRadius.all(Radius.circular(8)), |
|
boxShadow: [ |
|
BoxShadow( |
|
color: Colors.black.withAlpha(25), |
|
offset: Offset(0, 1), |
|
blurRadius: 12, |
|
spreadRadius: 0, |
|
) |
|
]), |
|
child: Row( |
|
mainAxisAlignment: MainAxisAlignment.start, |
|
crossAxisAlignment: CrossAxisAlignment.start, |
|
children: [ |
|
MImage( |
|
store.logo, |
|
width: 95.w, |
|
height: 95.h, |
|
fit: BoxFit.cover, |
|
errorSrc: "assets/image/default_1.png", |
|
fadeSrc: "assets/image/default_1.png", |
|
), |
|
SizedBox( |
|
width: 12.w, |
|
), |
|
Expanded( |
|
flex: 1, |
|
child: Container( |
|
height: 95.h, |
|
child: Column( |
|
mainAxisAlignment: MainAxisAlignment.spaceAround, |
|
crossAxisAlignment: CrossAxisAlignment.start, |
|
children: [ |
|
Row( |
|
mainAxisAlignment: MainAxisAlignment.spaceBetween, |
|
crossAxisAlignment: CrossAxisAlignment.center, |
|
children: [ |
|
Expanded( |
|
child: Text( |
|
store.storeName, |
|
overflow: TextOverflow.ellipsis, |
|
style: TextStyle( |
|
color: Colors.black, |
|
fontSize: 16.sp, |
|
fontWeight: FontWeight.bold, |
|
), |
|
), |
|
flex: 1, |
|
), |
|
Text( |
|
store.businessType, |
|
overflow: TextOverflow.ellipsis, |
|
textAlign: TextAlign.end, |
|
style: TextStyle( |
|
color: Color(0xFFEDB12F), |
|
fontSize: 14.sp, |
|
), |
|
), |
|
], |
|
), |
|
Row( |
|
mainAxisAlignment: MainAxisAlignment.spaceBetween, |
|
crossAxisAlignment: CrossAxisAlignment.center, |
|
children: [ |
|
Text( |
|
(store.distance ?? 0) > 1000 |
|
? S.of(context).gongli( |
|
((store.distance ?? 0) / 1000 * 100).toInt() / |
|
100.0) |
|
: S.of(context).mi( |
|
((store.distance ?? 0) * 100).toInt() / 100.0), |
|
style: TextStyle( |
|
color: Color(0xFF4C4C4C), |
|
fontSize: 12, |
|
), |
|
), |
|
SizedBox( |
|
width: 16.w, |
|
), |
|
Expanded( |
|
child: Container( |
|
alignment: Alignment.centerRight, |
|
child: Text( |
|
store.address, |
|
overflow: TextOverflow.ellipsis, |
|
style: TextStyle( |
|
color: Color(0xFF727272), |
|
fontSize: 12.sp, |
|
), |
|
), |
|
), |
|
) |
|
], |
|
), |
|
Expanded(child: Container(), flex: 1,), |
|
Container( |
|
child: Text( |
|
S.of(context).manlijiandaijinquan(50, 25), |
|
style: TextStyle( |
|
color: Color(0xFFFF7A1A), |
|
fontSize: 10.sp, |
|
), |
|
), |
|
), |
|
Row( |
|
mainAxisAlignment: MainAxisAlignment.spaceBetween, |
|
crossAxisAlignment: CrossAxisAlignment.center, |
|
children: [ |
|
Text( |
|
(store.openStartTime == null && |
|
store.openEndTime == null) |
|
? "全天" |
|
: "${store.openStartTime}-${store.openEndTime}", |
|
style: TextStyle( |
|
color: Color(0xFFA29E9E), |
|
fontSize: 12.sp, |
|
), |
|
), |
|
Text( |
|
S.of(context).ren( |
|
store == null ? "" : store.perCapitaConsumption), |
|
style: TextStyle( |
|
color: Color(0xFF353535), |
|
fontSize: 12.sp, |
|
), |
|
) |
|
], |
|
), |
|
], |
|
), |
|
), |
|
) |
|
], |
|
), |
|
); |
|
} |
|
|
|
@override |
|
bool get wantKeepAlive => true; |
|
}
|
|
|