DaI*_*Guy 7 dart flutter flutter-layout
我是新来的扑扑。我正在尝试实现此用户界面
我还没有找到任何完整的解决方案来在flutter中创建透明的底部导航栏。
我尝试使用
BottomNavigationBarItem(
backgroundColor: Colors.transparent,
icon: e,
activeIcon: _activeIcons[_index],
title: Text(
title[_index],
style: AppStyle.tabBarItem,
),
)
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用。请帮忙。
Ant*_*REL 34
给定的答案都不适合我,我想出了一些非常重要的事情:您必须添加属性 extendBody: true
如果为true,并且指定了bottomNavigationBar 或persistentFooterButtons,则主体会延伸到Scaffold 的底部,而不是仅延伸到bottomNavigationBar 或persistentFooterButtons 的顶部。
当 bottomNavigationBar 具有非矩形形状时,此属性通常很有用,例如 CircularNotchedRectangle,它会在栏的顶部边缘添加一个 FloatingActionButton 大小的凹口。在这种情况下,指定 extendBody: true 可确保通过底部导航栏的凹口可以看到脚手架的主体
随着backgroundColor: Color(0x00ffffff),.
注意:带有 0x 的颜色是十六进制 ARGB 值(0xAARRGGBB),因此 ffffff 之前的 00 表示最大透明度,您可以通过将 00 增加到 ff(十六进制为 255)来增加不透明度。
完整代码示例:
import 'package:flutter/material.dart';
class NavigationBar extends StatefulWidget {
static int _selectedIndex = 0;
@override
NavigationBarState createState() => NavigationBarState();
}
class NavigationBarState extends State<NavigationBar> {
void _onItemTapped(int index) {
setState(() {
NavigationBar._selectedIndex = index;
});
}
@override
Widget build(BuildContext context) {
return BottomNavigationBar(
elevation: 0, // to get rid of the shadow
currentIndex: NavigationBar._selectedIndex,
selectedItemColor: Colors.amber[800],
onTap: _onItemTapped,
backgroundColor: Color(0x00ffffff), // transparent, you could use 0x44aaaaff to make it slightly less transparent with a blue hue.
type: BottomNavigationBarType.fixed,
unselectedItemColor: Colors.blue,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.grade),
label: 'Level',
),
BottomNavigationBarItem(
icon: Icon(Icons.notifications),
label: 'Notification',
),
BottomNavigationBarItem(
icon: Icon(Icons.school),
label: 'Achievements',
),
BottomNavigationBarItem(
icon: Icon(Icons.settings),
label: 'Settings',
),
]
);
}
@override
Size get preferredSize => const Size.fromHeight(kToolbarHeight);
}
Run Code Online (Sandbox Code Playgroud)
然后你有 MaterialApp 返回:
return MaterialApp(
home: Scaffold(
extendBody: true, // very important as noted
bottomNavigationBar: NavigationBar(), // here you make use of the transparent bar.
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: ExactAssetImage("assets/background.png"), // because if you want a transparent navigation bar I assume that you have either a background image or a background color. You need to add the image you want and also authorize it in pubspec.yaml
fit: BoxFit.fill
),
),
child: Container(
// the body of your app
),
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
我希望它会有所帮助。
Sna*_*ips 21
我尝试使用评论中讨论的 Stack 方法:
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Stack(
children: <Widget>[
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/background.jpg'),
fit: BoxFit.cover),
),
),
Align(
alignment: Alignment.bottomCenter,
child: Theme(
data: Theme.of(context)
.copyWith(canvasColor: Colors.transparent),
child: BottomNavigationBar(
currentIndex: 0,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home), title: Text('Home')),
BottomNavigationBarItem(
icon: Icon(Icons.home), title: Text('Home')),
BottomNavigationBarItem(
icon: Icon(Icons.home), title: Text('Home'))
],
))),
],
),
),
);
}
Run Code Online (Sandbox Code Playgroud)
编辑:它BottomNavigationBar有一个8.0你无法改变的内置高程,并导致了奇怪的阴影效果。如果你想删除它,你可以像这样实现你自己的底栏:
Align(
alignment: Alignment.bottomCenter,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
IconButton(icon: Icon(Icons.home, color: Theme.of(context).accentColor,), onPressed: () {},),
IconButton(icon: Icon(Icons.home, color: Theme.of(context).accentColor,), onPressed: () {},),
IconButton(icon: Icon(Icons.home, color: Theme.of(context).accentColor,), onPressed: () {},),
],)),
Run Code Online (Sandbox Code Playgroud)
这就是我实现这一目标的方式
return Scaffold(
body: Builder(
builder: (context) => Container(
decoration: bgAuthenticationDecoration(),
child: _HomeBodyWidget(_currentIndex),
),
),
bottomNavigationBar: BottomNavigationBar(items: <BottomNavigationBarItem>[
BottomNavigationBarItem(icon: Icon(Icons.home,),title: Container()),
BottomNavigationBarItem(icon: Icon(Icons.message),title: Container()),
BottomNavigationBarItem(icon: Icon(Icons.list),title: Container()),
BottomNavigationBarItem(icon: Icon(Icons.favorite),title: Container()),
BottomNavigationBarItem(icon: Icon(Icons.supervised_user_circle),title: Container()),
],
backgroundColor:Colors.black.withOpacity(0.1),),
extendBodyBehindAppBar: true,
extendBody: true,
);
Run Code Online (Sandbox Code Playgroud)
然后您必须在应用程序主题中将画布颜色设置为透明。
canvasColor: Colors.transparent
Run Code Online (Sandbox Code Playgroud)
希望这会有所帮助。
快乐编码!
| 归档时间: |
|
| 查看次数: |
4223 次 |
| 最近记录: |