根据颤动中的变量更改背景色 [英] Changing backgroundcolor depending on variable in flutter

查看:52
本文介绍了根据颤动中的变量更改背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Flutter时,我想更改我的应用程序的背景色,无论何时"color"的值等于0.变量更改.

With Flutter i want to change the backgroundcolor of my app whenever the value of "color" variable changes.

 String color = "white";

@override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.green,

我不知道如何将颜色设置为backgroundColor属性.

I don't know how to set color to backgroundColor proprety.

推荐答案

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(),
        );
      }
    }

这篇关于根据颤动中的变量更改背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆