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.
170 lines
5.0 KiB
170 lines
5.0 KiB
import 'package:flutter/material.dart'; |
|
import 'package:flutter/rendering.dart'; |
|
import 'package:flutter_screenutil/flutter_screenutil.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 bool isScroll; |
|
|
|
StoreTitleTab(this.brandText, |
|
{this.scrollController, |
|
this.chiliGlobalKey, |
|
this.milkTeaGlobalKey, |
|
this.breadGlobalKey, this.isScroll = false}); |
|
|
|
@override |
|
State<StatefulWidget> createState() { |
|
return _StoreTitleTab(); |
|
} |
|
} |
|
|
|
class _StoreTitleTab extends State<StoreTitleTab> { |
|
int selectedIndex = -1; |
|
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) { |
|
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); |
|
|
|
var top = 96.h; |
|
if (chiliOffset.dy <= top) { |
|
if (!isVisible) { |
|
isVisible = true; |
|
setState(() {}); |
|
} |
|
} else { |
|
var b = isVisible; |
|
isVisible = false; |
|
if (b) setState(() {}); |
|
selectedIndex = -1; |
|
} |
|
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; |
|
} |
|
} |
|
}); |
|
} |
|
} |
|
|
|
@override |
|
Widget build(BuildContext context) { |
|
return Visibility( |
|
visible: (!widget.isScroll) ? true : isVisible, |
|
child: Container( |
|
width: MediaQuery.of(context).size.width, |
|
child: Container( |
|
height: 52.h, |
|
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) |
|
? [] |
|
: (widget.brandText.map((e) { |
|
return item(e, selectedIndex == widget.brandText.indexOf(e), |
|
widget.brandText.indexOf(e)); |
|
}).toList()), |
|
), |
|
), |
|
), |
|
); |
|
} |
|
|
|
Widget item(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 |
|
.findRenderObject() |
|
.parentData; |
|
} |
|
double offset = parendData.offset.dy - 52.h + 20.h; |
|
widget.scrollController.animateTo(offset, |
|
duration: Duration(seconds: 1), curve: Curves.ease); |
|
}, |
|
child: tabItem(text, isSelected, index), |
|
); |
|
} |
|
|
|
Widget tabItem(text, isSelected, index) { |
|
if (isSelected) { |
|
return IconText( |
|
text, |
|
isMax: false, |
|
rightImage: images[index], |
|
iconSize: 16, |
|
iconColor: Colors.red, |
|
textStyle: TextStyle( |
|
fontWeight: FontWeight.bold, |
|
fontSize: 16.sp, |
|
color: Color(0xFF353535)), |
|
); |
|
} else { |
|
return IconText( |
|
text, |
|
isMax: false, |
|
textStyle: TextStyle( |
|
fontWeight: FontWeight.bold, |
|
fontSize: 16.sp, |
|
color: Color(0xFF353535)), |
|
); |
|
} |
|
} |
|
}
|
|
|