如何在Flutter中更改单个选项卡的背景。

huangapple go评论52阅读模式
英文:

how to change background of individual Tab in flutter

问题

如何在Flutter中更改单个标签的背景,类似于以下示例:

如何在Flutter中更改单个选项卡的背景。

在Flutter中更改单个标签的背景颜色

TabBar(
  isScrollable: true,
  unselectedLabelStyle: TextStyle(color: Colors.black),
  unselectedLabelColor: Colors.black,
  indicator: BoxDecoration(
    borderRadius: BorderRadius.circular(15),
    color: Colors.black,
  ),
  tabs: [
    Tab(
      child: Container(
        color: Color(0xffebe7e2),
        child: Text(
          "All",
          textAlign: TextAlign.center,
        ),
      ),
    ),
  ],
)
英文:

how to change background of individual Tab in flutter like This

如何在Flutter中更改单个选项卡的背景。

change backgrounds colour of individual tab in flutter

                        TabBar(
                            isScrollable: true,
                            unselectedLabelStyle: TextStyle(color: Colors.black),
                            unselectedLabelColor: Colors.black,
                            indicator:BoxDecoration(
                                borderRadius: BorderRadius.circular(15),
                                color: Colors.black),
                            tabs: [
                              Tab(
                                child: Container(
                                  color: Color(0xffebe7e2),
                                  child: Text(
                                    "All",
                                    textAlign: TextAlign.center,
                                  ),
                                ),
                              ),
                            ],
                          ),

答案1

得分: 0

你可以在Container的装饰中设置选项卡项的外观,代码如下:

class FA extends StatefulWidget {
  const FA({super.key});

  @override
  State<FA> createState() => _FAState();
}

class _FAState extends State<FA> with SingleTickerProviderStateMixin {
  late final controller = TabController(length: 3, vsync: this)
    ..addListener(() {
      setState(() {});
    });
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: TabBar(
        controller: controller,
        isScrollable: true,
        unselectedLabelStyle: TextStyle(color: Colors.black),
        unselectedLabelColor: Colors.transparent,
        indicator: BoxDecoration(),
        indicatorPadding: EdgeInsets.zero,
        // splashBorderRadius: ,
        tabs: [
          Tab(
            iconMargin: EdgeInsets.zero,
            child: Container(
              decoration: BoxDecoration(
                  color: controller.index == 0
                      ? Colors.black
                      : Color.fromARGB(255, 164, 164, 164),
                  borderRadius: BorderRadius.circular(16)),
              padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
              child: Text(
                "All",
                textAlign: TextAlign.center,
                style: TextStyle(
                  color: controller.index == 0 ? Colors white : Colors.black,
                ),
              ),
            ),
          ),
          Tab(
            iconMargin: EdgeInsets.zero,
            child: Container(
              decoration: BoxDecoration(
                  color: controller.index == 1
                      ? Colors black
                      : Color.fromARGB(255, 164, 164, 164),
                  borderRadius: BorderRadius.circular(16)),
              padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
              child: Text(
                "Next btn",
                textAlign: TextAlign center,
                style: TextStyle(
                  color: controller.index == 1 ? Colors.white : Colors.black,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
英文:

You can provide tab item look on Container decoration like

class FA extends StatefulWidget {
  const FA({super.key});

  @override
  State&lt;FA&gt; createState() =&gt; _FAState();
}

class _FAState extends State&lt;FA&gt; with SingleTickerProviderStateMixin {
  late final controller = TabController(length: 3, vsync: this)
    ..addListener(() {
      setState(() {});
    });
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: TabBar(
        controller: controller,
        isScrollable: true,
        unselectedLabelStyle: TextStyle(color: Colors.black),
        unselectedLabelColor: Colors.transparent,
        indicator: BoxDecoration(),
        indicatorPadding: EdgeInsets.zero,
        // splashBorderRadius: ,
        tabs: [
          Tab(
            iconMargin: EdgeInsets.zero,
            child: Container(
              decoration: BoxDecoration(
                  color: controller.index == 0
                      ? Colors.black
                      : Color.fromARGB(255, 164, 164, 164),
                  borderRadius: BorderRadius.circular(16)),
              padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
              child: Text(
                &quot;All&quot;,
                textAlign: TextAlign.center,
                style: TextStyle(
                  color: controller.index == 0 ? Colors.white : Colors.black,
                ),
              ),
            ),
          ),
          Tab(
            iconMargin: EdgeInsets.zero,
            child: Container(
              decoration: BoxDecoration(
                  color: controller.index == 1
                      ? Colors.black
                      : Color.fromARGB(255, 164, 164, 164),
                  borderRadius: BorderRadius.circular(16)),
              padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
              child: Text(
                &quot;Next btn&quot;,
                textAlign: TextAlign.center,
                style: TextStyle(
                  color: controller.index == 1 ? Colors.white : Colors.black,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

huangapple
  • 本文由 发表于 2023年2月8日 22:30:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387256.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定