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.

513 lines
17 KiB

4 years ago
import 'dart:ui';
4 years ago
import 'package:amap_flutter_location/amap_flutter_location.dart';
import 'package:amap_flutter_location/amap_location_option.dart';
4 years ago
import 'package:dio/dio.dart';
4 years ago
import 'package:flutter/cupertino.dart';
4 years ago
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';
4 years ago
import 'package:huixiang/retrofit/data/base_data.dart';
import 'package:huixiang/retrofit/data/store.dart';
import 'package:huixiang/retrofit/retrofit_api.dart';
4 years ago
import 'package:huixiang/view_widget/classic_header.dart';
4 years ago
import 'package:huixiang/view_widget/custom_image.dart';
4 years ago
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';
4 years ago
import 'package:huixiang/view_widget/loading_view.dart';
import 'package:huixiang/view_widget/rotate_container.dart';
4 years ago
import 'package:permission_handler/permission_handler.dart';
4 years ago
import 'package:pull_to_refresh/pull_to_refresh.dart';
4 years ago
import 'package:shared_preferences/shared_preferences.dart';
4 years ago
import 'dart:async';
import 'dart:typed_data';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
4 years ago
class UnionPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _UnionPage();
}
}
4 years ago
class _UnionPage extends State<UnionPage> with AutomaticKeepAliveClientMixin {
4 years ago
//默认设置为不使用自定义地图,如果需要直接显示,在初始化是设置为true
CustomStyleOptions _customStyleOptions = CustomStyleOptions(false);
//加载自定义地图样式
4 years ago
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;
4 years ago
RefreshController refreshController =
RefreshController(initialRefresh: false);
4 years ago
@override
void dispose() {
super.dispose();
aMapFlutterLocation.stopLocation();
aMapFlutterLocation.destroy();
}
4 years ago
ApiService apiService;
4 years ago
@override
void initState() {
super.initState();
if (aMapFlutterLocation == null) {
AMapFlutterLocation.setApiKey("f39d1daa020a56f208eb2519f63e9534",
"feaae7986201b571cace1b83728be5bb");
aMapFlutterLocation = AMapFlutterLocation();
aMapFlutterLocation.onLocationChanged().listen((event) {
if (event != null &&
event["latitude"] != null &&
event["longitude"] != null) {
4 years ago
print("location: $event");
4 years ago
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"]);
}
4 years ago
saveLatLng(
latLng, event["province"], event["city"], event["district"]);
queryStore("${event["latitude"]}", "${event["longitude"]}",
event["province"], event["city"], event["district"], "");
4 years ago
if (_mapController != null)
4 years ago
_mapController.moveCamera(
CameraUpdate.newCameraPosition(CameraPosition(
target: latLng,
zoom: 15.0,
)),
);
4 years ago
}
});
}
aMapFlutterLocation.setLocationOption(AMapLocationOption(
4 years ago
needAddress: true,
4 years ago
onceLocation: true,
locationMode: AMapLocationMode.Hight_Accuracy,
pausesLocationUpdatesAutomatically: true,
));
_loadCustomData();
getLatLng();
startLocation();
}
4 years ago
LatLng latLng;
saveLatLng(LatLng latLng, province, city, district) async {
4 years ago
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString("latitude", "${latLng.latitude}");
await prefs.setString("longitude", "${latLng.longitude}");
4 years ago
await prefs.setString("province", province);
await prefs.setString("city", city);
await prefs.setString("district", district);
4 years ago
}
getLatLng() async {
SharedPreferences.getInstance().then((value) => {
4 years ago
apiService = ApiService(Dio(), token: value.getString('token')),
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"),
""),
4 years ago
setState(() {
if (_mapController != null) {
4 years ago
_mapController.moveCamera(
CameraUpdate.newCameraPosition(
CameraPosition(
target: latLng,
zoom: 15.0,
),
),
);
4 years ago
}
})
});
}
4 years ago
List<Store> storeList;
queryStore(latitude, longitude, province, city, district, searchKey) async {
BaseData baseData = await apiService.queryStore({
"city": city,
"district": district,
"latitude": latitude,
"longitude": longitude,
"province": province,
"searchKey": searchKey
4 years ago
}).catchError((error) {
refreshController.refreshFailed();
4 years ago
});
4 years ago
if(Navigator.canPop(context))
Navigator.of(context).pop();
4 years ago
if (baseData.isSuccess) {
storeList = (baseData.data as List<dynamic>)
.map((e) => Store.fromJson(e))
.toList();
4 years ago
buildMarker();
4 years ago
refreshController.refreshCompleted();
setState(() {});
} else {
refreshController.refreshFailed();
Fluttertoast.showToast(msg: baseData.msg);
}
}
4 years ago
RepaintBoundary repaintBoundary;
buildMarker() async {
markers.clear();
markers.addAll(storeList.map((element) => Marker(
position: LatLng(double.tryParse(element.latitude),
double.tryParse(element.longitude)),
anchor: Offset(0.5, 0.8),
icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueRose),
infoWindowEnable: true,
)));
setState(() {});
}
showLoadingDialog() {
showCupertinoDialog(
context: context,
barrierDismissible: true,
builder: (context) {
return LoadingView();
});
}
4 years ago
List<Marker> markers = [];
4 years ago
@override
Widget build(BuildContext context) {
AMapApiKey aMapApiKeys = AMapApiKey(
androidKey: 'f39d1daa020a56f208eb2519f63e9534',
iosKey: 'feaae7986201b571cace1b83728be5bb');
return Scaffold(
body: NestedScrollView(
physics: BouncingScrollPhysics(),
headerSliverBuilder: (context, inner) {
return [
4 years ago
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,
gestureRecognizers: <Factory<OneSequenceGestureRecognizer>>[
Factory<OneSequenceGestureRecognizer>(
() => EagerGestureRecognizer())
].toSet(),
)),
4 years ago
];
},
body: Container(
4 years ago
child: SmartRefresher(
controller: refreshController,
enablePullUp: false,
enablePullDown: true,
physics: BouncingScrollPhysics(),
4 years ago
header: MyHeader(),
4 years ago
onRefresh: () {
startLocation();
},
child: ListView.builder(
itemCount: storeList == null ? 0 : storeList.length,
padding: EdgeInsets.only(top: 8, bottom: 8),
itemBuilder: (context, position) {
return GestureDetector(
onTap: () {
4 years ago
Navigator.of(context).pushNamed(
'/router/union_detail_page',
4 years ago
arguments: {
"id":storeList[position].id
});
4 years ago
},
child: buildStoreItem(storeList[position], position),
);
}),
),
4 years ago
),
),
);
}
startLocation() async {
if (await Permission.locationWhenInUse.serviceStatus.isEnabled) {
bool isShown = await Permission.contacts.shouldShowRequestRationale;
if (isShown) {
Fluttertoast.showToast(
4 years ago
msg: "shouldShowRequestRationale",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0,
);
4 years ago
}
if (await Permission.location.isPermanentlyDenied) {
//openAppSettings
} else if (await Permission.location.isGranted) {
4 years ago
showLoadingDialog();
4 years ago
aMapFlutterLocation.startLocation();
} else {
await Permission.location.request();
startLocation();
}
} else {
//enabledLocation
}
}
AMapController _mapController;
4 years ago
TextEditingController editingController = TextEditingController();
4 years ago
void onMapCreated(AMapController controller) {
4 years ago
_mapController = controller;
4 years ago
}
Widget buildSliverAppBar(AMapWidget map) {
return SliverAppBar(
pinned: true,
floating: true,
snap: true,
backgroundColor: Color(0xFFFAFAFA),
elevation: 0,
automaticallyImplyLeading: false,
title: Container(
height: 36,
margin: EdgeInsets.fromLTRB(16, 8, 16, 8),
padding: EdgeInsets.fromLTRB(13, 6, 16, 6),
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: Row(
children: [
Icon(
Icons.search,
size: 24,
color: Colors.black,
),
Expanded(
child: TextField(
4 years ago
controller: editingController,
decoration: InputDecoration(border: InputBorder.none),
),
),
4 years ago
Icon(
Icons.close,
size: 19,
color: Colors.grey,
),
],
),
),
flexibleSpace: FlexibleSpaceBar(
background: Container(
child: map,
),
),
expandedHeight: 375,
bottom: PreferredSize(
preferredSize: Size(double.infinity, 50),
child: Container(
padding: EdgeInsets.only(top: 6),
color: Color(0xFFFAFAFA),
child: ItemTitle(
text: "净币联盟会员店",
imgPath: "assets/image/icon_union_store.png",
),
),
),
);
}
4 years ago
Widget buildStoreItem(Store store, position) {
4 years ago
return Container(
margin: EdgeInsets.fromLTRB(16, 8, 16, 8),
padding: EdgeInsets.fromLTRB(20, 20, 20, 20),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(8)),
boxShadow: [
BoxShadow(
4 years ago
color: Colors.black.withAlpha(25),
offset: Offset(0, 1),
blurRadius: 12,
spreadRadius: 0,
)
4 years ago
]),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
4 years ago
MImage(
4 years ago
store.logo,
4 years ago
width: 95,
height: 95,
fit: BoxFit.cover,
4 years ago
errorSrc: "assets/image/default_1.png",
fadeSrc: "assets/image/default_1.png",
4 years ago
),
SizedBox(
width: 12,
),
Expanded(
flex: 1,
child: Container(
height: 95,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
4 years ago
store.storeName,
4 years ago
style: TextStyle(
color: Colors.black,
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
4 years ago
Expanded(
child: Text(
store.posType == null ? "" : store.posType.desc,
overflow: TextOverflow.ellipsis,
textAlign: TextAlign.end,
style: TextStyle(
color: Color(0xFFEDB12F),
fontSize: 14,
),
4 years ago
),
4 years ago
flex: 1,
4 years ago
)
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
4 years ago
store.distance > 1000
? S.of(context).gongli(
(store.distance / 1000 * 100).toInt() / 100.0)
: S
.of(context)
.mi((store.distance * 100).toInt() / 100.0),
4 years ago
style: TextStyle(
color: Color(0xFF4C4C4C),
fontSize: 12,
),
),
SizedBox(
width: 16,
4 years ago
),
Expanded(
child: Container(
alignment: Alignment.centerRight,
child: Text(
4 years ago
store.address,
4 years ago
overflow: TextOverflow.ellipsis,
4 years ago
style: TextStyle(
4 years ago
color: Color(0xFF727272),
fontSize: 12,
),
4 years ago
),
),
)
],
),
Container(
margin: EdgeInsets.only(top: 16),
child: Text(
S.of(context).manlijiandaijinquan(50, 25),
4 years ago
style: TextStyle(
color: Color(0xFFFF7A1A),
fontSize: 10,
),
4 years ago
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
4 years ago
(store.openStartTime == null &&
store.openEndTime == null)
? "全天"
: "${store.openStartTime}-${store.openEndTime}",
4 years ago
style:
TextStyle(color: Color(0xFFA29E9E), fontSize: 12),
),
Text(
4 years ago
S.of(context).ren(store == null ? "" : store.perCapitaConsumption),
4 years ago
style:
TextStyle(color: Color(0xFF353535), fontSize: 12),
)
],
),
],
),
),
)
],
),
);
}
4 years ago
@override
bool get wantKeepAlive => true;
4 years ago
}