简体   繁体   English

Flutter 改变 FloatingActionButton 的子图标颜色

[英]Flutter change the child icon color of FloatingActionButton

I'm new to Flutter and was trying to change the child icon color of FloatingActionButton.我是Flutter的新手,正在尝试更改 FloatingActionButton 的子图标颜色。 Child icon color is white by default.子图标颜色默认为白色。

How can i change that??我怎样才能改变它?

Given below is the code that i have worked on.下面给出的是我工作过的代码。

floatingActionButton: new FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: new Icon(Icons.add),
        backgroundColor: Colors.yellow,

      ), // This trailing comma makes auto-formatting nicer for build methods.
    ); 

Thank you.谢谢你。

You can wrap in an IconTheme您可以包装在IconTheme

child: new IconTheme(
    data: new IconThemeData(
        color: Colors.yellow), 
    child: new Icon(Icons.add),
),

or a Theme where iconTheme is set the way you want iticonTheme Theme设置为您想要的方式的主题

(not tested) (未测试)

To change the color of child Icon, You have to set the color in the Icon() widget.要更改子图标的颜色,您必须在 Icon() 小部件中设置颜色。

Here I am sharing the code snippet where I have set the red color.在这里,我分享了设置红色的代码片段。

floatingActionButton: FloatingActionButton(
        tooltip: 'Increment',
        child: new Icon(Icons.add, color: Colors.red,),
        backgroundColor: Colors.yellow,
      ),

Flutter v17.4颤振 v17.4

This worked for me, if you just want to change to colour of icon.如果您只想更改图标的颜色,这对我有用。

floatingActionButton: FloatingActionButton(
    child: Icon(Icons.add,
      color: Colors.black, //The color which you want set.
    ),
    onPressed: () => {},
  ),

MaterialApp theme: ThemeData MaterialApp 主题:ThemeData

If using ThemeData in your MaterialApp to color your app like:如果在MaterialApp中使用ThemeData为您的应用着色,例如:

    return MaterialApp(
        title: 'Flutter Demo',
        theme: lightThemeData, // this guy
        darkTheme: darkThemeData, // and perhaps this guy
        home: MyHomePage()
    );

... you have 2 ways to color your FloatingActionButton (FAB) Icon color in ThemeData : ...您有2 种方法可以在ThemeData中为 FloatingActionButton (FAB) 图标颜色着色

  1. floatingActionButtonTheme: const FloatingActionButtonThemeData(foregroundColor: myColor)
  2. onSecondary: myColor (used only if ↑↑↑ is missing) onSecondary: myColor在缺少 ↑↑↑ 时使用)
final lightThemeData = ThemeData.from(
    colorScheme: ColorScheme.fromSeed(
      seedColor: Colors.blue,
      brightness: Brightness.light,
      primary: blue,
      secondary: blue,
      onSecondary: white, // FAB Icon color if FABTheme not provided like below
    ),
).copyWith(
  floatingActionButtonTheme: const FloatingActionButtonThemeData(foregroundColor: Colors.green), // this guy works
  iconTheme: const IconThemeData(color: white), // DOES NOT affect FAB Icon
  accentIconTheme: const IconThemeData(color: purpleRain), // argumentFormerlyKnownAsFabIconColor - deprecated, DON'T USE THIS
);

In the above themeData example, the FAB Icon would be colored green .在上面的themeData示例中,FAB 图标将显示为绿色

Because onSecondary is used to color the FAB icon only if floatingActionButtonTheme is missing.因为只有floatingActionButtonTheme缺失时才使用onSecondary为 FAB 图标着色。

If neither of the above theme colors are provided, the FAB Icon will fallback to black color.如果上述主题颜色均未提供,FAB 图标将回退为黑色。

iconTheme and accentIconTheme both have no effect on FAB icon color. iconThemeaccentIconTheme都对FAB 图标颜色没有影响

FAB accentIconTheme change happened in Flutter 1.17 . FAB accentIconTheme 更改发生在 Flutter 1.17中。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM