[英]How do I add background color to unselected tabs in flutter tabbar?
设置 Scaffold backgroundColor
类似于backgroundColor: Colors.grey.shade100,
并使用 TabController
class _TabBarDemoState extends State<TabBarDemo>
with SingleTickerProviderStateMixin {
late TabController controller = TabController(length: 10, vsync: this)
..addListener(() {
setState(() {});
});
和
TabBar(
isScrollable: true,
controller: controller,
tabs: List.generate(
10,
(index) => AnimatedContainer(
duration: Duration(milliseconds: 100),
width: 34,//do as you like
height: 34,//do as you like
alignment: Alignment.center,
decoration: ShapeDecoration(
color: controller.index == index
? Colors.deepPurple
: Colors.white,
shape: CircleBorder(),
),
child: Text(
"$index",
style: controller.index == index
? TextStyle()
: TextStyle(color: Colors.black),
),
),
)),
ListView.separated(
itemCount: 10,
scrollDirection: Axis.horizontal,
shrinkWrap: true,
separatorBuilder: (BuildContext context, int index) => const SizedBox(width: 5,),
itemBuilder: (context, index) {
return GestureDetector(
onTap: () {
setState(() {
isSelected = index;
});
},
child: Container(
height: 40,
width: 40,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(25),
color: isSelected == index ? Colors.pink : Colors.white
),
child: Padding(
padding: const EdgeInsets.all(10),
child: Center(child: Text("${index.toString()}", style: TextStyle(color: isSelected == index ? Colors.white : Colors.black),)),
),
),
);
}
),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.