参数类型“颜色?”不能分配给参数类型“颜色”

时间:2021-05-27 06:41:00

标签: flutter dart flutter-layout

class _CreateRoomButton extends StatelessWidget {
 @override
 Widget build(BuildContext context) {
   // ignore: deprecated_member_use
  return OutlineButton(
    onPressed: () => print('Create Room'),
     color: Colors.white,
  borderSide: BorderSide(width: 3.0, color: Colors.blueAccent[100]),
  textColor: Palette.facebookBlue,
  child: Row(
    children: [
      Icon(
        Icons.video_call,
        size: 35.0,
        color: Colors.white,
      )
    ],
  ),
  shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(30.0)),
);
}
}

我使用了一个轮廓按钮,我想在其中将边框颜色设置为 blueAccent[100]。尝试这样做时出现以下错误:

参数类型“颜色?”不能分配给参数类型“颜色”。

我还想将大纲按钮移到新的大纲按钮中,但我无法根据应用设置样式。

2 个答案:

答案 0 :(得分:0)

我认为您已经更新了 flutter sdk 并使用了空安全功能。只需添加!色后。

color: Colors.blueAccent[100]!,
<块引用>

注意:不推荐使用大纲按钮,而使用大纲按钮。

Flutter Outlinedbutton

答案 1 :(得分:0)

如果您看到 Colors.blueAccent[100] 实际上从地图中获取了一个值。因此,如果您的 flutter 版本低于 2.0,您将收到此错误,因为它可能返回空值。

现在为什么会发生这种情况:这是因为如果您使用的是默认情况下为空安全的 flutter 2.2。你会得到很多错误。

解决方案:Colors.blueAccent[100]!Colors.blueAccent.shade100;

对于轮廓按钮:

  OutlinedButton.icon(
    label: Text('MY BUTTON'),
    icon: Icon(Icons.video_call),
    onPressed: () {
      print('Pressed');
    },
    style: OutlinedButton.styleFrom(
    primary: Colors.white,
    backgroundColor: Colors.teal,
    shape: const RoundedRectangleBorder(borderRadius: 
     BorderRadius.all(Radius.circular(30))),
),
  )

您可以访问更多属性:From here 或者您可以查看此 Material Guidelines 或此博客 This Blog