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.

209 lines
6.6 KiB

4 years ago
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
3 years ago
import 'package:huixiang/retrofit/data/brand.dart';
4 years ago
import 'package:huixiang/view_widget/icon_text.dart';
class StoreTitleTab extends StatefulWidget {
final ScrollController scrollController;
3 years ago
final List<Brand> brands;
4 years ago
3 years ago
final List<GlobalKey> globaKeys;
4 years ago
final bool isScroll;
3 years ago
StoreTitleTab(
this.brands,
this.globaKeys,
this.scrollController, {
this.isScroll = false,
});
4 years ago
@override
State<StatefulWidget> createState() {
return _StoreTitleTab();
}
}
class _StoreTitleTab extends State<StoreTitleTab> {
int selectedIndex = -1;
int selectedIndex1 = -1;
bool isVisible = false;
3 years ago
List<GlobalKey> _globalKeys = [];
4 years ago
@override
void initState() {
super.initState();
3 years ago
if (widget.brands != null) {
widget.brands.forEach((element) {
_globalKeys.add(GlobalKey());
});
}
if (widget.scrollController != null &&
widget.globaKeys != null &&
widget.globaKeys.length > 0) {
widget.scrollController?.addListener(() {
3 years ago
if (widget.globaKeys[0].currentContext == null) return;
3 years ago
RenderBox firstRenderBox =
widget.globaKeys[0].currentContext.findRenderObject();
3 years ago
Offset first = firstRenderBox?.localToGlobal(Offset.zero);
3 years ago
4 years ago
var top = 96.h;
3 years ago
if (first.dy <= top) {
4 years ago
if (!isVisible) {
isVisible = true;
setState(() {});
}
} else {
var b = isVisible;
isVisible = false;
if (b) setState(() {});
selectedIndex = -1;
}
3 years ago
widget.globaKeys.forEach((element) {
3 years ago
if (widget.globaKeys.indexOf(element) <
(widget.globaKeys.length - 1)) {
3 years ago
if (element.currentContext == null) return;
RenderBox renderBox = element.currentContext.findRenderObject();
Offset offset = renderBox?.localToGlobal(Offset.zero);
3 years ago
int nextIndex = widget.globaKeys.indexOf(element) + 1;
RenderBox nextRenderBox =
widget.globaKeys[nextIndex].currentContext.findRenderObject();
3 years ago
Offset nextOffset = nextRenderBox?.localToGlobal(Offset.zero);
3 years ago
3 years ago
if (offset.dy <= top && nextOffset.dy > top) {
selectedIndex = widget.globaKeys.indexOf(element);
if (selectedIndex1 != selectedIndex) {
setState(() {
print("object:$selectedIndex");
3 years ago
if (!isClickScroll) {
3 years ago
scrollTab(selectedIndex);
} else if (selectedIndex == clickIndex) {
3 years ago
isClickScroll = false;
3 years ago
clickIndex = -1;
}
3 years ago
});
selectedIndex1 = selectedIndex;
}
return;
}
} else {
RenderBox lastRenderBox = element.currentContext.findRenderObject();
Offset lastOffset = lastRenderBox?.localToGlobal(Offset.zero);
if (lastOffset.dy <= top) {
selectedIndex = widget.globaKeys.indexOf(element);
if (selectedIndex1 != selectedIndex) {
setState(() {
print("object:$selectedIndex");
3 years ago
if (!isClickScroll) {
3 years ago
scrollTab(selectedIndex);
} else if (selectedIndex == clickIndex) {
3 years ago
isClickScroll = false;
3 years ago
clickIndex = -1;
}
3 years ago
});
selectedIndex1 = selectedIndex;
}
return;
}
4 years ago
}
3 years ago
});
4 years ago
});
}
}
3 years ago
scrollTab(index) {
3 years ago
if (_globalKeys[index].currentContext == null) return;
3 years ago
RenderBox renderBox = _globalKeys[index].currentContext.findRenderObject();
Offset nextOffset = renderBox?.localToGlobal(Offset.zero);
double widgetWidth = renderBox.size.width;
double screenWidth = MediaQuery.of(context).size.width;
if (nextOffset.dx < 0 || (nextOffset.dx + widgetWidth) > screenWidth) {
scrollController.animateTo(
3 years ago
nextOffset.dx/* < 0 ? nextOffset.dx : (nextOffset.dx + widgetWidth)*/,
3 years ago
duration: Duration(seconds: 1), curve: Curves.ease,
);
3 years ago
}
}
ScrollController scrollController = ScrollController();
4 years ago
@override
Widget build(BuildContext context) {
return Visibility(
4 years ago
visible: (!widget.isScroll) ? true : isVisible,
3 years ago
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
3 years ago
controller: scrollController,
3 years ago
physics: BouncingScrollPhysics(),
4 years ago
child: Container(
height: 52.h,
3 years ago
constraints: BoxConstraints(minWidth: MediaQuery.of(context).size.width),
4 years ago
color: Colors.white,
alignment: Alignment.center,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.center,
3 years ago
children: (widget.brands == null || widget.brands.length == 0)
4 years ago
? []
3 years ago
: (widget.brands.map((e) {
return item(e, selectedIndex == widget.brands.indexOf(e),
widget.brands.indexOf(e));
4 years ago
}).toList()),
),
),
),
);
}
3 years ago
bool isClickScroll = false;
3 years ago
int clickIndex = -1;
3 years ago
Widget item(Brand text, isSelected, index) {
4 years ago
return GestureDetector(
onTap: () {
3 years ago
FlexParentData parentData = widget.globaKeys[index].currentContext.findRenderObject().parentData;
3 years ago
double offset = parentData.offset.dy - 52.h + 20.h;
3 years ago
isClickScroll = true;
3 years ago
clickIndex = index;
3 years ago
widget.scrollController.animateTo(offset, duration: Duration(seconds: 1), curve: Curves.ease);
4 years ago
},
3 years ago
child: Container(
3 years ago
key: _globalKeys.length > 0 ? _globalKeys[index] : GlobalKey(),
3 years ago
padding: EdgeInsets.symmetric(horizontal: 15.w),
3 years ago
child: tabItem(text, isSelected),
3 years ago
),
4 years ago
);
}
3 years ago
Widget tabItem(Brand text, isSelected) {
4 years ago
if (isSelected) {
return IconText(
3 years ago
text.name,
4 years ago
isMax: false,
3 years ago
rightImage: text.icon ?? "assets/image/icon_xuanzhong.png",
4 years ago
iconSize: 16,
iconColor: Colors.red,
textStyle: TextStyle(
3 years ago
fontWeight: FontWeight.bold,
fontSize: 16.sp,
color: Color(0xFF353535),
),
4 years ago
);
} else {
return IconText(
3 years ago
text.name,
4 years ago
isMax: false,
textStyle: TextStyle(
3 years ago
fontWeight: FontWeight.bold,
fontSize: 16.sp,
color: Color(0xFF353535),
),
4 years ago
);
}
}
}