|
|
|
@ -1,23 +1,22 @@
|
|
|
|
|
import 'package:flutter/material.dart'; |
|
|
|
|
import 'package:flutter/rendering.dart'; |
|
|
|
|
import 'package:flutter_screenutil/flutter_screenutil.dart'; |
|
|
|
|
import 'package:huixiang/retrofit/data/brand.dart'; |
|
|
|
|
import 'package:huixiang/view_widget/icon_text.dart'; |
|
|
|
|
|
|
|
|
|
class StoreTitleTab extends StatefulWidget { |
|
|
|
|
final ScrollController scrollController; |
|
|
|
|
final List<String> brandText; |
|
|
|
|
|
|
|
|
|
final GlobalKey chiliGlobalKey; |
|
|
|
|
final GlobalKey milkTeaGlobalKey; |
|
|
|
|
final GlobalKey breadGlobalKey; |
|
|
|
|
final List<Brand> brands; |
|
|
|
|
|
|
|
|
|
final List<GlobalKey> globaKeys; |
|
|
|
|
final bool isScroll; |
|
|
|
|
|
|
|
|
|
StoreTitleTab(this.brandText, |
|
|
|
|
{this.scrollController, |
|
|
|
|
this.chiliGlobalKey, |
|
|
|
|
this.milkTeaGlobalKey, |
|
|
|
|
this.breadGlobalKey, this.isScroll = false}); |
|
|
|
|
StoreTitleTab(this.brands, |
|
|
|
|
this.globaKeys, |
|
|
|
|
this.scrollController, |
|
|
|
|
{ |
|
|
|
|
this.isScroll = false, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
State<StatefulWidget> createState() { |
|
|
|
@ -30,28 +29,20 @@ class _StoreTitleTab extends State<StoreTitleTab> {
|
|
|
|
|
int selectedIndex1 = -1; |
|
|
|
|
bool isVisible = false; |
|
|
|
|
|
|
|
|
|
List<String> images = [ |
|
|
|
|
"assets/image/icon_chili.webp", |
|
|
|
|
"assets/image/icon_milk_tea.webp", |
|
|
|
|
"assets/image/icon_bread.webp", |
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
void initState() { |
|
|
|
|
super.initState(); |
|
|
|
|
|
|
|
|
|
if (widget.scrollController != null) { |
|
|
|
|
print("firstOffset: ${widget.globaKeys}"); |
|
|
|
|
print("firstOffset: ${widget.brands}"); |
|
|
|
|
if (widget.scrollController != null && widget.globaKeys != null && widget.globaKeys.length > 0) { |
|
|
|
|
widget.scrollController?.addListener(() { |
|
|
|
|
RenderBox chiliRenderBox = widget.chiliGlobalKey.currentContext.findRenderObject(); |
|
|
|
|
RenderBox milkTeaRenderBox = widget.milkTeaGlobalKey.currentContext.findRenderObject(); |
|
|
|
|
RenderBox breadRenderBox = widget.breadGlobalKey.currentContext.findRenderObject(); |
|
|
|
|
|
|
|
|
|
Offset chiliOffset = chiliRenderBox?.localToGlobal(Offset.zero); |
|
|
|
|
Offset milkTeaOffset = milkTeaRenderBox?.localToGlobal(Offset.zero); |
|
|
|
|
Offset breadOffset = breadRenderBox?.localToGlobal(Offset.zero); |
|
|
|
|
|
|
|
|
|
if (widget.globaKeys[0].currentContext == null) return; |
|
|
|
|
RenderBox firstRenderBox = widget.globaKeys[0].currentContext.findRenderObject(); |
|
|
|
|
Offset first = firstRenderBox?.localToGlobal(Offset.zero); |
|
|
|
|
print("firstOffset: ${first.dy}"); |
|
|
|
|
var top = 96.h; |
|
|
|
|
if (chiliOffset.dy <= top) { |
|
|
|
|
if (first.dy <= top) { |
|
|
|
|
if (!isVisible) { |
|
|
|
|
isVisible = true; |
|
|
|
|
setState(() {}); |
|
|
|
@ -62,32 +53,66 @@ class _StoreTitleTab extends State<StoreTitleTab> {
|
|
|
|
|
if (b) setState(() {}); |
|
|
|
|
selectedIndex = -1; |
|
|
|
|
} |
|
|
|
|
if (chiliOffset.dy <= top && breadOffset.dy > top) { |
|
|
|
|
selectedIndex = 0; |
|
|
|
|
|
|
|
|
|
widget.globaKeys.forEach((element) { |
|
|
|
|
if (widget.globaKeys.indexOf(element) < (widget.globaKeys.length - 1)) { |
|
|
|
|
if (element.currentContext == null) return; |
|
|
|
|
RenderBox renderBox = element.currentContext.findRenderObject(); |
|
|
|
|
Offset offset = renderBox?.localToGlobal(Offset.zero); |
|
|
|
|
RenderBox nextRenderBox = element.currentContext.findRenderObject(); |
|
|
|
|
Offset nextOffset = nextRenderBox?.localToGlobal(Offset.zero); |
|
|
|
|
if (offset.dy <= top && nextOffset.dy > top) { |
|
|
|
|
selectedIndex = widget.globaKeys.indexOf(element); |
|
|
|
|
if (selectedIndex1 != selectedIndex) { |
|
|
|
|
setState(() { |
|
|
|
|
print("object:$selectedIndex"); |
|
|
|
|
}); |
|
|
|
|
selectedIndex1 = selectedIndex; |
|
|
|
|
} |
|
|
|
|
} else if (breadOffset.dy <= top && milkTeaOffset.dy > top) { |
|
|
|
|
selectedIndex = 1; |
|
|
|
|
if (selectedIndex1 != selectedIndex) { |
|
|
|
|
setState(() { |
|
|
|
|
print("object:$selectedIndex"); |
|
|
|
|
}); |
|
|
|
|
selectedIndex1 = selectedIndex; |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
} else if (milkTeaOffset.dy <= top) { |
|
|
|
|
selectedIndex = 2; |
|
|
|
|
} 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"); |
|
|
|
|
}); |
|
|
|
|
selectedIndex1 = selectedIndex; |
|
|
|
|
} |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// if (chiliOffset.dy <= top && breadOffset.dy > top) { |
|
|
|
|
// selectedIndex = 0; |
|
|
|
|
// if (selectedIndex1 != selectedIndex) { |
|
|
|
|
// setState(() { |
|
|
|
|
// print("object:$selectedIndex"); |
|
|
|
|
// }); |
|
|
|
|
// selectedIndex1 = selectedIndex; |
|
|
|
|
// } |
|
|
|
|
// } else if (breadOffset.dy <= top && milkTeaOffset.dy > top) { |
|
|
|
|
// selectedIndex = 1; |
|
|
|
|
// if (selectedIndex1 != selectedIndex) { |
|
|
|
|
// setState(() { |
|
|
|
|
// print("object:$selectedIndex"); |
|
|
|
|
// }); |
|
|
|
|
// selectedIndex1 = selectedIndex; |
|
|
|
|
// } |
|
|
|
|
// } else if (milkTeaOffset.dy <= top) { |
|
|
|
|
// selectedIndex = 2; |
|
|
|
|
// if (selectedIndex1 != selectedIndex) { |
|
|
|
|
// setState(() { |
|
|
|
|
// print("object:$selectedIndex"); |
|
|
|
|
// }); |
|
|
|
|
// selectedIndex1 = selectedIndex; |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -95,21 +120,23 @@ class _StoreTitleTab extends State<StoreTitleTab> {
|
|
|
|
|
Widget build(BuildContext context) { |
|
|
|
|
return Visibility( |
|
|
|
|
visible: (!widget.isScroll) ? true : isVisible, |
|
|
|
|
child: Container( |
|
|
|
|
width: MediaQuery.of(context).size.width, |
|
|
|
|
child: SingleChildScrollView( |
|
|
|
|
physics: BouncingScrollPhysics(), |
|
|
|
|
scrollDirection: Axis.horizontal, |
|
|
|
|
child: Container( |
|
|
|
|
height: 52.h, |
|
|
|
|
constraints: BoxConstraints(minWidth: MediaQuery.of(context).size.width), |
|
|
|
|
color: Colors.white, |
|
|
|
|
padding: EdgeInsets.all(6), |
|
|
|
|
alignment: Alignment.center, |
|
|
|
|
child: Row( |
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceAround, |
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center, |
|
|
|
|
children: (widget.brandText == null || widget.brandText.isEmpty) |
|
|
|
|
children: (widget.brands == null || widget.brands.length == 0) |
|
|
|
|
? [] |
|
|
|
|
: (widget.brandText.map((e) { |
|
|
|
|
return item(e, selectedIndex == widget.brandText.indexOf(e), |
|
|
|
|
widget.brandText.indexOf(e)); |
|
|
|
|
: (widget.brands.map((e) { |
|
|
|
|
return item(e, selectedIndex == widget.brands.indexOf(e), |
|
|
|
|
widget.brands.indexOf(e)); |
|
|
|
|
}).toList()), |
|
|
|
|
), |
|
|
|
|
), |
|
|
|
@ -117,53 +144,53 @@ class _StoreTitleTab extends State<StoreTitleTab> {
|
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Widget item(text, isSelected, index) { |
|
|
|
|
Widget item(Brand text, isSelected, index) { |
|
|
|
|
print("selectedIndex: $selectedIndex"); |
|
|
|
|
return GestureDetector( |
|
|
|
|
onTap: () { |
|
|
|
|
FlexParentData parendData; |
|
|
|
|
if (index == 0) { |
|
|
|
|
parendData = widget.chiliGlobalKey.currentContext |
|
|
|
|
.findRenderObject() |
|
|
|
|
.parentData; |
|
|
|
|
} else if (index == 2) { |
|
|
|
|
parendData = widget.milkTeaGlobalKey.currentContext |
|
|
|
|
.findRenderObject() |
|
|
|
|
.parentData; |
|
|
|
|
} else if (index == 1) { |
|
|
|
|
parendData = widget.breadGlobalKey.currentContext |
|
|
|
|
FlexParentData parendData = widget.globaKeys[index].currentContext |
|
|
|
|
.findRenderObject() |
|
|
|
|
.parentData; |
|
|
|
|
} |
|
|
|
|
double offset = parendData.offset.dy - 52.h + 20.h; |
|
|
|
|
widget.scrollController.animateTo(offset, |
|
|
|
|
duration: Duration(seconds: 1), curve: Curves.ease); |
|
|
|
|
}, |
|
|
|
|
child: Container( |
|
|
|
|
padding: EdgeInsets.symmetric(horizontal: 15.w), |
|
|
|
|
child: tabItem(text, isSelected, index), |
|
|
|
|
), |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Widget tabItem(text, isSelected, index) { |
|
|
|
|
// List<String> images = [ |
|
|
|
|
// "assets/image/icon_chili.webp", |
|
|
|
|
// "assets/image/icon_milk_tea.webp", |
|
|
|
|
// "assets/image/icon_bread.webp", |
|
|
|
|
// ]; |
|
|
|
|
|
|
|
|
|
Widget tabItem(Brand text, isSelected, index) { |
|
|
|
|
if (isSelected) { |
|
|
|
|
return IconText( |
|
|
|
|
text, |
|
|
|
|
text.name, |
|
|
|
|
isMax: false, |
|
|
|
|
rightImage: images[index], |
|
|
|
|
rightImage: text.icon ?? "assets/image/icon_chili.webp", |
|
|
|
|
iconSize: 16, |
|
|
|
|
iconColor: Colors.red, |
|
|
|
|
textStyle: TextStyle( |
|
|
|
|
fontWeight: FontWeight.bold, |
|
|
|
|
fontSize: 16.sp, |
|
|
|
|
color: Color(0xFF353535)), |
|
|
|
|
color: Color(0xFF353535), |
|
|
|
|
), |
|
|
|
|
); |
|
|
|
|
} else { |
|
|
|
|
return IconText( |
|
|
|
|
text, |
|
|
|
|
text.name, |
|
|
|
|
isMax: false, |
|
|
|
|
textStyle: TextStyle( |
|
|
|
|
fontWeight: FontWeight.bold, |
|
|
|
|
fontSize: 16.sp, |
|
|
|
|
color: Color(0xFF353535)), |
|
|
|
|
color: Color(0xFF353535), |
|
|
|
|
), |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|