Flutter中如何为ExpansionPanelList的各面板之间添加自定义间距?
解决ExpansionPanelList面板间距问题的方案
原生ExpansionPanelList确实不直接支持面板间距配置,以下是两种可直接落地的实现方法:
方案1:使用ExpansionTile替代(最推荐,无需第三方依赖)
直接放弃ExpansionPanelList,改用ListView包裹单个ExpansionTile组件实现,每个组件可独立配置margin,完全符合你要的间距效果,修改后代码如下:
ListView.builder( shrinkWrap: true, physics: const NeverScrollableScrollPhysics(), // 如果父组件已经有滚动逻辑加这行 itemCount: orders.length, itemBuilder: (context, index) { OrderDetails order = orders[index]; return Container( margin: const EdgeInsets.symmetric(vertical: 8), // 这里调整面板上下间距 decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(8), boxShadow: [ BoxShadow( color: Colors.grey.withOpacity(0.1), blurRadius: 4, offset: const Offset(0, 2), ) ], ), child: ExpansionTile( onExpansionChanged: (isExpanded) { setState(() { orders[index].isExpanded = isExpanded; }); }, initiallyExpanded: order.isExpanded, tilePadding: EdgeInsets.zero, title: InkWell( onTap: () { Navigator.push( context, MaterialPageRoute( builder: (context) => TrackOrder( orderID: order.orderID, total: order.total, ))); }, child: Row( children: <Widget>[ Container( margin: const EdgeInsets.all(20), decoration: BoxDecoration( color: MyColors.PrimaryColor.withOpacity(0.2), borderRadius: BorderRadius.circular(50)), child: IconButton( onPressed: () {}, icon: Icon( CupertinoIcons.cube_box_fill, color: MyColors.PrimaryColor, size: 22, ), ), ), Column( crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[ Text( 'Order #${order.orderID}', style: const TextStyle( color: Colors.black, fontSize: 18, fontWeight: FontWeight.bold), ), const SizedBox( height: 4, ), Row( children: [ const Text( 'Total: ', style: TextStyle( color: Colors.black, fontSize: 13, ), ), Text( '₹${order.total}', style: const TextStyle( color: Colors.black, fontSize: 13, fontWeight: FontWeight.w800), ), ], ), ], ) ], ), ), children: [ Container( padding: const EdgeInsets.symmetric(horizontal: 15), width: MediaQuery.of(context).size.width, height: 100, decoration: const BoxDecoration( color: Colors.white, ), child: Row( mainAxisSize: MainAxisSize.max, mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Row( mainAxisSize: MainAxisSize.max, children: [ Image.network( order.image, width: 60, height: 60, fit: BoxFit.fill, ), const SizedBox(width: 15,), Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( order.productTitle, style: const TextStyle( fontFamily: 'Poppins', fontWeight: FontWeight.bold, ), ), Text( 'Price: ${order.price}', style: const TextStyle( fontFamily: 'Poppins', ), ), Text( 'Quantity: ${order.quantity}', style: const TextStyle( fontFamily: 'Poppins', ), ) ], ) ], ), Text( 'Status: ${order.type}', style: TextStyle( color: MyColors.PrimaryColor, ), ) ], ), ) ], ), ); }, )
方案2:改造原生ExpansionPanelList实现
如果必须使用ExpansionPanelList组件,可以通过修改分割线属性制造间距效果:
- 设置
dividerColor: Colors.transparent隐藏默认分割线 - 给ExpansionPanel的
headerBuilder返回的组件底部加固定padding,展开时动态取消padding即可
ExpansionPanelList( elevation: 1, dividerColor: Colors.transparent, // 隐藏默认分割线 expansionCallback: (int index, bool isExpanded) { setState(() { orders[index].isExpanded = !orders[index].isExpanded; }); }, children: orders.map((OrderDetails order) { return ExpansionPanel( headerBuilder: (context, isExpanded) { return Padding( // 未展开时底部加12dp间距,展开时取消 padding: EdgeInsets.only(bottom: isExpanded ? 0 : 12), child: // 原有headerBuilder返回的组件代码 ); }, isExpanded: order.isExpanded, body: // 原有body代码 ); }).toList(), )
内容的提问来源于stack exchange,提问作者Saya
相关产品推荐
相关产品推荐

