Flutter中设置Padding时如何移除ExpansionTile展开后的分割线?
解决Flutter中带Padding的ExpansionTile移除展开后分割线的问题
嘿,我懂你的需求:给ExpansionTile加了外层Padding,但想去掉它展开后出现的那条分割线对吧?其实操作很简单,咱们直接针对ExpansionTile本身的属性调整就行,完全不用动你原来的Padding设置。
ExpansionTile默认在展开状态下,标题下方会显示一条分割线,咱们只需要给它设置dividerColor为透明色,就能把这条线彻底隐藏掉,同时外层的Padding效果完全不受影响。
下面是修改后的完整代码:
return Card( borderOnForeground: false, key: Key("${e.id}"), child: Padding( padding: EdgeInsets.only(bottom: 8.0), child: ExpansionTile( // 添加这一行,将分割线设为透明 dividerColor: Colors.transparent, backgroundColor: Theme.of(context).accentColor, title: Text( e.title, style: Theme.of(context).textTheme.bodyText1, maxLines: 1, ), subtitle: Text(e.by), children: <Widget>[ Row( mainAxisAlignment: MainAxisAlignment.spaceAround, children: <Widget>[ Text("${e.descendants} comments"), Icon(Icons.open_in_new) ], ) ], ), ), );
如果你的项目里有多个ExpansionTile都需要去掉分割线,也可以用Theme组件包裹来全局修改,这样就不用每个组件单独设置了:
return Card( borderOnForeground: false, key: Key("${e.id}"), child: Padding( padding: EdgeInsets.only(bottom: 8.0), child: Theme( data: Theme.of(context).copyWith( dividerColor: Colors.transparent, ), child: ExpansionTile( backgroundColor: Theme.of(context).accentColor, title: Text( e.title, style: Theme.of(context).textTheme.bodyText1, maxLines: 1, ), subtitle: Text(e.by), children: <Widget>[ Row( mainAxisAlignment: MainAxisAlignment.spaceAround, children: <Widget>[ Text("${e.descendants} comments"), Icon(Icons.open_in_new) ], ) ], ), ), ), );
这两种方法都能完美保留你原来的Padding布局,同时移除展开后的分割线,选哪种看你的实际场景需求就好~
内容的提问来源于stack exchange,提问作者Hoang Trung Nguyen
相关产品推荐
相关产品推荐

