适用于iOS和Android的Flutter插件,用于实现顶部菜单跟页面联合滚动效果。
在pubspec.yaml文件中添加link_scroll_menu为依赖项。
GlobalKey<CCMenuPageState> menuState = GlobalKey<CCMenuPageState>();
List<Color> colorList = [
Color(0xFF969AF9),
Color(0xFF53CFA1),
Color(0xFFFF9E59),
Color(0xFFFF99CB),
Color(0xFF80D0FF),
];
List<String> menuList = ['关注','推荐','热榜','抗疫','视频','小视频','直播','娱乐','新闻'];
return CCMenuPage(
menuList: menuList,
itemBuilder: (BuildContext context,int index){
return Container(
alignment: Alignment.center,
width: double.infinity,
height: 200,
color: colorList[index % colorList.length],
child: Text('${menuList[index]}',style: TextStyle(fontSize: 20),),
);
},
);
return CCMenuPage.custom(
menuList: menuList,
tabHeight: 60,
itemBuilder: (BuildContext context,int index){
return Container(
alignment: Alignment.center,
width: double.infinity,
height: 200,
color: colorList[index % colorList.length],
child: Text('${menuList[index]}',style: TextStyle(fontSize: 20),),
);
},
);
return CCMenuPage.builder(
itemCount: menuList.length,
tabBuilder: (BuildContext context,int index){
return Container(
alignment: Alignment.center,
width: 60,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
width: 30,
height: 30,
margin: EdgeInsets.only(
bottom: 10,
top: 10
),
color: Colors.amber,
),
Text(
menuList[index],style: TextStyle(fontSize: 18),
)
],
),
);
},
itemBuilder: (BuildContext context,int index){
return Container(
alignment: Alignment.center,
width: double.infinity,
height: 200,
color: colorList[index % colorList.length],
child: Text('${menuList[index]}',style: TextStyle(fontSize: 20),),
);
},
);
return CCMenuPage.custom(
menuList: menuList,
tabHeight: 60,
headerWidget: Container(
alignment: Alignment.center,
height: 200,
color: Colors.black38,
child: Text('这是头部',style: TextStyle(fontSize: 20),),
),
bottomWidget: Container(
alignment: Alignment.center,
height: 200,
color: Colors.black38,
child: Text('这是底部',style: TextStyle(fontSize: 20),),
),
itemBuilder: (BuildContext context,int index){
return Container(
alignment: Alignment.center,
width: double.infinity,
height: 200,
color: colorList[index % colorList.length],
child: Text('${menuList[index]}',style: TextStyle(fontSize: 20),),
);
},
);
GlobalKey<CCMenuPageState> menuState = GlobalKey<CCMenuPageState>();
return CCMenuPage.custom(
key: menuState,
menuList: menuList,
tabHeight: 60,
itemBuilder: (BuildContext context,int index){
return Container(
alignment: Alignment.center,
width: double.infinity,
height: 200,
color: colorList[index % colorList.length],
child: Text('${menuList[index]}',style: TextStyle(fontSize: 20),),
);
},
);
点击添加按钮菜单:
GestureDetector(
onTap: (){
menuList.add('新增菜单');
menuState.currentState.updateMenu();
setState(() {
});
},
child: Container(
width: 50,
child: Icon(Icons.add,color: Colors.white,),
),
)