Flutter中AppBar的elevation属性失效,阴影不显示问题求助
Flutter AppBar elevation阴影不显示问题解决
你的问题出在ClipPath包裹了AppBar——ClipPath会裁剪掉AppBar边界外的内容,而AppBar的阴影是绘制在自身区域之外的,所以即使设置了elevation属性,阴影也会被裁剪掉,无法显示。
下面提供两种解决方案:
方案一:移除不必要的ClipPath(无裁剪需求时)
如果不需要对AppBar进行裁剪,直接去掉ClipPath,给AppBar添加elevation即可:
appBar: PreferredSize( preferredSize: Size.fromHeight(130.0), child: AppBar( elevation: 8.0, // 设置阴影高度,数值越大阴影越明显 centerTitle: true, toolbarHeight: 130.0, title: Container( alignment: Alignment.center, child: RichText( textAlign: TextAlign.center, text: TextSpan( children: [ TextSpan( text: "Biruni University\n", style: TextStyle( color: Colors.white, fontSize: 30.0, ), ), TextSpan( text: "Attendance System", style: TextStyle( color: Colors.white, fontSize: 15.0, ), ), ], ), ), ), ), ),
方案二:保留ClipPath时用Container模拟阴影
如果确实需要ClipPath的裁剪效果,关闭AppBar自带的阴影,用外层Container的boxShadow模拟阴影:
appBar: PreferredSize( preferredSize: Size.fromHeight(130.0), child: Container( decoration: BoxDecoration( boxShadow: [ BoxShadow( color: Colors.grey.withOpacity(0.5), spreadRadius: 2, blurRadius: 5, offset: Offset(0, 3), // 控制阴影的偏移方向和距离 ), ], ), child: ClipPath( // 这里保留你的ClipPath路径(如果有自定义路径的话) child: AppBar( elevation: 0.0, // 关闭AppBar自带阴影 centerTitle: true, toolbarHeight: 130.0, title: Container( alignment: Alignment.center, child: RichText( textAlign: TextAlign.center, text: TextSpan( children: [ TextSpan( text: "Biruni University\n", style: TextStyle( color: Colors.white, fontSize: 30.0, ), ), TextSpan( text: "Attendance System", style: TextStyle( color: Colors.white, fontSize: 15.0, ), ), ], ), ), ), ), ), ), ),
内容的提问来源于stack exchange,提问作者ismail
相关产品推荐
相关产品推荐

