You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 08:47:35