如何移除Flutter TabBar点击或切换时的Ripple(波纹)效果
移除Flutter TabBar点击/切换时的波纹效果
要彻底消除TabBar的点击波纹,你需要补充设置TabBar的overlayColor属性,同时保留已有的splashFactory配置,具体修改如下:
修改后的完整代码
import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; void main() => runApp( MaterialApp( title: "App", home: Sample(), ), ); class Sample extends StatefulWidget { @override _StackOverState createState() => _StackOverState(); } class _StackOverState extends State<Sample> with SingleTickerProviderStateMixin { late TabController _tabController; @override void initState() { _tabController = TabController(length: 2, vsync: this); super.initState(); } @override void dispose() { _tabController.dispose(); super.dispose(); } final yourTheme = ThemeData.light(); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Tab bar'), ), body: Theme( data: yourTheme.copyWith( highlightColor: Colors.transparent, splashColor: Colors.transparent, splashFactory: NoSplash.splashFactory, ), child: Column( children: [ Container( width: MediaQuery.of(context).size.width / 2, height: 45, decoration: BoxDecoration( color: Colors.blue, borderRadius: BorderRadius.circular(25.0), ), child: Container( color: Colors.transparent, child: TabBar( splashFactory: NoSplash.splashFactory, // 新增:移除点击反馈的overlay颜色 overlayColor: MaterialStateProperty.all(Colors.transparent), labelPadding: EdgeInsets.zero, controller: _tabController, indicator: BoxDecoration( borderRadius: BorderRadius.circular(25.0), color: Colors.green, ), indicatorColor: Colors.transparent, dividerColor: Colors.transparent, labelColor: Colors.white, unselectedLabelColor: Colors.black, tabs: [ Tab( child: Align( alignment: Alignment.center, child: Text("Place Bid"), ), ), Tab( child: Align( alignment: Alignment.center, child: Text("buy now"), ), ), ], ), ), ), Expanded( child: TabBarView( controller: _tabController, children: [ Center( child: Text( 'Place Bid', style: TextStyle( fontSize: 25, fontWeight: FontWeight.w600, ), ), ), Center( child: Text( 'Buy Now', style: TextStyle( fontSize: 25, fontWeight: FontWeight.w600, ), ), ), ], ), ), ], ), ), ); } }
关键修改说明
- 添加
overlayColor属性:Flutter 2.0及以上版本中,组件的点击反馈(包括波纹)主要由overlayColor控制,设置为透明后会直接移除点击时的波纹效果。 - 保留
splashFactory: NoSplash.splashFactory:确保彻底禁用所有类型的波纹渲染逻辑。 - 精简冗余代码:移除了Tab中不必要的Container嵌套,保持代码简洁。
内容的提问来源于stack exchange,提问作者Erfan
相关产品推荐
相关产品推荐

