【发布时间】:2022-01-22 15:47:15
【问题描述】:
在颤振中使用底部导航栏中的三个图标时,我想移动到三个新页面(根据我下面的代码)。我尝试了几种方法,但代码不起作用,也无法按照我的要求弄清楚。
在这种情况下,最重要的是使用现有代码并使用 bottomNavigationBar 更改移动到新页面所需的更改。
如果有人知道如何使用 bottomNavigationBar(使用我现有的代码)导航到新页面,请告诉我。
提前谢谢你。
代码:
class dashboard extends StatefulWidget {
@override
_dashboardState createState() => _dashboardState();
}
// ignore: camel_case_types
class _dashboardState extends State<dashboard> {
int currentIndex = 1;
changeIndex(index) {
setState(() {
currentIndex = index;
});
}
@override
Widget build(BuildContext context) {
final authService = Provider.of<AuthService>(context);
return Scaffold(
body: SingleChildScrollView(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 80.0, right: 250),
child: Center(
child: Container(
width: 200.0,
height: 20.0,
decoration:
BoxDecoration(borderRadius: BorderRadius.circular(15.0)),
child: (const Text(
'Hello',
textAlign: TextAlign.center,
style: TextStyle(
fontWeight: FontWeight.bold, color: Colors.black),
)),
),
),
),
Padding(
padding: EdgeInsets.only(left: 300.0, top: 1.0),
child: IconButton(
icon: new Icon(Icons.account_circle, size: 30.0),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => HomePage(),
),
);
},
),
),
Padding(
padding: EdgeInsets.only(left: 300.0, top: 5.0),
child: IconButton(
icon: const Icon(
Icons.notifications,
size: 25.0,
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Notifications(),
),
);
},
),
),
Padding(
padding: const EdgeInsets.only(top: 0.0),
child: Center(
child: Container(
width: 390,
height: 450,
decoration: BoxDecoration(
color: Colors.green.shade100,
borderRadius: BorderRadius.circular(10.0)),
),
),
),
],
),
),
floatingActionButton: FloatingActionButton(onPressed: () async {
await authService.signOut();
}),
// : _children[_currentIndex],
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
backgroundColor: Colors.green[100],
items: const [
BottomNavigationBarItem(
//I want to navigate to a new page Library();
icon: Icon(Icons.book_online),
label: 'Library',
),
BottomNavigationBarItem(
//I want to navigate to a new page Store();
icon: Icon(Icons.read_more),
label: 'Store',
),
BottomNavigationBarItem(
//I want to navigate to a new page Profile();
icon: Icon(Icons.account_circle),
label: 'Profile',
),
],
),
);
}
}
【问题讨论】:
-
我检查了答案,它奏效了。但是现在导航到相关页面后,我无法从新页面移回上一页(仪表板)。这有什么原因吗?我尝试使用其他方式移至其他页面,并且能够移回上一页。但是现在我在使用底部导航栏移动到新页面后无法返回上一页。有什么原因吗?
-
谢谢。它现在起作用了。我使用
if (index == 2) { Navigator.of(context).push( new MaterialPageRoute( builder: (context) => HomePage(), ), ); }而不是if (index == 2) { Navigator.of(context).pushReplacement( new MaterialPageRoute( builder: (context) => HomePage(), ), ); }它现在按要求工作。非常感谢!
标签: flutter dart flutter-bottomnavigation flutter-routes