0

I have two screen, from one i want to pass a title string to another screen. This title can be sign in or sign up, pre decided in first screen. What I have tried:

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,
  )
),

Below is second screen with initialization steps:

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;
  }
}

This is the place where I am using screen title:

@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');
}

Can experts please help me. Thanks

Maurits van Beusekom
  • 5,579
  • 3
  • 21
  • 35
maddy
  • 4,001
  • 8
  • 42
  • 65
  • I think the answer on this page can help you https://stackoverflow.com/questions/50818770/passing-data-to-a-stateful-widget – alabiboo Jul 13 '20 at 19:23
  • I think the answer on this page https://stackoverflow.com/questions/50818770/passing-data-to-a-stateful-widget can help you – alabiboo Jul 13 '20 at 19:25

1 Answers1

0

The code is a bit hard to follow however it looks like you forgot to pass the screenTitle from the MyApp class to the MyAppStateful widget.

In the code you listed above you have the following stateless widget:

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();
  }
}

To me it seems you'll have to pass the screenTitle to the MyAppStateFul constructor in order to make it available in your stateful widget, like so:

  @override
  Widget build(BuildContext context) {
    return MyAppStateFul(screenTitle);
  }

Of course this also requires you to change the MyAppStateFulconstructor to accept the screenTitle parameter if it doesn't already.

I think this should fix it for you.

Maurits van Beusekom
  • 5,579
  • 3
  • 21
  • 35