gpt4 book ai didi

选择时 Flutter 更改 TabItem 背景

转载 作者:IT王子 更新时间:2023-10-29 06:54:56 27 4
gpt4 key购买 nike

我想问选择选项卡时如何更改选项卡项目背景颜色?

抱歉我是flutter的新手

使用底部标签栏好还是标签栏好?

像这样:

enter image description here

我的代码:

          bottomNavigationBar: new TabBar(
tabs: [
Tab(
icon: new Icon(Icons.home),
),
Tab(
icon: new Icon(Icons.rss_feed),
),
Tab(
icon: new Icon(Icons.perm_identity),
),
Tab(icon: new Icon(Icons.settings),)
],
labelColor: Colors.yellow,
indicatorWeight: 1.0,
unselectedLabelColor: Colors.blue,
indicatorSize: TabBarIndicatorSize.label,
indicatorPadding: EdgeInsets.all(5.0),
indicatorColor: Colors.red,
),
backgroundColor: Colors.black,
),
),
);
}
}

最佳答案

这非常简单,不需要实现全新的自定义标签栏。您只需创建一个自定义选择指示器,如下所示:

TabBar(
...
indicator: SolidIndicator(),
)

然后是SolidIndicator实现:

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

/// Solid tab bar indicator.
class SolidIndicator extends Decoration {
@override
BoxPainter createBoxPainter([VoidCallback onChanged]) {
return _SolidIndicatorPainter(this, onChanged);
}
}

class _SolidIndicatorPainter extends BoxPainter {
final SolidIndicator decoration;

_SolidIndicatorPainter(this.decoration, VoidCallback onChanged)
: assert(decoration != null),
super(onChanged);

@override
void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
assert(configuration != null);
assert(configuration.size != null);

final Rect rect = offset & configuration.size;
final Paint paint = Paint();
paint.color = Colors.white;
paint.style = PaintingStyle.fill;
canvas.drawRect(rect, paint);
}
}

关于选择时 Flutter 更改 TabItem 背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51941328/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com