【问题标题】:Error: no named parameter with the name 'color' Flutter错误:没有名为 \'color\' 的命名参数 Flutter
【发布时间】:2023-01-16 19:35:18
【问题描述】:

所以我是 Dart & Flutter 的新手,遇到了一个问题。我正在尝试学习布局并对文本和按钮小部件进行细微的 UI 更改。在这里,我试图将 ElevatedButton 的颜色更改为 blue

import 'package:flutter/material.dart';

class Answer extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      child: ElevatedButton(
        color: Colors.blue,
        child: Text('Answer 1'),
        onPressed: null,
      ),
    );
  }
} 

当我运行代码时出现此错误:

Error: no named parameter with the name 'color'

我认为使用按钮可以更改颜色参数。实现这个的正确方法是什么?

【问题讨论】:

    标签: flutter dart visual-studio-code


    【解决方案1】:

    您可以使用 styleFrom 设置 ElevatedButton 的样式

    ElevatedButton(
          child: const Text('Button'),
          onPressed: () {},
          style: ElevatedButton.styleFrom(
              primary: Colors.purple,
        ),
    

    或者你可以使用 ButtonStyle 类

    ElevatedButton(
          child: const Text('Button'),
          onPressed: () {},
          style: ButtonStyle(
              backgroundColor: MaterialStateProperty.all(Colors.red),
        ),
    

    【讨论】:

      【解决方案2】:
      ElevatedButton(
        style: ElevatedButton.styleFrom({
          Color primary, // set the background color 
          Color onPrimary,  // foreground
        }),
      ),
      

      【讨论】:

        【解决方案3】:

        在 Flutter 中,一些小部件处理用于一般应用程序主题目的的样式和主题,因为它不允许直接更改颜色,但可以使用样式参数:

        ElevatedButton(
          style: ElevatedButton.styleFrom({
            Color primary: Colors.green,
            Color onPrimary: Colors.white,  
          }),
        ),
        

        有关更多信息,请访问Flutter documents ElevatedButton.styeFrom 并尝试不同的参数。

        欢迎来到颤振。

        【讨论】:

          【解决方案4】:

          您可以通过以下方式设置 ElevatedButton 的样式:

          ElevatedButton(
                 style: ButtonStyle(
                       backgroundColor: MaterialStateProperty
                       .all<Color>(Colors.blue),
                       foregroundColor: MaterialStateProperty
                      .all<Color>(Colors.white),
                  ),
                child: Text('your text'),
                onPressed: null,
          ),
          

          【讨论】:

            【解决方案5】:

            更新你的 flutter SDK。发生这种情况是因为 SDK 未更新。

            要更新您的 flutter SKD,请从 Windows 打开 CMD 并命令“flutter upgrade”。

            【讨论】:

              猜你喜欢
              • 2021-05-21
              • 2022-01-17
              • 2021-12-11
              • 1970-01-01
              • 2023-01-03
              • 2021-06-18
              • 2021-11-26
              • 2022-01-17
              • 1970-01-01
              相关资源
              最近更新 更多