Flutter 如何在颤振中同时创建抽屉和底部导航条?
我到处寻找,但我找不到如何使用抽屉和底部导航栏一起在颤振 我创建的两个ui都显示良好。主要问题是我可以在body中导航抽屉的items类或底部导航栏类。如果单击了底部项目,则应加载底部类,当抽屉的项目被单击时,则应加载其类 我的代码在下面,请查看代码,如果有任何错误或建议我的任何例子。提前谢谢Flutter 如何在颤振中同时创建抽屉和底部导航条?,flutter,bottomnavigationview,drawer,Flutter,Bottomnavigationview,Drawer,我到处寻找,但我找不到如何使用抽屉和底部导航栏一起在颤振 我创建的两个ui都显示良好。主要问题是我可以在body中导航抽屉的items类或底部导航栏类。如果单击了底部项目,则应加载底部类,当抽屉的项目被单击时,则应加载其类 我的代码在下面,请查看代码,如果有任何错误或建议我的任何例子。提前谢谢 ''' //for setting title name of side bar items class DrawerItem { String title; Ic
'''
//for setting title name of side bar items
class DrawerItem {
String title;
IconData icon;
DrawerItem(this.title, this.icon);
}
//for setting title name of bottom bar items
// class widgetBottomItems{
// String title;
// IconData icon;
// widgetBottomItems(this.title, this.icon);
// }
class DrawerSetting extends StatefulWidget {
final drawerItems = [
new DrawerItem("Add Manual FD", Icons.add),
new DrawerItem("Zero Balance Portfolio", Icons.alarm),
new DrawerItem("Message", Icons.message),
new DrawerItem("My Info & Setting", Icons.settings),
new DrawerItem("Support", Icons.help),
new DrawerItem("FAQ", Icons.book_outlined),
new DrawerItem("Contact Us", Icons.contact_phone_outlined),
new DrawerItem("Logout", Icons.logout)
];
@override
State<StatefulWidget> createState() {
return new HomePageState();
}
}
class HomePageState extends State<DrawerSetting> {
final Color myColor = Color(0xFF15A2EB);
final Color myColorLightGray = Color(0xFF989E9E);
final Color text_Semi_Black_Color = Color(0xFF414B51);
final Color text_gray_color = Color(0xFF70787C);
int _selectedDrawerIndex = 0;
//bottom navigation
int _selectedIndex = 0;
var _screens = [
DiscoverScreen(),
HomeFragment(),
PartnersScreen(),
// HistoryScreen(),
AppHistoryTabScreen(),
];
static const TextStyle optionStyle = TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
color: Colors.red,
fontFamily: "verdana_regular",
);
static const List<Widget> widgetBottomItems = <Widget>[
Text(
'Discover',
style: optionStyle,
),
Text(
'Portfolio',
style: optionStyle,
),
Text(
'Partners',
style: optionStyle,
),
Text(
'History',
style: optionStyle,
),
];
String currentProfilePic =
"https://service2home.in/wp-content/uploads/2021/01/rakesh.jpg";
_getDrawerItemWidget(int pos) {
switch (pos) {
case 0:
return new AddManualFdFragment();
break;
case 1:
return new ZeroBalancePortFolioScreen();
break;
case 2:
return MessageFragment();
break;
case 3:
return new UserProfileScreen("drawer");
break;
case 4:
return new SupportFragment();
break;
case 5:
return new FAQFragment();
break;
case 6:
return new ContactUsScreen();
break;
case 7:
SystemNavigator.pop();
//exit(0);
break;
}
}
//for bottom items
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
//calling both items
// _onSelectItem(index);
});
}
//for drawer items
_onSelectItem(int index) {
setState(() => _selectedDrawerIndex = index);
Navigator.of(context).pop(); // close the drawer
}
@override
Widget build(BuildContext context) {
List<Widget> drawerOptions = [];
for (var i = 0; i < widget.drawerItems.length; i++) {
var d = widget.drawerItems[i];
drawerOptions.add(new ListTile(
leading: new Icon(
d.icon,
),
title: new Text(
d.title,
style: TextStyle(
fontSize: 16,
color: myColorLightGray,
fontFamily: "verdana_regular",
),
),
selected: i == _selectedDrawerIndex,
onTap: () => _onSelectItem(i),
));
}
//on backpress return to home screen , you can use it for drawer items
return WillPopScope(
onWillPop: () {
if (_selectedIndex != 0) {
setState(() {
_selectedIndex = 0;
});
_getDrawerItemWidget(_selectedIndex);
} else {
Navigator.pop(context, true);
}
return;
},
child: Scaffold(
appBar: new AppBar(
title: new Text(
widget.drawerItems[_selectedDrawerIndex].title,
style: TextStyle(
color: Colors.white,
fontSize: 16,
fontFamily: "verdana_regular"),
),
),
drawer: new Drawer(
child: SingleChildScrollView(
child: new Column(
children: [
UserAccountsDrawerHeader(
currentAccountPicture: Container(
child: new GestureDetector(
child: new CircleAvatar(
backgroundImage: new NetworkImage(currentProfilePic),
),
onTap: () => Navigator.push(
context,
MaterialPageRoute(
builder: (context) => UserProfileScreen("header")),
),
),
),
accountName: new Text(
"Rakesh saini",
style: TextStyle(
fontSize: 18,
color: Colors.black,
fontFamily: "verdana_regular",
),
),
accountEmail: new Text(
"rakesh.ollosoft@gmail.com",
style: TextStyle(
fontSize: 15,
color: Colors.white,
fontFamily: "verdana_regular",
),
),
),
Column(children: drawerOptions)
],
),
),
),
body: /*_screens[_selectedIndex]*/
_getDrawerItemWidget(_selectedDrawerIndex),
//bottom navigation bar
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
backgroundColor: myColor,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.search),
label: 'Discover',
),
BottomNavigationBarItem(
icon: ImageIcon(
AssetImage("assets/images/briefcase.png"),
// color: Colors.grey,
),
label: 'Portfolio',
),
BottomNavigationBarItem(
icon: ImageIcon(
AssetImage("assets/images/handshake.png"),
// color: Colors.grey,
),
label: 'Partners',
),
BottomNavigationBarItem(
icon: Icon(Icons.history),
label: 'History',
),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.white,
unselectedItemColor: myColorLightGray,
onTap: _onItemTapped,
),
// bottomNavigationBar: BottomNavigationBar(),
),
);
}
//go to news detail page
void goToProfile_Screen(BuildContext ctx) {
Navigator.of(ctx).push(
MaterialPageRoute(
builder: (_) {
return UserProfileScreen("drawer");
},
),
);
}
}
'''
“”
//用于设置侧栏项目的标题名称
阶级仪式{
字符串标题;
Iconda图标;
DroperItem(this.title,this.icon);
}
//用于设置底部栏项目的标题名称
//类widgetBottomItems{
//字符串标题;
//Iconda图标;
//widgetBottomItems(this.title、this.icon);
// }
类DrawerSetting扩展了StatefulWidget{
最终付款日期=[
新的抽屉指令(“添加手动FD”,Icons.Add),
新的付款方式(“零余额投资组合”,图标。报警),
新的抽屉主题(“消息”,图标。消息),
新的DroperItem(“我的信息和设置”,图标。设置),
新的DroperItem(“支持”,Icons.help),
新的DroperItem(“常见问题”,图标,书籍概述),
新的付款方式(“联系我们”,图标。联系电话),
新的DroperItem(“注销”,图标。注销)
];
@凌驾
状态createState(){
返回新的HomePageState();
}
}
类HomePageState扩展了状态{
最终颜色myColor=颜色(0xFF15A2EB);
最终颜色myColorLightGray=颜色(0xFF989E9E);
最终颜色文本\半\黑色\颜色=颜色(0xFF414B51);
最终颜色文本\灰色\颜色=颜色(0xFF70787C);
int _selectedDrawerIndex=0;
//底部导航
int _selectedIndex=0;
变量_屏幕=[
DiscoverScreen(),
HomeFragment(),
PartnersScreen(),
//历史屏幕(),
AppHistoryTabScreen(),
];
静态常量TextStyle选项style=TextStyle(
尺寸:16,
fontWeight:fontWeight.bold,
颜色:颜色,红色,
fontFamily:“verdana_regular”,
);
静态常量列表widgetBottomItems=[
正文(
“发现”,
样式:可选样式,
),
正文(
“投资组合”,
样式:可选样式,
),
正文(
“合伙人”,
样式:可选样式,
),
正文(
"历史",,
样式:可选样式,
),
];
字符串currentProfilePic=
"https://service2home.in/wp-content/uploads/2021/01/rakesh.jpg";
_getDrawerItemWidget(内部位置){
开关(pos){
案例0:
返回新的AddManualFdFragment();
打破
案例1:
返回新的ZeroBalancePortFolioScreen();
打破
案例2:
返回MessageFragment();
打破
案例3:
返回新的UserProfileScreen(“抽屉”);
打破
案例4:
返回新的SupportFragment();
打破
案例5:
返回新的FAQFragment();
打破
案例6:
返回新联系人屏幕();
打破
案例7:
SystemNavigator.pop();
//出口(0);
打破
}
}
//对于底部项目
void\u未映射(整数索引){
设置状态(){
_selectedIndex=索引;
//调用两个项目
//(索引);;
});
}
//抽屉物品
_onSelectItem(整数索引){
设置状态(()=>_selectedDrawerIndex=索引);
Navigator.of(context.pop();//关闭抽屉
}
@凌驾
小部件构建(构建上下文){
列表抽屉选项=[];
对于(var i=0;i(i),
));
}
//在反按返回主屏幕时,您可以将其用于抽屉项目
返回式示波器(
onWillPop:(){
如果(_selectedIndex!=0){
设置状态(){
_selectedIndex=0;
});
_getDrawerItemWidget(_selectedIndex);
}否则{
pop(上下文,true);
}
返回;
},
孩子:脚手架(
appBar:新的appBar(
标题:新文本(
widget.drawerItems[\u selectedDrawerIndex].title,
样式:TextStyle(
颜色:颜色,白色,
尺寸:16,
fontFamily:“verdana_regular”),
),
),
抽屉:新抽屉(
子:SingleChildScrollView(
子:新列(
儿童:[
UserAccountsDrawerHeader(
currentAccountPicture:容器(
儿童:新的手势检测器(
孩子:新CircleAvatar(
背景图片:新网络图片(currentProfilePic),
),
onTap:()=>Navigator.push(
上下文
材料路线(
生成器:(上下文)=>UserProfileScreen(“标题”)
Scaffold(
key: _scaffoldKey,
bottomNavigationBar: FooterNavigationBar(),
drawer: AppDrawer(
scaffoldKey: _scaffoldKey,
),
backgroundColor: CBase().backgroundColor,
body: ......
InkWell(
onTap: () {
_scaffoldKey.currentState.openDrawer();
},
child: SvgPicture.asset(
"images/DrawerIcon.svg"),
),
...............