Flutter中如何在Get.defaultDialog的中间文本使用TextSpan
要实现弹窗中间文本里的用户名加粗,你需要换掉middleText,改用Get.defaultDialog的content参数来自定义富文本,具体用RichText搭配TextSpan就能实现样式区分。
修改后的完整代码如下:
void getDeleteConfirmationDialog(){ Get.defaultDialog( contentPadding: EdgeInsets.all(8), title: 'Delete User', // 替换middleText为content,用RichText实现富文本 content: RichText( textAlign: TextAlign.center, // 保持原middleText的居中对齐效果 text: TextSpan( style: TextStyle(color: Colors.black87), // 设置默认文本样式,和原middleText一致 children: [ const TextSpan(text: 'Are you sure to Delete record of '), TextSpan( text: users[selectedIndex.value].name, style: const TextStyle(fontWeight: FontWeight.bold), // 用户名加粗 ), const TextSpan(text: '?'), ], ), ), confirm: ElevatedButton(onPressed: ()async{ deleteRecord(); Navigator.of(Get.overlayContext!).pop(); }, child: const Text('Sure, Delete')), cancel: OutlinedButton(onPressed: (){ Navigator.of(Get.overlayContext!).pop(); }, child: const Text('Cancel')), ); }
关键说明:
- Get.defaultDialog的
middleText只能接收普通字符串,没办法做样式区分,所以改用content参数传入自定义Widget。 RichText用来承载多样式文本,TextSpan的children数组可以组合不同样式的文本片段。- 给普通文本设置默认样式,给用户名的
TextSpan单独加上fontWeight: FontWeight.bold实现加粗效果。 - 加上
textAlign: TextAlign.center,让文本保持原middleText的居中对齐效果,和弹窗布局更协调。
内容的提问来源于stack exchange,提问作者user22362195
相关产品推荐
相关产品推荐

