【问题标题】:The argument type 'Color?' can't be assigned to the parameter type 'Color'参数类型“颜色?”不能分配给参数类型“颜色”
【发布时间】:2021-08-15 09:45:15
【问题描述】:
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]。尝试这样做时会出现以下错误:

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

我还想将大纲按钮转换为新的大纲按钮,但我无法根据应用设置样式。

【问题讨论】:

  • Colors.blueAccent[100]! 试试这个!

标签: flutter dart flutter-layout


【解决方案1】:

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

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

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

对于 OutlinedButton:

  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

【讨论】:

    【解决方案2】:

    您已经更新了我认为的颤振 sdk 并使用了 null 安全功能。只需添加!上色后。

    color: Colors.blueAccent[100]!,
    

    注意:Outlinebutton 已弃用,请改用 outlinebutton。

    Flutter Outlinedbutton

    【讨论】:

    • 它是一个 null 安全特性变量不能包含 null 除非你说它们可以.. dart.dev/….
    猜你喜欢
    • 2021-06-05
    • 2022-01-16
    • 2021-12-14
    • 2020-11-29
    • 2021-08-27
    • 2021-06-16
    • 2021-08-18
    • 2023-04-09
    • 2022-07-20
    相关资源
    最近更新 更多