【问题标题】:How to set Gradient Color to Icons in Flutter?如何在 Flutter 中为图标设置渐变颜色?
【发布时间】:2019-10-30 16:24:56
【问题描述】:

如何使图标的颜色渐变?

请看下面的例子:

Click Here to see the Example

我现在的代码...

appBar: AppBar(
            backgroundColor: Colors.white,
            bottom: TabBar(
              labelColor: (Colors.blue),
              indicatorColor: Colors.blue,
              unselectedLabelColor: Colors.grey,
              tabs: [
                Tab(icon: Icon(Icons.search,size: 40,)),
                Tab(icon: Icon(Icons.star,size: 40,)),
                Tab(icon: Icon(Icons.account_circle,size: 40,)),
              ],
            ),
          ),

【问题讨论】:

标签: flutter dart


【解决方案1】:

您可以使用 ShaderMask
您可以在下面运行完整代码

代码sn-p

Tab(icon: ShaderMask(
                    blendMode: BlendMode.srcIn,
                    shaderCallback: (Rect bounds) {
                      return ui.Gradient.linear(
                        Offset(4.0, 24.0),
                        Offset(24.0, 4.0),
                        [
                          Colors.blue[200],
                          Colors.greenAccent,
                        ],
                      );
                    },
                    child: Icon(Icons.search)))

完整代码

import 'package:flutter/material.dart';
import 'dart:ui' as ui;

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: TabBarDemo(),
    );
  }
}


class TabBarDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: DefaultTabController(
        length: 3,
        child: Scaffold(
          appBar: AppBar(
            bottom: TabBar(
              tabs: [
                Tab(icon: ShaderMask(
                    blendMode: BlendMode.srcIn,
                    shaderCallback: (Rect bounds) {
                      return ui.Gradient.linear(
                        Offset(4.0, 24.0),
                        Offset(24.0, 4.0),
                        [
                          Colors.blue[200],
                          Colors.greenAccent,
                        ],
                      );
                    },
                    child: Icon(Icons.search))),
                Tab(icon: ShaderMask(
                    blendMode: BlendMode.srcIn,
                    shaderCallback: (Rect bounds) {
                      return ui.Gradient.linear(
                        Offset(4.0, 24.0),
                        Offset(24.0, 4.0),
                        [
                          Colors.blue[200],
                          Colors.greenAccent,
                        ],
                      );
                    },
                    child: Icon(Icons.star))),
                Tab(icon: ShaderMask(
                    blendMode: BlendMode.srcIn,
                    shaderCallback: (Rect bounds) {
                      return ui.Gradient.linear(
                        Offset(4.0, 24.0),
                        Offset(24.0, 4.0),
                        [
                          Colors.blue[200],
                          Colors.greenAccent,
                        ],
                      );
                    },
                    child: Icon(Icons.account_circle))),
              ],
            ),
            title: Text('Tabs Demo'),
          ),
          body: TabBarView(
            children: [
              Icon(Icons.directions_car),
              Icon(Icons.directions_transit),
              Icon(Icons.directions_bike),
            ],
          ),
        ),
      ),
    );
  }
}

【讨论】:

  • 我运行了示例代码。但它没有用。您的屏幕截图与提供的 OP 不同。
猜你喜欢
  • 2020-04-01
  • 2019-09-04
  • 2020-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-01
  • 1970-01-01
  • 2016-12-15
相关资源
最近更新 更多