flutter 如何模糊抖动中的底部导航栏?

u59ebvdq  于 2023-01-14  发布在  Flutter
关注(0)|答案(3)|浏览(174)

我想模糊一个脚手架的BottomNavigationBar的背景,这样它可以给后面的项目很酷的模糊效果。我该怎么做呢?

**更多信息:**我尝试通过在BottomNavigationBar中创建一个新主题来为canvasColor添加不透明度。

bottomNavigationBar: new Theme( data: Theme.of(context).copyWith( canvasColor: Color(0xff424242).withOpacity(0.5), ), child: new BottomNavigationBar( onTap: navigationTapped, currentIndex: _page, items: [ new BottomNavigationBarItem( icon: new Icon(Icons.home), title: new Text('Home') ), new BottomNavigationBarItem( icon: new Icon(Icons.dashboard), title: new Text('Menu') ), new BottomNavigationBarItem( icon: new Icon(Icons.date_range), title: new Text('Dates') ) ], ), )
这是我得到的输出:Image
令人惊讶的是,正如你所看到的,不透明度根本没有被应用。我实际上不希望BottomNavigationBar是不透明的。我希望它是模糊的,这样它后面的内容就可以在BottomNavigationBar上被视为模糊的。我也尝试过将BottomNavigationBar Package 在ImageFilter.blur()中,但这也不起作用。

9rygscc1

9rygscc11#

为了实现这个功能,我必须将底部导航与屏幕的其他内容放在一个堆栈中。我使用了ClipRect,BackdropFilter和Opacity的组合。这是工作解决方案:

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: _buildAppBar(context),
      body: Stack(
        //these are the screens that will be loaded for every bottom nav item
        children: <Widget>[
          IndexedStack(
            index: _currentTab,
            children: _pages,
          ),
          _buildBottomNavigation()
        ],
      ),
    );
  }

Widget _buildBottomNavigation() => Align(
        alignment: FractionalOffset.bottomCenter,
        //this is very important, without it the whole screen will be blurred
        child: ClipRect(
          //I'm using BackdropFilter for the blurring effect
          child: BackdropFilter(
            filter: ImageFilter.blur(
              sigmaX: 10.0,
              sigmaY: 10.0,
            ),
            child: Opacity(
              //you can change the opacity to whatever suits you best
              opacity: 0.8,
              child: BottomNavigationBar(
                currentIndex: _currentTab,
                onTap: (int index) {
                  setState(() {
                    _currentTab = index;
                  });
                },
                type: BottomNavigationBarType.fixed,
                unselectedItemColor: AppColors.colorBlack,
                items: allRoutes.map((NavigationRoute route) {
                  return _buildBottomNavigationBarItem(route);
                }).toList(),
              ),
            ),
          ),
        ),
      );
ecfsfe2w

ecfsfe2w2#

** just set a transparent backgroundColor .**
@override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      navigationBar: CupertinoNavigationBar(
        backgroundColor: Colors.transparent,
        border: Border.all(
          width: 0.0,
          style: BorderStyle.none,
        ),
      ),
      child: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              Expanded(
                child: Container(
                  padding: EdgeInsets.only(
                    left: 20.0,
                    top: 0.0,
                    right: 20.0,
                  ),
                  height: 280.0,
                  decoration: BoxDecoration(
                    gradient: LinearGradient(
                      colors: Const.color.linearColors,
                    ),
//                    image: DecorationImage(
//                      image: AssetImage('assets/image/bac.png'),
//                    ),
                  ),
                  child: Row(
                    children: <Widget>[Text('Hello')],
                  ),
                ),
              ),

              /// end of Expanded
            ],
          ),
        ],
      ),
    );
n6lpvg4x

n6lpvg4x3#

你可以用一个Backdropfilter Package 你的BottomNavigationBar,然后用一个ClipRRect Package 所有的BottomNavigationBar。最后但同样重要的是,将Scaffold属性extendBody设置为true。

bottomNavigationBar: ClipRRect(
    child: BackdropFilter(
      filter: ImageFilter.blur(sigmaX: 3, sigmaY: 3),
      child: BottomNavigationBar(
       YOUR CODE HERE

相关问题