Flutter - 动态传递导航小部件名称
Flutter - Pass Navigating Widget Name Dynamically
我创建了一个按钮小部件以避免代码重复,因为屏幕上有多个按钮。单击每个按钮应将其带到不同的屏幕。
class ButtonWidget extends StatelessWidget {
final buttonText;
final destination;
ButtonWidget({this.buttonText, this.destination});
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () {
Navigator.push(context, MaterialPageRoute(builder: (context) {
switch (destination) {
case 'signUp':
return SignUp();
break;
case 'signIn':
return SignIn();
break;
case 'SignInGoogle':
return SignInGoogle();
break;
default:
return SignUp();
}
}));
},
child: Align(
alignment: Alignment.bottomCenter,
child: Container(
constraints: BoxConstraints(
minHeight: 6.5 * SizeConfig.heightMultiplier,
maxHeight: 7.9 * SizeConfig.heightMultiplier),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(4 * SizeConfig.heightMultiplier),
),
color: AppTheme.topBarBackgroundColor,
),
child: Center(child: Text(buttonText)),
)));
}
}
点击我想导航到目标小部件。但这似乎不起作用。我按以下方式调用 ButtonWidget:
ButtonWidget(
buttonText: Strings.signUp,
destination: 'signUp',
)
传递小部件而不是它的名称表示:
class ButtonWidget extends StatelessWidget {
final buttonText;
final Widget destination;
ButtonWidget({this.buttonText, this.destination});
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () {
Navigator.push(context, MaterialPageRoute(builder: (context) => destination
));
},
child: Align(
alignment: Alignment.bottomCenter,
child: Container(
constraints: BoxConstraints(
minHeight: 6.5 * SizeConfig.heightMultiplier,
maxHeight: 7.9 * SizeConfig.heightMultiplier),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(4 * SizeConfig.heightMultiplier),
),
color: AppTheme.topBarBackgroundColor,
),
child: Center(child: Text(buttonText)),
)));
}
}
实施:
ButtonWidget(
buttonText: Strings.signUp,
destination: SignUp(),
)
我创建了一个按钮小部件以避免代码重复,因为屏幕上有多个按钮。单击每个按钮应将其带到不同的屏幕。
class ButtonWidget extends StatelessWidget {
final buttonText;
final destination;
ButtonWidget({this.buttonText, this.destination});
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () {
Navigator.push(context, MaterialPageRoute(builder: (context) {
switch (destination) {
case 'signUp':
return SignUp();
break;
case 'signIn':
return SignIn();
break;
case 'SignInGoogle':
return SignInGoogle();
break;
default:
return SignUp();
}
}));
},
child: Align(
alignment: Alignment.bottomCenter,
child: Container(
constraints: BoxConstraints(
minHeight: 6.5 * SizeConfig.heightMultiplier,
maxHeight: 7.9 * SizeConfig.heightMultiplier),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(4 * SizeConfig.heightMultiplier),
),
color: AppTheme.topBarBackgroundColor,
),
child: Center(child: Text(buttonText)),
)));
}
}
点击我想导航到目标小部件。但这似乎不起作用。我按以下方式调用 ButtonWidget:
ButtonWidget(
buttonText: Strings.signUp,
destination: 'signUp',
)
传递小部件而不是它的名称表示:
class ButtonWidget extends StatelessWidget {
final buttonText;
final Widget destination;
ButtonWidget({this.buttonText, this.destination});
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () {
Navigator.push(context, MaterialPageRoute(builder: (context) => destination
));
},
child: Align(
alignment: Alignment.bottomCenter,
child: Container(
constraints: BoxConstraints(
minHeight: 6.5 * SizeConfig.heightMultiplier,
maxHeight: 7.9 * SizeConfig.heightMultiplier),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(4 * SizeConfig.heightMultiplier),
),
color: AppTheme.topBarBackgroundColor,
),
child: Center(child: Text(buttonText)),
)));
}
}
实施:
ButtonWidget(
buttonText: Strings.signUp,
destination: SignUp(),
)