简体   繁体   English

在 TabBar 中更改图像颜色 Flutter

[英]change image color in TabBar Flutter

When clicking on the image they change color, it just doesn't change if you drag with your finger, could someone help me with the icons change color when the user also wants to drag with the finger, this below is the code.单击图像时它们会改变颜色,如果用手指拖动它就不会改变,当用户也想用手指拖动时,有人可以帮我改变图标的颜色,下面是代码。

                SliverOverlapAbsorber(
                        handle: NestedScrollView.sliverOverlapAbsorberHandleFor(
                            context),
                        sliver: SliverAppBar(
                          pinned: true,
                          floating: true,
                          automaticallyImplyLeading: false,
                          forceElevated: innerBoxIsScrolled,
                          bottom: TabBar(
                            onTap: (value) {
                              WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
                                setState(() {
                                  index=value;
                                });
                              });
                            },
                            tabs: [
                              Tab(
                                icon: SizedBox(
                                  height: 25,
                                  child: Image.asset(
                                    AppImages.fix,
                                    color:
                                        index == 0 ? Colors.white : Colors.blue,
                                  ),
                                ),
                              ),
                              Tab(
                                icon: SizedBox(
                                  height: 25,
                                  child: Image.asset(
                                    AppImages.home,
                                    color:
                                        index == 1 ? Colors.white : Colors.blue,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),

在此处输入图像描述

Please have a look at the official documentation on how to implement TabBar :请查看有关如何实现TabBar的官方文档:

https://api.flutter.dev/flutter/material/TabBar-class.html https://api.flutter.dev/flutter/material/TabBar-class.html

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
 
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: DefaultTabController(
        length: 2,
        child: Builder(
          builder: (context) {
            final TabController tabController =
                DefaultTabController.of(context)!;
            tabController.addListener(() {
              if (!tabController.indexIsChanging) {
                setState(() {});
              }
            });
            return NestedScrollView(
              headerSliverBuilder: (context, innerBoxIsScrolled) {
                return [
                  SliverOverlapAbsorber(
                    handle: NestedScrollView.sliverOverlapAbsorberHandleFor(
                        context),
                    sliver: SliverAppBar(
                      pinned: true,
                      floating: true,
                      automaticallyImplyLeading: false,
                      forceElevated: innerBoxIsScrolled,
                      bottom: TabBar(tabs: [
                        Tab(
                          icon: Icon(
                            Icons.home,
                            color: tabController.index == 0
                                ? Colors.red
                                : Colors.white,
                          ),
                        ),
                        Tab(
                          icon: Icon(
                            Icons.light,
                            color: tabController.index == 1
                                ? Colors.red
                                : Colors.white,
                          ),
                        ),
                      ]),
                    ),
                  )
                ];
              },
              body: TabBarView(
                controller: tabController,
                children: const [
                  Center(
                    child: Text('page 1'),
                  ),
                  Center(
                    child: Text('page 2'),
                  ),
                ],
              ),
            );
          },
        ),
      ),
    );
  }
}

A friend of mine helped me我的一个朋友帮助了我

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

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