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
相关产品推荐
相关产品推荐

