編輯:解決方案 我使用了包custom_navigator
在導航欄中,我有 2 頁要重定向,但我想導航到第三頁,并且仍然希望在那里看到導航欄(這個有 2 頁)。
有可能嗎?我必須為此頁面制作自己的導航欄嗎?
class Bar extends StatefulWidget {
@override
BarState createState() => BarState();
}
class BarState extends State<Bar> {
int tabIndex = 0;
List<Widget> pages = [
FirstPage(),
SecondPage(),
];
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: PreferredSize(
preferredSize: Size.fromHeight(kToolbarHeight),
child: SafeArea(
child: BottomNavigationBar(
iconSize: 25,
elevation: 4.0,
items: <BottomNavigationBarItem>[
barItem(Icons.message),
barItem(Icons.camera_enhance),
barItem(Icons.person),
],
currentIndex: tabIndex,
onTap: (int index) {
setState(() {
tabIndex = index;
});
},
),
)),
body: Container(
child: pages.elementAt(tabIndex),
),
);
}
}
這就是我嘗試的:
List<Widget> pages = [
Container(
color: Colors.green,
child: Center(
child: ElevatedButton(
onPressed: state
),
),
),
SecondPage(),
ThirdPage()
];
state() {
tabIndex = 2;
setState(() {
});
}
uj5u.com熱心網友回復:
最簡單的方法,如果你不介意它的影片是AppBar在你的導航器中初始化一個并將它傳遞給頁面,他們會在那里的腳手架中使用它。
class MyFlow extends StatefulWidget {
const MyFlow({Key? key}) : super(key: key);
@override
_MyFlowState createState() => _MyFlowState();
}
class _MyFlowState extends State<MyFlow> {
@override
Widget build(BuildContext context) {
final appBar = AppBar();
return Navigator(
onPopPage: (route, result) => true,
pages: [
MaterialPage(child: PageOne(appBar: appBar)),
MaterialPage(child: PageTwo(appBar: appBar)),
MaterialPage(child: PageThree(appBar: appBar)),
],
);
}
}
class PageOne extends StatelessWidget {
const PageOne({Key? key, required this.appBar}) : super(key: key);
final AppBar appBar;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: appBar,
);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/314889.html
