flutter - 根据颤动中的变量更改背景颜色
问题描述
使用 Flutter,每当“颜色”变量的值发生变化时,我想更改我的应用程序的背景颜色。
String color = "white";
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.green,
我不知道如何将颜色设置为 backgroundColor 属性。
解决方案
class AppColor {
static const RED = "RED";
static const GREEN = "GREEN";
static const BLUE = "BLUE";
static const DEFAULT = "DEFAULT";
static const _colorMap = {
RED: Colors.red,
GREEN: Colors.green,
BLUE: Colors.blue,
DEFAULT: Colors.teal,
};
const AppColor._();
static getColorFor(String color) => _colorMap[color.toUpperCase()] ?? _colorMap[DEFAULT];
}
class SO extends StatelessWidget {
var color = 'red';
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: AppColor.getColorFor(color),
appBar: AppBar(),
);
}
}
推荐阅读
- crystal-lang - 斐波那契问题导致算术溢出
- java - 非递归二分查找
- python - AWS Python Lambda 函数 JSON 格式
- javascript - 创建多个按钮 - Javascript
- python - sqlalchemy 上的 DataFrame 打印数字而不是实际的列名
- c# - 在 .NET 核心应用程序中使用 WinRT 组件
- python - 为什么我在此程序中收到“列表索引超出范围”错误?
- python - 使用 set 和 line.split python
- android - Android有没有办法设置所有小部件的样式?
- google-colaboratory - Colab 笔记本中的最新输出