Dart 颤振导航问题
材质设计不建议层次结构中的子页面访问底部导航栏。因此,没有明确的方法在所有屏幕中统一实现相同的底部导航器。问题是,一旦我移动到子页面,相同的底部导航器应该适用于所有我不确定你的意思 用于访问底部导航栏的层次结构中的子页面 但是,如果您试图对多个页面使用相同的Dart 颤振导航问题,dart,flutter,Dart,Flutter,材质设计不建议层次结构中的子页面访问底部导航栏。因此,没有明确的方法在所有屏幕中统一实现相同的底部导航器。问题是,一旦我移动到子页面,相同的底部导航器应该适用于所有我不确定你的意思 用于访问底部导航栏的层次结构中的子页面 但是,如果您试图对多个页面使用相同的BottomNavigationBar,请尝试以下操作: import "package:flutter/material.dart"; void main() { runApp(new MaterialApp( home: n
BottomNavigationBar
,请尝试以下操作:
import "package:flutter/material.dart";
void main() {
runApp(new MaterialApp(
home: new Example(),
));
}
class Example extends StatefulWidget {
@override
ExampleState createState() => new ExampleState();
}
class ExampleState extends State<Example> {
int currentTab = 0; // Index of currently opened tab.
PageOne pageOne = new PageOne(); // Page that corresponds with the first tab.
PageTwo pageTwo = new PageTwo(); // Page that corresponds with the second tab.
PageThree pageThree = new PageThree(); // Page that corresponds with the third tab.
List<Widget> pages; // List of all pages that can be opened from our BottomNavigationBar.
// Index 0 represents the page for the 0th tab, index 1 represents the page for the 1st tab etc...
Widget currentPage; // Page that is open at the moment.
@override
void initState() {
super.initState();
pages = [pageOne, pageTwo, pageThree]; // Populate our pages list.
currentPage = pageOne; // Setting the first page that we'd like to show our user.
// Notice that pageOne is the 0th item in the pages list. This corresponds with our initial currentTab value.
// These two should match at the start of our application.
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
// Here we create our BottomNavigationBar.
final BottomNavigationBar navBar = new BottomNavigationBar(
currentIndex: currentTab, // Our currentIndex will be the currentTab value. So we need to update this whenever we tab on a new page!
onTap: (int numTab) { // numTab will be the index of the tab that is pressed.
setState(() { // Setting the state so we can show our new page.
print("Current tab: " + numTab.toString()); // Printing for debugging reasons.
currentTab = numTab; // Updating our currentTab with the tab that is pressed [See 43].
currentPage = pages[numTab]; // Updating the page that we'd like to show to the user.
});
},
items: <BottomNavigationBarItem>[ // Visuals, see docs for more information: https://docs.flutter.io/flutter/material/BottomNavigationBar-class.html
new BottomNavigationBarItem( //numTab 0
icon: new Icon(Icons.ac_unit),
title: new Text("Ac unit")
),
new BottomNavigationBarItem( //numTab 1
icon: new Icon(Icons.access_alarm),
title: new Text("Access alarm")
),
new BottomNavigationBarItem( //numTab 2
icon: new Icon(Icons.access_alarms),
title: new Text("Access alarms")
)
],
);
return new Scaffold(
bottomNavigationBar: navBar, // Assigning our navBar to the Scaffold's bottomNavigationBar property.
body: currentPage, // The body will be the currentPage. Which we update when a tab is pressed.
);
}
}
class PageOne extends StatelessWidget { // Creating a simple example page.
@override
Widget build(BuildContext context) {
return new Center(child: new Text("Page one"));
}
}
class PageTwo extends StatelessWidget { // Creating a simple example page.
@override
Widget build(BuildContext context) {
return new Center(child: new Text("Page two"));
}
}
class PageThree extends StatelessWidget { // Creating a simple example page.
@override
Widget build(BuildContext context) {
return new Center(child: new Text("Page three"));
}
}
导入“包装:颤振/材料.省道”;
void main(){
runApp(新材料)PP(
主页:新示例(),
));
}
类示例扩展StatefulWidget{
@凌驾
ExampleState createState()=>新建ExampleState();
}
类ExampleState扩展了状态{
int currentTab=0;//当前打开的选项卡的索引。
PageOne PageOne=新建PageOne();//与第一个选项卡对应的页面。
PageTwo PageTwo=new PageTwo();//与第二个选项卡相对应的页面。
PageThree PageThree=新的PageThree();//与第三个选项卡对应的页面。
列出页面;//可以从底部导航栏打开的所有页面的列表。
//索引0表示第0个选项卡的页面,索引1表示第1个选项卡的页面等。。。
Widget currentPage;//当前打开的页面。
@凌驾
void initState(){
super.initState();
pages=[pageOne,PageII,pageThree];//填充我们的页面列表。
currentPage=pageOne;//设置要向用户显示的第一页。
//请注意,pageOne是页面列表中的第0项。这与我们的初始currentTab值相对应。
//这两个应该在应用程序开始时匹配。
}
@凌驾
无效处置(){
super.dispose();
}
@凌驾
小部件构建(构建上下文){
//在这里,我们创建底部导航栏。
最终底部导航栏导航栏=新底部导航栏(
currentIndex:currentTab,//我们的currentIndex将是currentTab值。因此,每当我们在新页面上进行tab时,我们都需要更新它!
onTap:(int numTab){//numTab将是所按选项卡的索引。
setState((){//设置状态以便显示新页面。
打印(“当前选项卡:+numTab.toString());//出于调试原因打印。
currentTab=numTab;//使用按下的选项卡更新currentTab[请参阅43]。
currentPage=pages[numTab];//更新我们要向用户显示的页面。
});
},
项目:[//视觉效果,有关详细信息,请参阅文档:https://docs.flutter.io/flutter/material/BottomNavigationBar-class.html
新的BottomNavigationBarItem(//numTab 0
图标:新图标(图标。空调单元),
标题:新文本(“Ac单元”)
),
新的BottomNavigationBarItem(//numTab 1
图标:新图标(图标访问报警),
标题:新文本(“访问警报”)
),
新的BottomNavigationBarItem(//numTab 2
图标:新图标(图标。访问报警),
标题:新文本(“访问警报”)
)
],
);
归还新脚手架(
bottomNavigationBar:navBar,//将我们的navBar分配给Scaffold的bottomNavigationBar属性。
body:currentPage,//body将是currentPage。当按下选项卡时,我们会更新它。
);
}
}
类PageOne扩展了无状态小部件{//创建了一个简单的示例页面。
@凌驾
小部件构建(构建上下文){
返回新中心(子项:新文本(“第一页”);
}
}
类PageTwo扩展了无状态小部件{//创建了一个简单的示例页面。
@凌驾
小部件构建(构建上下文){
返回新中心(子项:新文本(“第二页”);
}
}
类PageThree扩展了无状态小部件{//创建了一个简单的示例页面。
@凌驾
小部件构建(构建上下文){
返回新中心(子项:新文本(“第三页”);
}
}
如果这不是想要的效果,请详细说明。非常感谢!这正是我要找的@Pushkarkumar如果您对这个答案感到满意,请将它标记为解决方案好吗?如果其中一个页面有子页面,如何保持底部栏?例如,假设第1页有一个指向第5页的链接