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.

604 lines
20 KiB

4 years ago
import 'dart:ui';
import 'package:dio/dio.dart';
4 years ago
import 'package:flutter/cupertino.dart';
4 years ago
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
3 years ago
import 'package:flutter_baidu_mapapi_map/flutter_baidu_mapapi_map.dart';
import 'package:flutter_baidu_mapapi_utils/flutter_baidu_mapapi_utils.dart';
3 years ago
import 'package:flutter_easyloading/flutter_easyloading.dart';
4 years ago
import 'package:huixiang/generated/l10n.dart';
4 years ago
import 'package:huixiang/main.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/utils/event_type.dart';
3 years ago
import 'package:huixiang/utils/location.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/icon_text.dart';
4 years ago
import 'package:huixiang/view_widget/item_title.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 'package:flutter/rendering.dart';
4 years ago
import 'package:flutter_screenutil/flutter_screenutil.dart';
3 years ago
import 'package:flutter_baidu_mapapi_base/flutter_baidu_mapapi_base.dart';
4 years ago
4 years ago
class UnionPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _UnionPage();
}
}
4 years ago
class _UnionPage extends State<UnionPage>
with AutomaticKeepAliveClientMixin, WidgetsBindingObserver {
3 years ago
final RefreshController refreshController =
RefreshController(initialRefresh: false);
3 years ago
ApiService apiService;
bool isKeyBoardShow = false;
BMFCoordinate latLng;
4 years ago
@override
void dispose() {
super.dispose();
4 years ago
WidgetsBinding.instance.removeObserver(this);
3 years ago
if (Location.getInstance() != null &&
Location.getInstance().aMapFlutterLocation != null)
3 years ago
Location.getInstance().aMapFlutterLocation.stopLocation();
3 years ago
if (refreshController != null) refreshController.dispose();
4 years ago
}
4 years ago
@override
void didChangeMetrics() {
super.didChangeMetrics();
WidgetsBinding.instance.addPostFrameCallback((_) {
setState(() {
4 years ago
print("object: ${MediaQuery.of(context).viewInsets.bottom}");
4 years ago
if (MediaQuery.of(context).viewInsets.bottom == 0) {
if (isKeyBoardShow) {
isKeyBoardShow = false;
4 years ago
//关闭键盘 软键盘关闭了, 清除输入控件的焦点, 否则重新进入页面会导致软键盘再弹出问题
FocusScope.of(context).requestFocus(FocusNode());
}
} else {
isKeyBoardShow = true;
4 years ago
}
});
});
}
4 years ago
@override
void initState() {
super.initState();
4 years ago
WidgetsBinding.instance.addObserver(this);
3 years ago
eventBus.on<EventType>().listen((event) {
if (event.type < 3) {
setState(() {});
}
});
getLatLng();
startLocation();
}
startLocation() async {
EasyLoading.show(status: S.current.zhengzaijiazai);
3 years ago
Location.getInstance()
.aMapFlutterLocation
.onResultCallback()
.listen((event) {
3 years ago
if (event != null &&
event["latitude"] != null &&
event["longitude"] != null) {
print("location: $event");
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"]);
}
3 years ago
BMFCalculateUtils.coordConvert(
3 years ago
coordinate: latLng,
fromType: BMF_COORD_TYPE.BD09LL,
toType: BMF_COORD_TYPE.COMMON)
3 years ago
.then((value) {
this.latLng = value;
saveLatLng(
value, event["province"], event["city"], event["district"]);
3 years ago
print("union: Location result ${value.latitude} "
"${value.longitude}");
3 years ago
Location.getInstance().stopLocation();
3 years ago
queryStore(
"${value.latitude}",
"${value.longitude}",
event["province"],
event["city"],
event["district"],
editingController.text);
if (_mapController != null)
_mapController.updateMapOptions(BMFMapOptions(
center: value,
zoomLevel: 15,
));
});
3 years ago
} else {
EasyLoading.dismiss();
3 years ago
}
});
4 years ago
3 years ago
Location.getInstance().prepareLoc();
Location.getInstance().startLocation(context).then((value) {
3 years ago
if (!value) {
EasyLoading.dismiss();
3 years ago
refreshController.refreshFailed();
3 years ago
}
3 years ago
});
4 years ago
}
3 years ago
saveLatLng(BMFCoordinate 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}");
3 years ago
if (province != null) await prefs.setString("province", province);
if (city != null) await prefs.setString("city", city);
if (district != null) await prefs.setString("district", district);
4 years ago
}
getLatLng() async {
4 years ago
SharedPreferences.getInstance().then(
(value) => {
3 years ago
apiService = ApiService(Dio(),
context: context,
token: value.getString('token'),
showLoading: false),
4 years ago
if (value.containsKey("latitude") &&
value.containsKey("longitude") &&
value.containsKey("province") &&
value.containsKey("city") &&
3 years ago
value.containsKey("district"))
{
3 years ago
latLng = BMFCoordinate(double.tryParse(value.getString("latitude")),
4 years ago
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) {
3 years ago
_mapController.updateMapOptions(BMFMapOptions(
center: latLng,
zoomLevel: 15,
));
4 years ago
}
})
}
else
{
queryStore("", "", "", "", "", editingController.text),
4 years ago
}
},
);
4 years ago
}
4 years ago
List<Store> storeList;
queryStore(latitude, longitude, province, city, district, searchKey) async {
3 years ago
BaseData<List<Store>> baseData = await apiService.queryStore({
4 years ago
// "city": city,
// "district": district,
// "province": province,
4 years ago
"latitude": latitude,
"longitude": longitude,
"searchKey": searchKey
4 years ago
}).catchError((error) {
refreshController.refreshFailed();
4 years ago
});
4 years ago
if (baseData != null && baseData.isSuccess) {
3 years ago
storeList = baseData.data;
4 years ago
refreshController.refreshCompleted();
} else {
refreshController.refreshFailed();
}
3 years ago
EasyLoading.dismiss();
setState(() {});
4 years ago
}
4 years ago
@override
Widget build(BuildContext context) {
4 years ago
super.build(context);
4 years ago
return GestureDetector(
4 years ago
onTap: () {
4 years ago
FocusScope.of(context).requestFocus(FocusNode());
},
child: Scaffold(
4 years ago
resizeToAvoidBottomInset: false,
appBar: PreferredSize(
preferredSize: Size(double.infinity, 75.h),
child: buildSearchItem(),
),
body: Column(
children: [
PreferredSize(
preferredSize: Size(double.infinity, 52.h),
child: Container(
3 years ago
padding: EdgeInsets.only(top: 20.h),
4 years ago
color: Color(0xFFFAFAFA),
child: ItemTitle(
text: S.of(context).jingbilianmenghuiyuandian,
imgPath: "assets/image/icon_union_store.png",
),
),
),
buildItem(),
],
),
),
);
}
Widget buildItem() {
return Container(
height: MediaQuery.of(context).size.height -
4 years ago
103.h -
4 years ago
MediaQuery.of(context).padding.top,
child: SmartRefresher(
controller: refreshController,
enablePullUp: false,
4 years ago
enablePullDown: true,
physics: BouncingScrollPhysics(),
header: MyHeader(),
onRefresh: () {
3 years ago
startLocation();
4 years ago
},
child: ListView.builder(
itemCount: storeList == null ? 0 : storeList.length,
// padding: EdgeInsets.only(top: 8.h, bottom: 84.h + (375.h - 88.h) + 4.h),
padding: EdgeInsets.only(
3 years ago
top: 8.h, bottom: 84.h /* + (375.h - 88.h) + 4.h*/),
4 years ago
physics: NeverScrollableScrollPhysics(),
itemBuilder: (context, position) {
return GestureDetector(
onTap: () {
3 years ago
// Navigator.of(context).pushNamed(
// '/router/union_detail_page',
// arguments: {
// "id": storeList[position].id,
// "storeName": storeList[position].storeName
// });
3 years ago
Navigator.of(context).pushNamed(
3 years ago
'/router/store_order',
arguments: {
"id": storeList[position].id,
3 years ago
"tenant": storeList[position].tenantCode,
3 years ago
"storeName": storeList[position].storeName
},
);
4 years ago
},
child: buildStoreItem(storeList[position], position),
);
}),
4 years ago
),
);
}
3 years ago
BMFMapController _mapController;
3 years ago
final TextEditingController editingController = TextEditingController();
4 years ago
3 years ago
void onMapCreated(BMFMapController controller) {
4 years ago
_mapController = controller;
4 years ago
}
4 years ago
Widget buildSearchItem() {
return Container(
height: 36.h,
margin: EdgeInsets.fromLTRB(16.w, 40, 16.w, 0),
padding: EdgeInsets.fromLTRB(0, 6.h, 0, 6.h),
decoration: BoxDecoration(
3 years ago
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(4)),
boxShadow: [
BoxShadow(
color: Colors.black.withAlpha(12),
offset: Offset(0, 3),
blurRadius: 14,
spreadRadius: 0,
3 years ago
),
3 years ago
],
),
4 years ago
child: TextField(
textInputAction: TextInputAction.search,
onEditingComplete: () {
3 years ago
startLocation();
4 years ago
},
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,
),
),
);
}
3 years ago
// Widget buildSliverAppBar(BMFMapWidget 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",
// ),
// ),
// ),
// );
// }
4 years ago
4 years ago
Widget buildStoreItem(Store store, position) {
4 years ago
return Container(
3 years ago
margin: EdgeInsets.fromLTRB(16.w, 8.h, 16.w, 8.h),
4 years ago
padding: EdgeInsets.fromLTRB(20.w, 20.h, 20.w, 20.h),
4 years ago
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,
3 years ago
width: 100,
height: 100,
4 years ago
fit: BoxFit.cover,
4 years ago
errorSrc: "assets/image/default_1.png",
fadeSrc: "assets/image/default_1.png",
4 years ago
),
SizedBox(
3 years ago
width: 8.w,
4 years ago
),
Expanded(
flex: 1,
child: Container(
3 years ago
height: 100.h,
4 years ago
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
4 years ago
crossAxisAlignment: CrossAxisAlignment.end,
4 years ago
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
4 years ago
Expanded(
child: Text(
4 years ago
store.storeName,
4 years ago
overflow: TextOverflow.ellipsis,
style: TextStyle(
4 years ago
color: Colors.black,
4 years ago
fontSize: 14.sp,
4 years ago
fontWeight: FontWeight.bold,
4 years ago
),
4 years ago
),
4 years ago
flex: 1,
4 years ago
),
Text(
4 years ago
store.businessType,
4 years ago
overflow: TextOverflow.ellipsis,
textAlign: TextAlign.end,
style: TextStyle(
color: Color(0xFFEDB12F),
4 years ago
fontWeight: FontWeight.w400,
4 years ago
fontSize: 14.sp,
),
),
4 years ago
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(
child: Container(
child: Text(
4 years ago
store.couponVO == null
? ""
: store.couponVO.bizType == 1
? S.of(context).manlijiandaijinquan(
double.tryParse(
store.couponVO.fullAmount)
.toInt(),
double.tryParse(
store.couponVO.discountAmount)
.toInt())
: S.of(context).quanchangzhe(
store.couponVO.discountPercent),
4 years ago
style: TextStyle(
4 years ago
color: Color(0xFFFF7A1A),
4 years ago
fontWeight: FontWeight.w500,
fontSize: 12.sp,
4 years ago
),
4 years ago
),
),
4 years ago
),
Text(
S.of(context).ren(
store == null ? "" : store.perCapitaConsumption),
style: TextStyle(
color: Color(0xFF353535),
4 years ago
fontWeight: FontWeight.w400,
4 years ago
fontSize: 12.sp,
),
),
4 years ago
],
),
3 years ago
SizedBox(
height: 4.h,
),
3 years ago
Row(
4 years ago
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
3 years ago
Container(
3 years ago
padding: EdgeInsets.only(top: 3.h, left: 2, right: 2),
3 years ago
child: Image.asset(
"assets/image/icon_union_location_black.png",
3 years ago
width: 14,
height: 14,
3 years ago
),
4 years ago
),
3 years ago
Expanded(
flex: 1,
3 years ago
child: Container(
padding: EdgeInsets.only(top: 2.h),
child: Text(
store.address,
overflow: TextOverflow.ellipsis,
maxLines: 2,
style: TextStyle(
color: Color(0xFF727272),
fontWeight: FontWeight.w400,
fontSize: 12.sp,
// height: 1.3.h,
),
3 years ago
),
4 years ago
),
3 years ago
),
4 years ago
],
4 years ago
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
4 years ago
IconText(
3 years ago
(store.openStartTime == null &&
store.openEndTime == null)
4 years ago
? S.of(context).quantian
3 years ago
: "${store.openStartTime.substring(0, store.openStartTime.lastIndexOf(":"))} - ${store.openEndTime.substring(0, store.openEndTime.lastIndexOf(":"))}",
4 years ago
textStyle: TextStyle(
4 years ago
color: Color(0xFF727272),
4 years ago
fontSize: 12.sp,
),
4 years ago
leftImage: "assets/image/icon_union_time.png",
3 years ago
iconSize: 14.w,
4 years ago
),
4 years ago
Visibility(
child: Text(
(store.distance ?? 0) > 1000
3 years ago
? S.of(context).gongli(
((store.distance ?? 0) / 1000 * 100).toInt() /
100.0)
: S.of(context).mi(
((store.distance ?? 0) * 100).toInt() /
100.0),
4 years ago
style: TextStyle(
color: Color(0xFF4C4C4C),
3 years ago
fontSize: 12.sp,
4 years ago
),
4 years ago
),
4 years ago
visible: store.distance != null,
4 years ago
),
4 years ago
],
),
],
),
),
)
],
),
);
}
4 years ago
@override
bool get wantKeepAlive => true;
4 years ago
}