gpt4 book ai didi

dart - flutter : Open Page from page inside bottomnavigationview

转载 作者:IT王子 更新时间:2023-10-29 07:23:59 24 4
gpt4 key购买 nike

我想从主页打开个人资料页面,而无需按 bottomnavigation 项目中的“个人资料”按钮。感谢帮助。这是我项目的主要类(class)。我将所有页面都放在这里并且我已经将所有页面导入这里。

主类

int _currentIndex = 0;
final List<Widget> _children = [
HomePage(),
MessagePage(),
ProfilePage()
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('My Flutter App'),
),
body: _children[_currentIndex],
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currentIndex,
onTap: onTabTapped,
items: [
BottomNavigationBarItem(
icon: new Icon(Icons.home),
title: new Text('Home'),

),
BottomNavigationBarItem(
icon: new Icon(Icons.mail),
title: new Text('Messages'),
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
title: Text('Profile')
)
],
),
);
}

这是我的主页。此页面与个人资料和消息页面相同。我需要从主页打开个人资料页面,而无需按底部的个人资料菜单。我只想按主页中的配置文件按钮。

首页

import 'package:flutter/material.dart';
import 'package:navigation/profile_page.dart';

class HomePage extends StatefulWidget {
_HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
children: <Widget>[
Text("Home Page"),
RaisedButton(
child: Text("GO TO PROFILE PAGE"),
onPressed: (){

);
},
)
],
),
),
);
}
}

enter image description here

最佳答案

这可以通过直接调用类名来完成。例如,将 Profile() 视为扩展有状态类的类。

家课

....

int _index = 1;

final options = [
Text('Home'),
Text('Message'),
Profile() //directly call the class to load
];

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("My flutter app"),
),
body: Center(
child: options.elementAt(_index),
),
bottomNavigationBar: BottomNavigationBar(
items: [
new BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Home'),
),
new BottomNavigationBarItem(
icon: Icon(Icons.message),
title: Text('message'),
),
new BottomNavigationBarItem(
icon: Icon(Icons.person),
title: Text('profile')
)
],
currentIndex: _index,
onTap: _onTapped,
),
);
}

void _onTapped(int index) {
setState(() {
_index = index;
});
}

同样,您也可以加载其他类。希望对您有所帮助:)

关于dart - flutter : Open Page from page inside bottomnavigationview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53993764/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com