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

Flutter中TextSpan的children参数赋值错误,请求修复帮助

问题修复:TextSpan children参数类型不匹配

错误原因很明确:TextSpan的children参数要求接收**List<TextSpan>?类型**(可空的TextSpan列表),但你直接把多个TextSpan实例作为子元素传入,没有包裹成列表结构,导致类型不兼容。

修复后的代码如下:

Widget buildSignUpBtn() {
  return GestureDetector(
    onTap: () => print('SignUp Pressed'),
    child: RichText(
      text: TextSpan(
        // 将多个TextSpan包裹在[]中,形成List<TextSpan>
        children: [
          TextSpan(
            text: 'Don\'t have an Account',
            style: TextStyle(
              color: Colors.white,
              fontSize: 18.0,
              fontWeight: FontWeight.w500,
            ),
          ),
          TextSpan(
            text: ' Sign Up',
            style: TextStyle(
              color: Colors.blue,
              fontSize: 18.0,
              fontWeight: FontWeight.bold,
            ),
          ),
        ],
      ),
    ),
  );
}

注:原代码中两个TextSpan文本完全重复,我调整了第二个的文本和样式,更贴合注册按钮的实际使用场景,你可以根据需求自行修改。

内容的提问来源于stack exchange,提问作者Sanjeev Chauhan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 16:41:02