所以我設定了一個底部導航欄來在 5 個不同的頁面之間切換,這些頁面分為 5 個不同的檔案,第 6 個作為實際的導航欄頁面。問題是,我什至無法使用路由或命名路由來更改頁面。
這是我在主頁中的代碼。
import 'package:flutter/material.dart';
import 'package:fluttericon/typicons_icons.dart';
import 'package:luxview/Custom/Themes.dart';
import 'package:luxview/Pages/Messages.dart';
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int _currentIndex = 0;
Widget _currentWidget = Container();
@override
void initState() {
super.initState();
_Screen();
}
void _Screen() {
// Area where I tried to set up screen switching
switch (_currentIndex) {
case 0:
Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => const MessagePage()));
//case 1:
// Themes
//case 2:
// Plugins
//case 3:
// Profile
//case 4:
// Settings
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: _currentWidget,
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currentIndex,
onTap: (index) {
setState(() => _currentIndex = index);
_Screen();
},
backgroundColor: bottomnavbar_bgc,
selectedItemColor: bottomnavbar_selcol,
unselectedItemColor: Colors.blue,
type: BottomNavigationBarType.fixed,
items: const [
BottomNavigationBarItem(
icon: Icon(Icons.message),
label: "Messages",
),
BottomNavigationBarItem(icon: Icon(Icons.brush), label: "Themes"),
BottomNavigationBarItem(
icon: Icon(Typicons.puzzle), label: "Plugins"),
BottomNavigationBarItem(
icon: Icon(Icons.account_box), label: "Profile"),
BottomNavigationBarItem(
icon: Icon(Icons.settings), label: "Settings"),
]),
);
}
}
這就是我在 messages.dart 檔案中的內容,以防萬一。
import 'package:flutter/material.dart';
import 'package:luxview/Custom/Themes.dart';
class MessagePage extends StatefulWidget {
const MessagePage({super.key});
@override
_MessagePageState createState() => _MessagePageState();
}
class _MessagePageState extends State<MessagePage> {
@override
Widget build(BuildContext context) {
return Container(
color: universal_bgc,
);
}
}
uj5u.com熱心網友回復:
如果您只想更新頁面。
class MainPage extends StatefulWidget {
const MainPage({super.key});
@override
_MainPageState createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
int _currentIndex = 0; //keep track of current selected index.
@override
Widget build(BuildContext context) {
Widget selectedWidget = Home();
switch (_currentIndex) {
case 0:
selectedWidget = Home();
break;
case 1:
selectedWidget = Page2();
break;
...
}
return Scaffold(
body: selectedWidget,
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currentIndex,
onTap: (index) {
setState(() => _currentIndex = index);
},
...
),
);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/529218.html
標籤:扑镖用户界面
上一篇:如何從QWidget類中的QMainWindow類訪問變數和方法-PythonPyQt5
下一篇:在Kivy中每秒顯示一個亂數
