我想为我的新应用程序创建一个设计系统.

Standard elevated button

      child: ElevatedButton.icon(               
            icon: Icon(Icons.power_settings_new),
            onPressed: () => context.go('/login'),
            label: const Text('Log out'),
          ),

这是可以的,但在这种情况下,它应该有一个红色的重点.这一目标很容易通过使用forecoundColor属性来实现.通过使用该技术,不需要担心图标的 colored颜色 、文本的 colored颜色 、涟漪效果的 colored颜色 .

enter image description here

      child: ElevatedButton.icon(
            style: ElevatedButton.styleFrom(
              foregroundColor: Colors.red,
            ),
            icon: Icon(Icons.power_settings_new),
            onPressed: () => context.go('/login'),
            label: const Text('Log out'),
          ),

但是,我不想每次都指定这个 colored颜色 .我更喜欢做这样的东西,我想避免为按钮编写Anny包装器.

      child: ElevatedButton.error( // Color accent is red
                    icon: Icon(Icons.power_settings_new),
                    onPressed: () => context.go('/login'),
                    label: const Text('Log out'),
                  ),

这件事有可能发生吗?

Question 2:图标、文本和涟漪效果如何知道它们需要使用前景色来呈现自己?

推荐答案

不带参数的扩展

只需更改 colored颜色 ,即可定义errorsuccess的每个实例.

extension ExtendedElevatedButton on ElevatedButton {
  ElevatedButton error() {
    return ElevatedButton(
      style: ElevatedButton.styleFrom(
        foregroundColor: Colors.red,
      ),
      onPressed: onPressed,
      child: child,
    );
  }
}

并将其用作

ElevatedButton(
   onPressed: () => context.go('/login'),
   child: const Text('Log out'),
).error(),

带参数的扩展

通过使用参数并传递所需的 colored颜色 ,可以使其更加通用

extension ExtendedElevatedButton on ElevatedButton {
  ElevatedButton addColor(Color color) {
    return ElevatedButton(
      style: ElevatedButton.styleFrom(
        foregroundColor: color,
      ),
      onPressed: onPressed,
      child: child,
    );
  }
}

并将其用作

ElevatedButton(
   onPressed: () => context.go('/login'),
   child: const Text('Log out'),
).addColor(Colors.red),

Extra: If you further want to use the extension with all parameters you should define every desired extension with the preffered name.

extension ExtendedElevatedButton on ElevatedButton {
  ElevatedButton error(Function<void> onPressed,Widget child) {
    return ElevatedButton(
      style: ElevatedButton.styleFrom(
        foregroundColor: Colors.red,
      ),
      onPressed: onPressed,
      child: child,
    );
  }
  ElevatedButton success(Function<void> onPressed,Widget child) {
    return ElevatedButton(
      style: ElevatedButton.styleFrom(
        foregroundColor: Colors.green,
      ),
      onPressed: onPressed,
      child: child,
    );
  }
}

并将其用作

ElevatedButton.error(
   onPressed: () => context.go('/login'),
   child: const Text('Log out'),
);

ElevatedButton.success(
   onPressed: () => context.go('/login'),
   child: const Text('Log out'),
);

Flutter相关问答推荐

无法将Flutter 连接到Firebase

MobX Build_Runner问题:不生成.G文件(Flutter )

在Flutter 中每次点击按钮时都很难调用自定义函数

使索引[0]在ListViewBuilder中返回字符串1

Flutter GestureDetector单击问题-堆叠外面的按钮不可点击

Flutter 附近的连接

Flutter 需要 Xcode 14 或更高版本

Flutter:我的应用程序是否需要包含退款购买?

找不到任何与 com.google.firebase:firebase-sessions:[15.0.0, 16.0.0) 匹配的版本

Flutter:在 pubspec.yaml 中找不到assets资源

Flutter中pubspec.yaml文件中的name参数是什么?

在 Flutter 中解码 BLE 设备负载

flutter 在文本字段中更改部分文本选定 colored颜色

在 Flutter 中使用 HardwareKeyboard 时所有键都没有响应

Flutter 动画页面计数器文本

Show Snackbar above to showModalBottomSheet Flutter

Getx Flutter 在更新值时抛出空错误

函数中的变量在 Dart 中应该有什么关键字?

从 Uint8List 或图像对象创建 InputImageData 用于 Google ML Kit 人脸检测

如何从 MemoryFileSystem 字节创建假 dart:io 文件?