简体   繁体   English

如何在返回小部件的函数中获取颜色?

[英]How to get color inside a function that returns a widget?

I am learning flutter, I am trying to do something like 'values/color.xml' (which is used in android studio) to keep my colors in a file so that i can use them throughout my app easily.我正在学习颤动,我正在尝试做一些类似'values/color.xml' (在android studio中使用)的事情来将我的颜色保存在一个文件中,以便我可以在我的应用程序中轻松使用它们。 i have stored them in a class but when i use it inside a function that returns widget the colors are not appearing.我已将它们存储在一个类中,但是当我在返回小部件的函数中使用它时,颜色没有出现。

Color Values Class:颜色值类:

import 'package:flutter/material.dart';

class ColorValues {
  static Color highlightColor = const Color(0xFF202d35);
}

my function:我的功能:

Container currentPage() {
    return Container(
      height: 20.0,
      width: double.infinity,
      child: Text("index"),
      color: ColorValues.highlightColor, //Doesn't work.
      color: Colors.Black, // But this works perfectly.
    );
  }

Inside this container widget i will be displaying the index of current page of PageView.在这个容器小部件中,我将显示 PageView 当前页面的索引。 I am using ColorValues.highlightColor inside my build function and it is working fine there.我在我的构建函数中使用 ColorValues.highlightColor,它在那里工作正常。 help me please.请帮帮我。

Thank you.谢谢你。

Try this once.试试这个。 Remove the static keyword and if you are working in flutter then rename your color file as color.dart not as color.xml删除 static 关键字,如果您正在使用颤振,则将您的颜色文件重命名为 color.dart 而不是 color.xml

将您的颜色文件重命名为color_value.dart ,而不是color.xml ,就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Flutter 中获取小部件内部的主题颜色 - How to get theme color inside the widget in Flutter 如何为 Flutter 中的主窗口小部件内的窗口小部件设置背景颜色? - How to set a background color for a widget inside a main widget in Flutter? 如何在返回 stream 的 function 中获取当前用户 - How to get current user inside a function that returns a stream 如何获取小部件的当前颜色属性? - How to get current color property of a widget? 如何在 flutter 树部件中获取父部件颜色? - How to get parent widget color in flutter tree widget? Flutter,如何从返回的 Widget 调用 Stateful Widget 内部的函数? - Flutter, how to call a function inside Stateful Widget from a returned Widget? 如何从父小部件在子有状态小部件内运行函数? - How to run a function inside a child stateful widget from parent widget? Flutter - 如何更改放置在自定义小部件中的 Color 类的值? - Flutter - How to change the Color class' value that placed inside a custom widget? 如何从 Void 函数内部的变量中获取变量值并将这些变量用于小部件 - How to get Variable Value from Variable inside Void Function and Use Those Variable to Widget 如何在 GridView flutter 中获取小部件的偏移量 - How to get offset of a widget inside a GridView flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM