如何使导航底部栏透明

时间:2020-06-02 18:28:04

标签: flutter dart

我正在尝试创建一个透明的底部导航栏,这是我的底部栏部分:

Align(
                      alignment: Alignment.bottomCenter,
                      child: Theme(
                          data: Theme.of(context)
                              .copyWith(canvasColor: Colors.transparent),
                          child: BottomNavigationBar(
                            currentIndex: 0,
                            items: [
                              BottomNavigationBarItem(
                                  icon: Icon(Icons.location_on),
                                  title: Text('Map')),
                              BottomNavigationBarItem(
                                  icon: Icon(Icons.line_style),
                                  title: Text('List')),
                              BottomNavigationBarItem(
                                  icon: Icon(Icons.add), title: Text('Add Property'))
                            ],
                          ))),

这是我的完整Scafold Widget

return Scaffold(
      backgroundColor: AppTheme.white,
      body: FutureBuilder<bool>(
        future: getData(),
        builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
          if (!snapshot.hasData) {
            return const SizedBox();
          } else {
            return Padding(
              padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  appBar(),
                  // tabBar(),
                  Expanded(
                    child: FutureBuilder<bool>(
                      future: getData(),
                      builder:
                          (BuildContext context, AsyncSnapshot<bool> snapshot) {
                        if (!snapshot.hasData) {
                          return const SizedBox();
                        } else {
                          return ChangeNotifierProvider(
                            create: (context) => Properties(),
                            child: PropertiesGrid(_showOnlyFavorites),
                          );
                        }
                      },
                    ),
                  ),
                  Align(
                      alignment: Alignment.bottomCenter,
                      child: Theme(
                          data: Theme.of(context)
                              .copyWith(canvasColor: Colors.transparent),
                          child: BottomNavigationBar(
                            currentIndex: 0,
                            items: [
                              BottomNavigationBarItem(
                                  icon: Icon(Icons.location_on),
                                  title: Text('Map')),
                              BottomNavigationBarItem(
                                  icon: Icon(Icons.line_style),
                                  title: Text('List')),
                              BottomNavigationBarItem(
                                  icon: Icon(Icons.add), title: Text('Add Property'))
                            ],
                          ))),
                ],
              ),
            );
          }
        },
      ),
    );
  }

  Widget appBar() {
    return SizedBox(
      height: AppBar().preferredSize.height,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Padding(
            padding: const EdgeInsets.only(top: 8, left: 8),
            child: Container(
              width: AppBar().preferredSize.height - 8,
              height: AppBar().preferredSize.height - 8,
            ),
          ),
          Expanded(
            child: Center(
              child: Padding(
                padding: const EdgeInsets.only(top: 4),
                child:
                    Image.asset('assets/images/logo.png', fit: BoxFit.contain),
              ),
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(top: 8, right: 8),
            child: Container(
              width: AppBar().preferredSize.height - 8,
              height: AppBar().preferredSize.height - 8,
              color: Colors.white,
              child: Material(
                color: Colors.transparent,
                child: InkWell(
                  borderRadius:
                      BorderRadius.circular(AppBar().preferredSize.height),
                  child: Icon(
                    Icons.location_on,
                    color: AppTheme.dark_grey,
                  ),
                  onTap: () {
                    setState(() {
                      multiple = !multiple;
                    });
                  },
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget tabBar() {
    return SizedBox(
      height: AppBar().preferredSize.height,
    );
  }
}

但是我看到它没有像透明条中那样显示页面背景,如下图所示。

Result

我希望有人能帮助我弄清楚问题出在哪里。

1 个答案:

答案 0 :(得分:1)

您应该在脚手架外部而不是在身体内部使用futurebuilder,因为您确实要更改整个页面

return FutureBuilder<bool>(
      future: getData(),
      builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
        if (!snapshot.hasData) {
          return const SizedBox();
        } else 
          return Scaffold(
            backgroundColor: AppTheme.white,
            extendBody: true, //the body of the scaffold is shown behind the bottomNavigationBar
            appBar: AppBar( //use an AppBar widget instead of the one you created
              centerTitle: true, //this centers the title, in this case the image
              leading: IconButton(
                icon: const Icon(Icons.menu),
                onPressed: null,
              ), //the button of the left
              title: Padding( //your image
                padding: const EdgeInsets.only(top: 4),
                child: Image.asset('assets/images/logo.png', fit: BoxFit.contain),
              ),
              actionsIconTheme: IconThemeData(color: AppTheme.dark_grey), //this change the theme of the action buttons, the one at the right
              actions: [
                IconButton(
                   icon: const Icon(Icons.location_on),
                   onPressed: () {
                     setState(() {
                       multiple = !multiple;
                     });
                  },
               )
             ],
            ),
            body: ChangeNotifierProvider(
              create: (context) => Properties(),
              child: PropertiesGrid(_showOnlyFavorites),
            ),
           bottomNavigationBar: BottomNavigationBar(
             backgroundColor: Colors.transparent, //no color
             elevation: 0.0, // no shadow
             currentIndex: 0,
             items: [
               BottomNavigationBarItem(
                 icon: Icon(Icons.location_on),
                 title: Text('Map')),
               BottomNavigationBarItem(
                 icon: Icon(Icons.line_style),
                 title: Text('List')),
               BottomNavigationBarItem(
                 icon: Icon(Icons.add), title: Text('Add Property'))
             ],
           ),
          )
        }
    );

您内部有另一个具有相同未来的Future生成器,这是无关紧要的,因为您已经在第一个中拥有值,所以我删除了它。当未来在等待并且没有数据时,它将显示一个您想要的sizeBox(),当未来完成时它将构建支架,您可以使用其参数在应有的位置使用appbar和BottomNavigationBarItem来构建页面。是

更新

我用脚手架期望的小部件AppBar()更改了应用程序栏(它期望实现了PreferredSizeWidget类的控件)

Widget appBar() {
    return SizedBox(....);
  }

您创建的此小部件将SizedBox用作treee中的第一个小部件,该小部件未实现该类,因此会给您带来错误,最好先使用flutter使您开箱即用,然后在了解其工作原理后可以创建自己的扩展PreferredSizeWidget(如果需要实现其他功能)