我有两个屏幕,我想将一个标题字符串传递给另一个屏幕。该标题可以登录或注册,由第一个屏幕决定。我尝试过的:
Container(
child: RaisedGradientButton(
onPressed: () {
print('Login clicked');
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
MyApp(
formMode: FormMode.LOGIN,
screenTitle: "Login",
)
),
);
},
textButton: "Login",
height: 55,
width : 200.0,
buttonTitleColor: Colors.white,
buttonBackgroundColor: Colors.red,
)
),
下面是带有初始化步骤的第二个屏幕:
enum FormMode { LOGIN, SIGNUP }
void main() {
runApp(
MaterialApp(
home: StatelessLanding(),
),
);
}
class MyApp extends StatelessWidget{
// In the constructor, require a Todo
final FormMode formMode;
final String screenTitle;
MyApp({Key key, @required this.formMode, @required this.screenTitle})
: super(key: key);
@override
Widget build(BuildContext context){
return MyAppStateFul();
}
}
class _MyAppStateFulState extends State<MyAppStateFul> {
FormMode formMode;
String screenTitle;
_MyAppStateFulState(FormMode formMode, String screenTitle) {
this.formMode = formMode;
this.screenTitle = screenTitle;
}
}
这是我使用屏幕标题的地方:
@override
Widget build(BuildContext context) {
var screenTitle = "Login";
print('screen title is $widget.screenTitle');
print('screen title is $this.screenTitle');
print('screen title is $screenTitle');
}
请专家帮助我。 谢谢
答案 0 :(得分:0)
该代码很难理解,但是似乎您忘记了将screenTitle
类中的MyApp
传递给MyAppStateful
小部件。
在上面列出的代码中,您具有以下无状态窗口小部件:
class MyApp extends StatelessWidget{
// In the constructor, require a Todo
final FormMode formMode;
final String screenTitle;
MyApp({Key key, @required this.formMode, @required this.screenTitle}) : super(key: key);
@override
Widget build(BuildContext context) {
return MyAppStateFul();
}
}
在我看来,您必须将screenTitle
传递给MyAppStateFul
构造函数,以使其在您的有状态小部件中可用,例如:
@override
Widget build(BuildContext context) {
return MyAppStateFul(screenTitle);
}
当然,这还需要您更改MyAppStateFul
构造函数以接受screenTitle
参数(如果尚未接受)。
我认为这应该为您解决。