如何更改appBar背景颜色以使其与Flutter中不同的tabView页面的背景颜色匹配?

约翰

我试图建立一个简单的应用程序结构的appBartabBar和3tabView页。这些页面中的每一个都有不同的背景颜色。我希望这种背景色可以覆盖整个屏幕(即,AppBar也要占用的空间)。因此,我需要在用户在选项卡之间切换时更改AppBar的颜色。

这是我走了多远:

import 'package:flutter/material.dart';

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  Color PrimaryColor = Colors.teal[400];

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 3,
      child: Scaffold(
        appBar: AppBar(
            elevation: 0,
            backgroundColor: PrimaryColor,
            bottom: TabBar(
              isScrollable: false,
              indicatorColor: Colors.white,
              indicatorWeight: 5,
              onTap: (index) {
                setState(() {
                  switch (index) {
                    case 0:
                      PrimaryColor = Colors.teal[400];
                      break;
                    case 1:
                      PrimaryColor = Colors.orange[500];
                      break;
                    case 2:
                      PrimaryColor = Colors.pink[500];
                      break;
                    default:
                  }
                });
              },
              tabs: [
                Tab(text: ''),
                Tab(text: ''),
                Tab(text: ''),
              ],
            )),
        body: TabBarView(
          children: [
            Container(
              color: Colors.teal[400],
            ),
            Container(
              color: Colors.orange[500],
            ),
            Container(
              color: Colors.pink[500],
            ),
          ],
        ),
      ),
    );
  }
}

几乎可以实现我想要的UI,但是仅当tabBar按下按钮时背景才会改变,而在选项卡之间使用滑动手势时不会改变背景

非常感谢您提供一些纠正方法的指导。谢谢。

该图显示了3个选项卡屏幕的UI的模型

此图显示了选项卡1和2之间过渡的模型,其中整个背景(包括应用栏的背景)在滑动时都会改变

洛夫

如果您希望在颜色改变时在AppBar中保持透明,可以使用StackWidget来放置TabBar以上页面:

DefaultTabController(
  length: 3,
  child: Scaffold(
    body: Stack(
      alignment: Alignment.topCenter,
      children: [
        TabBarView(
          children: [
            Container(
              padding: const EdgeInsets.only(top: 132.0), //note that without this padding the content of the page will apear behind the TabBar
              color: Colors.teal[400],
            ),
            Container(
              color: Colors.orange[500],
            ),
            Container(
              color: Colors.pink[500],
            ),
          ],
        ),

        Column(
          children: [
            SizedBox(height: 50.0),
            Text(
              'Title',   //A text to represent the title of the image
              textAlign: TextAlign.center,
              style: TextStyle(fontSize: 30.0, color: Colors.white),
            ),
            TabBar(
              isScrollable: false,
              indicatorColor: Colors.white,
              indicatorWeight: 5,
              onTap: (index) {},
              tabs: [
                Tab(text: 'Home'),
                Tab(text: 'Groups'),
                Tab(text: 'Profile'),
              ],
            ),
          ],
        )
      ],
    ),
  ),
);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Angular中更改整个页面的背景颜色

如何更改背景颜色以使其适合 CircularPercentIndicator?

在Flutter中在PageView页面的背景颜色之间创建渐变?

在 MUI 中更改 Appbar 背景颜色

我们如何在Flutter中更改Appbar背景颜色

如何在Xamarin iOS中更改选项卡式页面的背景颜色?

如何在Word 2010中更改单个页面的背景颜色?

如何更改 OpenGL 表面的背景颜色?

如何将整个pdf页面的背景颜色从白色更改为rails中的其他颜色

如何更改div元素的背景颜色以与主体背景颜色匹配?

更改网站中每个页面的导航栏的背景颜色和文本颜色

如何使用 Xamarin 中的开关更改页面背景颜色

是否可以更改多个页面的主体背景颜色?

在 Wordpress 上滚动时更改页面的背景颜色

如何在Flutter中更改BottomSheet的背景颜色?

如何在 Flutter InputDecoration 中更改 errorText 背景颜色

如何在Flutter中更改PopupMenuItem的背景颜色

如何在Flutter中更改DataColumn的背景颜色?

如何在Flutter中更改弹出背景颜色

如何更改背景颜色

如何更改背景颜色

在Flutter中更改图像的背景颜色

如何更改单个内容页面的状态栏背景、文本颜色 Xamarin(Ios、Android)

如何使用表单和php更改html页面的背景颜色?

更改文本颜色以匹配动态背景颜色

更改Flutter抽屉的背景颜色

用样式设置页面的背景颜色

更改不同页面上包含的PHP的背景颜色?

如何更改搜索栏后面的背景颜色