Max
Max

Reputation: 1301

How to return the value of a variable to the previous class in Flutter?

I pass isSendEmail boolean variable to another AboutPageWidget widget, in this widget I change the value of this variable and I want it to return to screen 1, how to do that, how to get the value of isSendEmail variable on screen 1?

screen 1

Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    AboutPageWidget(isSendEmail: isSendEmail),
                    const SizedBox(
                      height: 42,
                    ),
                    isSendEmail
                        ? SetNewPasswordFields(
                            newPasswordCubit: newPasswordCubit,
                          )
                        : const EmailFieldWidget(),
                  ],
                ),

screen 2

class AboutPageWidget extends StatefulWidget {
  AboutPageWidget({
    Key? key,
    required this.isSendEmail,
  }) : super(key: key);

  bool isSendEmail;

  @override
  State<AboutPageWidget> createState() => _AboutPageWidgetState();
}

class _AboutPageWidgetState extends State<AboutPageWidget> {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Row(
          children: [
            Expanded(
              flex: 1,
              child: Align(
                alignment: Alignment.centerLeft,
                child: IconButton(
                  onPressed: () => widget.isSendEmail
                      ? setState(
                          () => widget.isSendEmail = !widget.isSendEmail,
                        )
                      : _onBackPressed(context),

Upvotes: 0

Views: 1043

Answers (3)

Keff
Keff

Reputation: 1071

You must define a callback in the screen2 then listen to it on screen.

For example:

Screen1:

Column(
    mainAxisAlignment: MainAxisAlignment.start,
    children: [
    AboutPageWidget(
        isSendEmail: isSendEmail,
        isSendEmailChanged: (value) => setState(() => isSendEmail = value); 
    ),
    const SizedBox(
        height: 42,
    ),
    isSendEmail
        ? SetNewPasswordFields(
            newPasswordCubit: newPasswordCubit,
            )
        : const EmailFieldWidget(),
    ],
),

Screen2:

class AboutPageWidget extends StatefulWidget {
  AboutPageWidget({
    Key? key,
    required this.isSendEmail,
    required this.isSendEmailChanged,
  }) : super(key: key);

  ValueChanged<bool> isSendEmailChanged;
  bool isSendEmail;

  @override
  State<AboutPageWidget> createState() => _AboutPageWidgetState();
}

class _AboutPageWidgetState extends State<AboutPageWidget> {
    @override
    Widget build(BuildContext context) {
      return Column(
        children: [
          Row(
            children: [
              Expanded(
                flex: 1,
                child: Align(
                  alignment: Alignment.centerLeft,
                  child: IconButton(
                    onPressed: () => widget.isSendEmail
                        ? widget.isSendEmailChanged(!widget.isSendEmail)
                        : _onBackPressed(context),

Then inside _AboutPageWidgetState whenever you want to emit that isSendEmail has changed, just call widget.isSendEmailChanged with the new value.

For more complex states, I'd recommend using Provider or some other state management solution.

Upvotes: 1

Arsalan Khan
Arsalan Khan

Reputation: 146

Navigater has a .then method so you can do

Navigator.of(context).push(nextscreen).then(result=>{
  // Result will contain the data you passed back
});

and on the next screen

Navigator.pop(variable);

Upvotes: 2

Wiktor
Wiktor

Reputation: 775

Unless you want your code to be really messy, It'd be much better to use simple state management. Provider should come in handy.

Upvotes: 1

Related Questions