Flutter求助:实现Slidable图标在Container上方滑动不偏移容器
解决方案
要实现滑动列表项时删除图标显示在Container上方、且Container不向左偏移的效果,只需调整Slidable的滑动动效及布局配置,修改后的代码如下:
ListView.builder( itemCount: _prefsManager.campaings.length + 1, itemBuilder: (context, index) { if (index == _prefsManager.campaings.length) { return ListTile( title: IconButton( icon: Icon( Icons.add, color: Colors.deepPurple, ), onPressed: () { _AddPrize(); }, ), ); } else { return Slidable( key: Key(_prefsManager.campaings[index]), direction: Axis.horizontal, startActionPane: ActionPane( // 改用DrawerMotion,滑动时child固定不动,action面板从侧边滑出覆盖在上方 motion: const DrawerMotion(), // 控制action面板的宽度比例,按需调整 extentRatio: 0.2, children: [ SlidableAction( flex: 2, onPressed: (BuildContext context) { _showConfirmationDialog(context, () { setState(() { campaings.removeAt(index); _prefsManager.campaings = campaings; _prefsManager.saveCampaings(); }); }); }, backgroundColor: Colors.red, icon: Icons.delete, label: 'Delete', ) ], ), child: Container( // 去掉固定宽度,让Container占满列表项宽度,确保action面板能完整覆盖 width: double.infinity, height: 50.h, decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(8.r), ), child: GestureDetector( onTap: () { Navigator.of(context).push(_createRoute(index)); }, child: Center( child: Text( _prefsManager.campaings[index], style: TextStyle( color: Colors.black, fontSize: 13.sp, ), ), ), ), ), ); } }, )
关键修改说明:
- 替换滑动动效:将
StretchMotion改为DrawerMotion,这是核心改动——DrawerMotion的逻辑是让操作面板从侧边滑出并覆盖在内容组件上方,而非带动内容组件偏移。 - 调整Container宽度:移除固定的
285.w宽度,改为double.infinity让容器占满列表项宽度,保证操作面板能正确覆盖在容器上方。 - 控制操作面板宽度:通过
extentRatio参数设置操作面板占列表项宽度的比例,按需调整数值即可适配需求。
内容的提问来源于stack exchange,提问作者BaselAr
相关产品推荐
相关产品推荐

