gpt4 book ai didi

Flutter:如何切换到已经在 Navigator 堆栈中的页面?

转载 作者:行者123 更新时间:2023-12-05 07:18:24 26 4
gpt4 key购买 nike

我有一个抽屉,其中包含指向不同搜索页面的命名路由。推送路线导航到搜索页面。搜索页面中的每个项目都会导航到详细信息页面。后退按钮弹出详细信息页面,我又回到了搜索页面。此场景正确使用了导航器堆栈。

但是如何在多个搜索页面之间切换呢?我只能弹出一个搜索页面,然后再推送另一个。

我正在寻找的另一个用例是在详细信息页面之间切换。在这种情况下,导航器将包含(按此顺序):第一个搜索页面、第一个详细信息页面、第二个搜索页面、第二个详细信息页面。我想在两个详细信息页面之间切换。

有什么方法可以查看 Navigator 堆栈并将查看的条目带到堆栈的顶部?

多个导航器在这里有帮助吗?我可以在导航器之间切换吗?

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

GlobalKey<NavigatorState> navKey = GlobalKey( );

void main( ) => runApp( MyApp( ) );

class MyApp extends StatelessWidget {

final Provider<MyMenu> menuProvider = Provider(
builder: ( context ) => MyMenu( ) );

@override
Widget build( BuildContext context ) {
return MultiProvider(
child: MaterialApp(
title: 'My App',
home: MyHomePage( ),
navigatorKey: navKey,
routes: {
routePage1: ( context ) => Page1( ),
routePage2: ( context ) => Page2( ),
},
theme: ThemeData( primarySwatch: Colors.blue, ),
),
providers: [
menuProvider,
], );
}
}

class MyMenu extends StatelessWidget {

@override
Widget build( BuildContext context ) {
String currentRoute = getCurrentRouteName( );

return Drawer( child: ListView( children: <Widget>[
ListTile(
leading: Icon( Icons.home ),
onTap: ( ) => navigateTo( context, routePage1 ),
selected: currentRoute == routePage1,
title: Text( "Page 1" ),
),
ListTile(
onTap: ( ) => navigateTo( context, routePage1 ),
selected: currentRoute == routePage1,
title: Text( "Page 1" ),
),
ListTile(
onTap: ( ) => navigateTo( context, routePage2 ),
selected: currentRoute == routePage2,
title: Text( "Page 2" ),
),
],
),
);
}
}

String getCurrentRouteName( ) {
String currentRouteName;
navKey.currentState.popUntil( ( route ) {
currentRouteName = route.settings.name;
return true;
} );
return currentRouteName;
}

void navigateTo( BuildContext context, String namedRoute, { Object arguments } ) {
// --- Close drawer menu if open
if ( Scaffold
.of( context )
.isDrawerOpen ) {
navKey.currentState.pop( );
}

// --- No navigation if target page already active
String currentRoute = getCurrentRouteName( );
if ( currentRoute == namedRoute ) return;

// --- Navigate to target
if ( currentRoute == "/" ) {
navKey.currentState.pushNamed( namedRoute, arguments: arguments );
} else {
navKey.currentState.popAndPushNamed( namedRoute, arguments: arguments );
}
}

class MyAppBar extends StatelessWidget implements PreferredSizeWidget {
@override
Widget build( BuildContext context ) {
String currentRoute = getCurrentRouteName( );
return AppBar(
leading: MenuButton( ),
title: Text( 'my app -> ${currentRoute}' ), );
}

@override
Size get preferredSize => Size.fromHeight( kToolbarHeight );
}

class MenuButton extends StatelessWidget {
@override
Widget build( BuildContext context ) {
return IconButton(
icon: Icon( Icons.menu ),
onPressed: ( ) {
ScaffoldState scaffold = Scaffold.of( context );
if ( scaffold.isDrawerOpen ) {
navKey.currentState.pop( );
} else {
scaffold.openDrawer( );
}
},
);
}
}

class MyHomePage extends StatelessWidget {
@override
Widget build( BuildContext context ) {
return Scaffold(
appBar: MyAppBar( ),
body: Center(
child: Text( "This is the Homepage" ),
),
drawer: Provider.of<MyMenu>( context ),
);
}
}

const String routePage1 = '/page1';

class Page1 extends StatelessWidget {
@override
Widget build( BuildContext context ) {
return Scaffold(
appBar: MyAppBar( ),
body: Center(
child: Text( "This is Page 1" ),
),
drawer: Provider.of<MyMenu>( context ),
);
}
}

const String routePage2 = '/page2';

class Page2 extends StatelessWidget {
@override
Widget build( BuildContext context ) {
return Scaffold(
appBar: MyAppBar( ),
body: Center(
child: Text( "This is Page 2" ),
),
drawer: Provider.of<MyMenu>( context ),
);
}
}

最佳答案

创建一个字段类型的小部件,并像下面的代码一样替换菜单项上的那些小部件。

 enum DrawerSelection {
home,
cropFamilies,
cropCalender,
favorites,
settings,
notes,
contactUs,
disclaimer
}

class _MyHomePage extends State<MyHomePage> {

DrawerSelection _drawerSelection = DrawerSelection.home;
Widget _appBarTitle = Text("Home");


Widget _currentWidget = MainWidget();

ListTile(
selected: _drawerSelection == DrawerSelection.home,
title: Text('Home'),
leading: Icon(Icons.home),
onTap: () {
_drawerSelection = DrawerSelection.home;
Navigator.pop(context);
_currentWidget = MainWidget();
setState(() {
_appBarTitle = Text("Home");
});
},
),
ListTile(
selected: _drawerSelection == DrawerSelection.cropFamilies,
title: Text('Crop families فصل کا خاندان'),
leading: Icon(Icons.nature),
onTap: () {
_drawerSelection = DrawerSelection.cropFamilies;
Navigator.pop(context);
_currentWidget = CropFamiliesWidget();
setState(() {
_appBarTitle = Text("Crop families فصل کا خاندان");
});
},
),

在所有菜单项中执行此操作。如果您不想在抽屉菜单中显示所选颜色,则可以忽略抽屉选择。

更新如果您想在堆栈中导航回所需的路线,您可以使用如下所示的 popUntil 方法来完成。 see here

  Navigator.popUntil(context, ModalRoute.withName('/login'));

关于Flutter:如何切换到已经在 Navigator 堆栈中的页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58296419/

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