Flutter ';TextStyle?&x27;can';不能分配给参数类型';文本样式';
我在使用颤振应用程序时遇到此空安全错误Flutter ';TextStyle?&x27;can';不能分配给参数类型';文本样式';,flutter,dart,Flutter,Dart,我在使用颤振应用程序时遇到此空安全错误 The argument type 'TextStyle?' can't be assigned to the parameter type 'TextStyle'. 以下是引发此错误的代码段: ButtonStyle(textStyle:MaterialStateProperty.all(Theme.of(context.textTheme.button)) VS代码建议将not运算符放在参数的末尾 ButtonStyle(textStyle: Ma
The argument type 'TextStyle?' can't be assigned to the parameter type 'TextStyle'.
以下是引发此错误的代码段:
ButtonStyle(textStyle:MaterialStateProperty.all(Theme.of(context.textTheme.button))
VS代码建议将not运算符放在参数的末尾
ButtonStyle(textStyle: MaterialStateProperty.all<TextStyle>(Theme.of(context).textTheme.button!))
ButtonStyle(textStyle:MaterialStateProperty.all(Theme.of(context.textTheme.button!))
问题:这是使用的好方法吗在我的代码中,以及在这种情况下还有哪些其他解决方案?在Buildcontext中初始化样式,例如:
var _styleType = Theme.of(context)
.textTheme
.body1
.copyWith(color: Colors.white);
然后将其应用于小部件:
Container(
margin: EdgeInsets.symmetric(vertical: 10),
child: new Text("\u00a9 TasnuvaOshin",
overflow: TextOverflow.ellipsis,
style: _styleType),
),
材质状态属性表示依赖于小部件材质“状态”的值。状态编码为一组MaterialState值,如MaterialState.focused、MaterialState.hovered、MaterialState.pressed。例如,InkWell.OverlyColor定义了按下墨迹、聚焦或悬停时填充墨迹的颜色(“飞溅颜色”)。墨水池使用覆盖颜色的“解析”方法计算墨水池当前状态的颜色 您只能使用上下文的主题来使用如下主题文本样式:
Text("copied text theme",
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.button)
因此,我不应该将样式放在按钮上,而应该将其添加到文本中?因此,我应该在文本小部件中而不是按钮小部件中删除样式?您可以在MaterialTheme中定义您的样式,无论我如何按照您的代码片段使用按钮文本样式来设置讨论中的文本小部件的样式。:)实际上,我想在按钮内设置文本样式。所以使用materialstateproperty是一个错误的选择?是的,您可以简单地使用TextStyle()或Theme.of(context)textTheme。