在名为 flutter 的 pushreplacement 中传递参数
passing arguments in pushreplacement named flutter
我正在尝试在我的 flutter 项目中使用 CurveBottomNavigatorBar...
我在主飞镖中使用了我的曲线...,如下所示:
class _BottomNavigatorBarState extends State<BottomNavigatorBar> {
int selectedIndex = 0;
final screen = [
HomeScreen(),
CartScreen(),
MyStore(),
SettingScreen(),
];
@override
Widget build(BuildContext context) {
int args = ModalRoute.of(context).settings.arguments;
return Scaffold(
body: screen[selectedIndex],
bottomNavigationBar: CurvedNavigationBar(
height: 55,
backgroundColor: Colors.transparent,
buttonBackgroundColor: Theme.of(context).accentColor,
color: Theme.of(context).primaryColor,
animationCurve: Curves.easeOutCubic,
index: selectedIndex,
items: <Widget>[
// code here
],
onTap: (index) {
setState(() {
args = selectedIndex;
selectedIndex = index;
});
},
),
);
}
}
当我进入我的商店屏幕时,有一个添加按钮将我带到一个表格来完成它,但是当我返回时,它没有在我的屏幕上显示 BOTNAVBAR,所以我想如果我可以通过争论所以当我推回到我的主要它自动打开我的商店
这是添加店铺页面:
@override
Widget build(BuildContext context) {
int index = 2;
_onBackPressed() {
Navigator.of(context).pushReplacementNamed(
'/myStore',
arguments: index,
);
}
return WillPopScope(
onWillPop: () {
return _onBackPressed();
},
child: Scaffold(
appBar: AppBar(
title: Text('Add Store'),
leading: IconButton(
onPressed: () {
Navigator.of(context).pushReplacementNamed(
'/myStore',
arguments: index,
);
},
icon: Icon(Icons.arrow_back),
),
),
sp 我是那样做的,我知道这是错误的,因为它不起作用
任何帮助请原谅我糟糕的英语:(
您有一个参数 arguments
,您可以将其传递给 pushReplacementName
:
Future<T?> pushReplacementNamed <T extends Object?, TO extends Object?>(
BuildContext context,
String routeName,
{
TO? result,
Object? arguments,
}
)
你可以这样称呼它:
// Considering a variable named my_args_obj
Navigator.of(context).pushReplacementNamed('/myStore', arguments: my_args_obj);
然后从您的页面获取它:
final args = ModalRoute.of(context).settings.arguments;
请参考:https://flutter.dev/docs/cookbook/navigation/navigate-with-arguments
Navigator.pushNamed(
context,
ExtractArgumentsScreen.routeName,
arguments: MyStore(index),
);
制作接受参数的 MyStore() 小部件,
int Index;
MyStore(this.Index)
并在该屏幕中使用
widget.Index
请将以上link遍历一次。
我正在尝试在我的 flutter 项目中使用 CurveBottomNavigatorBar...
我在主飞镖中使用了我的曲线...,如下所示:
class _BottomNavigatorBarState extends State<BottomNavigatorBar> {
int selectedIndex = 0;
final screen = [
HomeScreen(),
CartScreen(),
MyStore(),
SettingScreen(),
];
@override
Widget build(BuildContext context) {
int args = ModalRoute.of(context).settings.arguments;
return Scaffold(
body: screen[selectedIndex],
bottomNavigationBar: CurvedNavigationBar(
height: 55,
backgroundColor: Colors.transparent,
buttonBackgroundColor: Theme.of(context).accentColor,
color: Theme.of(context).primaryColor,
animationCurve: Curves.easeOutCubic,
index: selectedIndex,
items: <Widget>[
// code here
],
onTap: (index) {
setState(() {
args = selectedIndex;
selectedIndex = index;
});
},
),
);
}
}
当我进入我的商店屏幕时,有一个添加按钮将我带到一个表格来完成它,但是当我返回时,它没有在我的屏幕上显示 BOTNAVBAR,所以我想如果我可以通过争论所以当我推回到我的主要它自动打开我的商店
这是添加店铺页面:
@override
Widget build(BuildContext context) {
int index = 2;
_onBackPressed() {
Navigator.of(context).pushReplacementNamed(
'/myStore',
arguments: index,
);
}
return WillPopScope(
onWillPop: () {
return _onBackPressed();
},
child: Scaffold(
appBar: AppBar(
title: Text('Add Store'),
leading: IconButton(
onPressed: () {
Navigator.of(context).pushReplacementNamed(
'/myStore',
arguments: index,
);
},
icon: Icon(Icons.arrow_back),
),
),
sp 我是那样做的,我知道这是错误的,因为它不起作用
任何帮助请原谅我糟糕的英语:(
您有一个参数 arguments
,您可以将其传递给 pushReplacementName
:
Future<T?> pushReplacementNamed <T extends Object?, TO extends Object?>(
BuildContext context,
String routeName,
{
TO? result,
Object? arguments,
}
)
你可以这样称呼它:
// Considering a variable named my_args_obj
Navigator.of(context).pushReplacementNamed('/myStore', arguments: my_args_obj);
然后从您的页面获取它:
final args = ModalRoute.of(context).settings.arguments;
请参考:https://flutter.dev/docs/cookbook/navigation/navigate-with-arguments
Navigator.pushNamed(
context,
ExtractArgumentsScreen.routeName,
arguments: MyStore(index),
);
制作接受参数的 MyStore() 小部件,
int Index;
MyStore(this.Index)
并在该屏幕中使用
widget.Index
请将以上link遍历一次。