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 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 createState() { return _StoreTitleTab(); } } class _StoreTitleTab extends State { int selectedIndex = -1; int selectedIndex1 = -1; bool isVisible = false; List 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, color: Color(0xFF353535)), ); } else { return IconText( text, isMax: false, textStyle: TextStyle( fontWeight: FontWeight.bold, fontSize: 16, color: Color(0xFF353535)), ); } } }