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.

329 lines
11 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_utils/flutter_baidu_mapapi_utils.dart';
import 'package:flutter_bmflocation/flutter_bmflocation.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';
import 'package:huixiang/retrofit/data/ip_data.dart';
4 years ago
import 'package:huixiang/retrofit/retrofit_api.dart';
3 years ago
import 'package:huixiang/union/union_list.dart';
4 years ago
import 'package:huixiang/utils/event_type.dart';
3 years ago
import 'package:huixiang/utils/location.dart';
3 years ago
import 'package:huixiang/view_widget/my_tab.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
import '../view_widget/no_data_view.dart';
4 years ago
class UnionPage extends StatefulWidget {
3 years ago
final int initialIndex;
UnionPage(Key key, this.initialIndex) : super(key: key);
3 years ago
4 years ago
@override
State<StatefulWidget> createState() {
3 years ago
return UnionPageState();
4 years ago
}
}
3 years ago
class UnionPageState extends State<UnionPage>
with
AutomaticKeepAliveClientMixin,
WidgetsBindingObserver,
SingleTickerProviderStateMixin {
final TextEditingController editingController = TextEditingController();
3 years ago
bool isKeyBoardShow = false;
TabController tabController;
3 years ago
BMFCoordinate latLng;
String areaName;
List<GlobalKey> _allKey = [];
4 years ago
jumpIndex(jpIndex) {
3 years ago
tabController.index = jpIndex;
}
3 years ago
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();
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();
tabController = TabController(
length: 4, vsync: this, initialIndex: widget.initialIndex);
4 years ago
WidgetsBinding.instance.addObserver(this);
3 years ago
eventBus.on<EventType>().listen((event) {
if (event.type < 3) {
setState(() {});
}
});
queryIpInfo();
3 years ago
}
startLocation() async {
Location.getInstance().startLocation(context, (BaiduLocation result) async {
if (result != null &&
result.latitude != null &&
result.longitude != null) {
print("location: $result");
latLng = BMFCoordinate(result.latitude, result.longitude);
latLng = await BMFCalculateUtils.coordConvert(
coordinate: latLng,
fromType: BMF_COORD_TYPE.BD09LL,
toType: BMF_COORD_TYPE.COMMON);
await saveLatLng(latLng, result.province, result.city, result.district);
print("union: Location result ${latLng.latitude} "
"${latLng.longitude}");
Location.getInstance().stopLocation();
} else {
await getLatLng();
3 years ago
}
loadFinish(showLoading: false);
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 {
var tempLatLng = await SharedPreferences.getInstance();
if (tempLatLng.containsKey("latitude") &&
tempLatLng.containsKey("longitude") &&
tempLatLng.containsKey("province") &&
tempLatLng.containsKey("city") &&
tempLatLng.containsKey("district")) {
latLng = BMFCoordinate(double.tryParse(tempLatLng.getString("latitude")),
double.tryParse(tempLatLng.getString("longitude")));
}
4 years ago
}
queryIpInfo() async {
EasyLoading.show(status: S.current.zhengzaijiazai);
ApiService apiIpService = ApiService(Dio(), context: context, isIp: true);
IpData baseData = await apiIpService.getIpInfo().catchError((onError) {});
if (baseData != null) {
areaName = baseData.city.replaceAll("", "");
3 years ago
}
startLocation();
4 years ago
}
3 years ago
loadFinish({bool showLoading = true}) {
if (showLoading) EasyLoading.show(status: S.current.zhengzaijiazai);
_allKey = [GlobalKey(), GlobalKey(), GlobalKey(), GlobalKey()];
setState(() {});
}
4 years ago
4 years ago
@override
Widget build(BuildContext context) {
4 years ago
super.build(context);
3 years ago
return GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
FocusScope.of(context).requestFocus(FocusNode());
},
child: Container(
child: Column(
children: [
Row(
children: [
Padding(
padding: EdgeInsets.only(left: 18.w, right: 10.w),
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
Navigator.of(context).pushNamed(
'/router/union_select_city',
arguments: {"cityName": areaName}).then((value) {
if (value != null) {
areaName = value;
loadFinish();
}
});
},
child: Row(
children: [
Text(
areaName ?? "",
overflow: TextOverflow.ellipsis,
maxLines: 1,
style: TextStyle(
fontSize: 14.sp,
fontWeight: FontWeight.bold,
color: Colors.white),
),
Icon(
Icons.keyboard_arrow_down,
color: Colors.white,
size: 24,
)
],
),
),
),
Expanded(child: buildSearchItem())
],
),
Align(
alignment: Alignment.centerLeft,
child: TabBar(
controller: tabController,
isScrollable: true,
//可滚动
indicatorColor: Colors.white,
labelColor: Colors.white,
labelStyle: TextStyle(
fontSize: 18.sp,
fontWeight: FontWeight.bold,
),
unselectedLabelStyle: TextStyle(
fontSize: 15.sp,
fontWeight: FontWeight.normal,
),
//未选中文字颜色
unselectedLabelColor: Colors.white,
indicatorSize: TabBarIndicatorSize.label,
//指示器与文字等宽
tabs: <Widget>[
MyTab(text: S.of(context).quanbu),
MyTab(text: S.of(context).chi),
MyTab(text: S.of(context).he),
MyTab(text: S.of(context).wan),
],
),
),
_allKey.isEmpty
? NoDataView(
src: "assets/image/di_zhi.webp",
isShowBtn: false,
text: "暂无店铺列表~",
fontSize: 16.sp,
margin: EdgeInsets.only(top: 120.h),
)
: Expanded(
child: TabBarView(
controller: tabController,
children: [
UnionList(_allKey[0], "", latLng,
editingController.text, areaName),
UnionList(_allKey[1], "EATSTORE", latLng,
editingController.text, areaName),
UnionList(_allKey[2], "DRINKSTORE", latLng,
editingController.text, areaName),
UnionList(_allKey[3], "HAPPYSTORE", latLng,
editingController.text, areaName),
],
),
)
],
),
padding:
EdgeInsets.only(top: MediaQuery.of(context).padding.top + 17.h),
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Color(0xFF32A060),
Color(0xFF32A060),
Colors.white,
Colors.white,
],
stops: [
0,
0.2,
0.4,
1
]),
),
),
4 years ago
);
}
Widget buildSearchItem() {
return Container(
height: 36.h,
margin: EdgeInsets.fromLTRB(6.w, 0, 14.w, 0),
decoration: BoxDecoration(
color: Color(0xFFF5FAF7),
borderRadius: BorderRadius.circular(4),
boxShadow: [
BoxShadow(
color: Colors.black.withAlpha(12),
offset: Offset(0, 3),
blurRadius: 14,
spreadRadius: 0,
),
],
),
child: TextField(
textInputAction: TextInputAction.search,
onEditingComplete: () {
FocusScope.of(context).requestFocus(FocusNode());
loadFinish();
},
controller: editingController,
cursorHeight: 25.h,
decoration: InputDecoration(
contentPadding: EdgeInsets.symmetric(
vertical: 14.h,
),
hintText: "搜索联盟会员店",
hintStyle: TextStyle(
fontSize: 12.sp,
color: Color(0xFFB3B3B3),
),
prefixIcon: GestureDetector(
onTap: loadFinish,
child: Image.asset(
"assets/image/icon_search.webp",
width: 16,
height: 16,
),
),
border: InputBorder.none,
),
),
);
}
4 years ago
4 years ago
@override
bool get wantKeepAlive => true;
4 years ago
}