Flutter 如何在另一个无状态小部件中导航BottomNavigationBar
我在一个无状态小部件中有一个底部导航栏。我正在使用ViewModelProvider控制onTap事件时的选项卡更改。使用BottomNavigationBar导航没有问题,但我无法从身体内部控制导航栏。我使用的是以下方法,我尝试了streambuilder来控制导航,但是当导航到另一个选项卡时,streambuilder将处理内容,这不是我想要的。我无法在个人资料页面中单击按钮并导航到主页 下面是我的BottomNavigationBar小部件Flutter 如何在另一个无状态小部件中导航BottomNavigationBar,flutter,dart,flutter-bottomnavigation,Flutter,Dart,Flutter Bottomnavigation,我在一个无状态小部件中有一个底部导航栏。我正在使用ViewModelProvider控制onTap事件时的选项卡更改。使用BottomNavigationBar导航没有问题,但我无法从身体内部控制导航栏。我使用的是以下方法,我尝试了streambuilder来控制导航,但是当导航到另一个选项卡时,streambuilder将处理内容,这不是我想要的。我无法在个人资料页面中单击按钮并导航到主页 下面是我的BottomNavigationBar小部件 class BottomNavigationVi
class BottomNavigationView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ViewModelProvider<BottomNavigationViewModel>.withConsumer(
viewModel: BottomNavigationViewModel(),
builder: (context, model, child) => Scaffold(
primary: false,
body: IndexedStack(
children: <Widget>[
BrowseView(),
HomeView(),
ProfileView(),
],
index: model.currentIndex,
),
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
backgroundColor: Colors.white,
currentIndex: model.currentIndex,
onTap: model.changeView,
items: [
BottomNavigationBarItem(
icon: new Icon(Icons.search),
title: SizedBox.shrink(),
),
BottomNavigationBarItem(
icon: new Icon(Icons.home),
title: SizedBox.shrink(),
),
BottomNavigationBarItem(
icon: new Icon(WineIcon.person),
title: SizedBox.shrink(),
),
],
),
),
);
}
}
纵断面图和纵断面图模型
class BottomNavigationViewModel extends ChangeNotifier {
int _currentIndex = 2;
int get currentIndex => _currentIndex;
void changeView(int index) {
_currentIndex = index;
notifyListeners();
}
}
class ProfileView extends StatelessWidget {
const ProfileView ({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return ViewModelProvider<ProfileViewModel>.withConsumer(
viewModel: ProfileViewModel(),
builder: (context, model, child) => Scaffold(
body: Center(
child: RaisedButton(
onPressed: ()=> model.goHome(),
child:Text('Go to Home'),
),
)
),
);
}
}
class ProfileViewModel extends ChangeNotifier {
final BottomNavigationViewModel _bottomNavigationBar =
locator<BottomNavigationViewModel>();
void goHome() {
_bottomNavigationBar.changeView(1);
}
}
class ProfileView扩展了无状态小部件{
const ProfileView({Key}):super(Key:Key);
@凌驾
小部件构建(构建上下文){
返回ViewModelProvider.withConsumer(
viewModel:ProfileViewModel(),
生成器:(上下文、模型、子项)=>Scaffold(
正文:中(
孩子:升起按钮(
onPressed:()=>model.goHome(),
孩子:文本(“回家”),
),
)
),
);
}
}
类ProfileViewModel扩展了ChangeNotifier{
最终底部导航视图模型_底部导航栏=
定位器();
void goHome(){
_bottomNavigationBar.changeView(1);
}
}
不完全理解“无法从身体内部控制导航栏”的含义。但我想你的意思是,当你在每个页面中导航时,btmnav就消失了?使用Indexstack将页面的状态保持在内部,已经完成了一半。我的部分没有使用streambuilder,但对于每个页面的导航器,创建一个要使用的全局键
Navigator(
key: 1 key for each tab here,
onGenerateRoute: (settings) => MaterialPageRoute(
settings: settings,
builder: (context) => page,
), // this will generate your pages
如果你想替换整个页面,那么
Navigator.of(context,rootNavigator: true).push(MaterialPageRoute(builder: (context) =>anotherpage)
否则只需将rootNavigator设置为false不完全理解“无法从身体内部控制导航栏”的含义。但我想你的意思是,当你在每个页面中导航时,btmnav就消失了?使用Indexstack将页面的状态保持在内部,已经完成了一半。我的部分没有使用streambuilder,但对于每个页面的导航器,创建一个要使用的全局键
Navigator(
key: 1 key for each tab here,
onGenerateRoute: (settings) => MaterialPageRoute(
settings: settings,
builder: (context) => page,
), // this will generate your pages
如果你想替换整个页面,那么
Navigator.of(context,rootNavigator: true).push(MaterialPageRoute(builder: (context) =>anotherpage)
否则,只需将rootNavigator设置为false我的意思是,我喜欢在底部导航栏中导航,而不必在导航栏上进行切换。我喜欢通过页面中的一个按钮来导航,我的意思是我喜欢在底部导航栏中导航,而不必在导航栏上打标签。我喜欢通过页面内的按钮来导航。