flutter - 如何在Flutter中将渐变颜色设置为图标?

标签 flutter dart

如何使图标的颜色渐变?

请参见下面的示例:

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,)),
              ],
            ),
          ),

最佳答案

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

代码段

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)))

enter image description here

完整的代码
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),
            ],
          ),
        ),
      ),
    );
  }
}

关于flutter - 如何在Flutter中将渐变颜色设置为图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58629838/

相关文章:

flutter - 如何回到之前的路线?

testing - 如何测试main.dart

flutter - 如何在 Flutter 中用作边框颜色的两种颜色之间连续设置动画

flutter - `State<T extends StatefulWidget>`有什么用

flutter - 如何在 Flutter 中实现 CircularProgressIndicator

Dart 用 List<int> 填充 ByteData

dart - 与 java 的实现相比,为什么这个 Dart 代码这么慢?

flutter User.Read azure Active Directory "Insufficient privileges to complete the operation"

flutter - 创建类似于突破游戏的方 block 网格

带有 WebView 的 Flutter 应用程序中的 Firebase 身份验证