Flutter创建固定尺寸列组件:解决昵称过长布局不均问题
解决方案:固定Column尺寸并实现昵称文本省略
核心修改点
- 给每个
Column包裹固定宽度容器,强制统一水平尺寸 - 给昵称
Text添加省略号配置,处理过长文本
步骤1:固定Column宽度
用SizedBox包裹整个Column,设置合适的固定宽度(示例设为80,可根据UI需求调整),确保所有Column占用相同水平空间:
solverWidgets.add( // 新增:固定Column宽度 SizedBox( width: 80, child: Column( crossAxisAlignment: CrossAxisAlignment.center, children: [ // 原有内容... ], ), ), );
步骤2:实现昵称文本省略
修改昵称的Text组件,添加overflow、maxLines属性,确保过长文本自动显示为省略号:
Text( solverTicket[solver]['nickname'], style: weight600Style.copyWith( color: primaryColor, fontSize: 13, ), // 新增:省略号配置 overflow: TextOverflow.ellipsis, maxLines: 1, textAlign: TextAlign.center, // 保持文本居中对齐 ),
完整修改后的代码片段
solverWidgets.add( SizedBox( width: 80, // 固定宽度,统一所有Column尺寸 child: Column( crossAxisAlignment: CrossAxisAlignment.center, children: [ BuildRoundedRectProfile( height: 40, width: 40, name: solverTicket[solver]['nickname'], profileLink: solverTicket[solver]['profile_picture_link'] ?? '', ), SizedBox(height: 3), Text( solverTicket[solver]['nickname'], style: weight600Style.copyWith( color: primaryColor, fontSize: 13, ), overflow: TextOverflow.ellipsis, // 过长文本显示省略号 maxLines: 1, // 限制只显示一行 textAlign: TextAlign.center, ), if (solversStatus[solver] != null && (povStatus == 'pic' || povStatus == 'pic-client')) GestureDetector( onTap: () { if (solversStatus[solver] == 'Done') { setState(() { needToLoadSolverWidgets = true; isNotExpandedSolverWidgets[solver] = !isNotExpandedSolverWidgets[solver]; }); if (isExpandedSolverWidgetsContent[solver]) { Future.delayed(Duration(milliseconds: 300), () { setState(() { needToLoadSolverWidgets = true; isExpandedSolverWidgetsContent[solver] = false; }); }); } else { setState(() { needToLoadSolverWidgets = true; isExpandedSolverWidgetsContent[solver] = true; }); } } }, child: AnimatedContainer( duration: Duration(milliseconds: 300), height: isNotExpandedSolverWidgets[solver] ? 20 : 55, padding: EdgeInsets.only(top: 5, bottom: 5, left: 5, right: 5), decoration: BoxDecoration( color: defaultProfileColor.withOpacity(0.2), borderRadius: BorderRadius.circular(10), ), child: solversStatus[solver] != 'Done' ? Text( solversStatus[solver], style: primaryColor600Style.copyWith( fontSize: fontSize9, ), ).tr() : isNotExpandedSolverWidgets[solver] ? Row( crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.center, children: [ Text( solversStatus[solver], style: primaryColor600Style.copyWith( fontSize: fontSize10, ), ).tr(), Icon( Icons.arrow_drop_down, size: fontSize15, ), ], ) : ClipRRect( child: Column( crossAxisAlignment: CrossAxisAlignment.end, children: [ Row( children: [ Text( solversStatus[solver], style: primaryColor600Style .copyWith( fontSize: fontSize9, ), ).tr(), Icon( Icons.arrow_drop_down, size: fontSize18, ), ], ), GestureDetector( onTap: () { showDialog( context: context, builder: (BuildContext context) => ReusableConfirmationDialog( titleText: 'changeWorkStatus'.tr(), contentText: 'rejectWorkStatus'.tr(), confirmButtonText: 'yes'.tr(), onConfirm: () async { await Database.changeWorkStatus( ticketId: widget.ticketId, ticketData: ticketData, targetId: solver as String, oldWorkStatus: 'done', workStatus: 'todo'); Navigator.pop(context); setState(() { isNotExpandedSolverWidgets[solver] = true; needToLoadTicket = true; }); })); }, child: Container( padding: EdgeInsets.all(5), decoration: BoxDecoration( color: warningColor, borderRadius: BorderRadius.circular(5), ), child: Text( 'disagree', style: primaryColor600Style .copyWith( fontSize: fontSize9, color: Colors.white, ), ).tr(), ), ) ]), ), ), ), if(solversStatus[solver] != null && (povStatus != 'pic' && povStatus != 'pic-client')) Container( padding: EdgeInsets.all(5), decoration: BoxDecoration( color: formBackgroundColor, borderRadius: BorderRadius.all( Radius.circular(10))), child: Text( solversStatus[solver], style: weight600Style.copyWith( color: primaryColor, fontSize: 9, ), ), ), ], ), ), );
额外提示
- 若Row总宽度有限,可改用动态宽度计算(比如
MediaQuery.of(context).size.width * 0.3)适配不同屏幕 - 状态文本如果也存在过长情况,可给对应
Text组件添加相同的省略号配置
内容的提问来源于stack exchange,提问作者Kim San
相关产品推荐
相关产品推荐

