首页 > 解决方案 > Returning to same exact place flutter navigation

问题描述

Is it possible to return to the exact same place meaning state wise in flutter while using this?

Navigator.of(context).push(new MaterialPageRoute(builder: (BuildContext context) => new ConnectHome(user:widget.user))));

We have cards on the home screen "ConnectHome()" and we need to return them to the same spot.

标签: flutterdart

解决方案


您可以在下面复制粘贴运行完整代码
您可以await Navigator.push并在Navigator.pop包含UserObject()
您可以看到代码继续执行并打印UserObject()
代码片段

final result = await Navigator.push(
  context,
  MaterialPageRoute(builder: (context) => ConnectHome()),
);

print('result ${result.name}')

...
Navigator.pop(context, UserObject("hello","world"));

工作演示

在此处输入图像描述

完整代码

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    title: 'Returning Data',
    home: HomeScreen(),
  ));
}

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Returning Data Demo'),
      ),
      body: Center(child: SelectionButton()),
    );
  }
}

class SelectionButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return RaisedButton(
      onPressed: () {
        _navigateAndDisplaySelection(context);
      },
      child: Text('Pick an option, any option!'),
    );
  }

  // A method that launches the SelectionScreen and awaits the result from
  // Navigator.pop.
  _navigateAndDisplaySelection(BuildContext context) async {
    // Navigator.push returns a Future that completes after calling
    // Navigator.pop on the Selection Screen.
    final result = await Navigator.push(
      context,
      MaterialPageRoute(builder: (context) => ConnectHome()),
    );

    print('result ${result.name}');
    // After the Selection Screen returns a result, hide any previous snackbars
    // and show the new result.
    Scaffold.of(context)
      ..removeCurrentSnackBar()
      ..showSnackBar(SnackBar(content: Text("${result.name}")));
  }
}

class UserObject {
  String name;
  String id;

  UserObject(this.name, this.id);
}

class ConnectHome extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Pick an option'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: RaisedButton(
                onPressed: () {
                  // Close the screen and return "Yep!" as the result.
                  Navigator.pop(context, UserObject("hello","world"));
                },
                child: Text('Hello'),
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: RaisedButton(
                onPressed: () {
                  // Close the screen and return "Nope!" as the result.
                  Navigator.pop(context, UserObject("no","No"));
                },
                child: Text('No.'),
              ),
            )
          ],
        ),
      ),
    );
  }
}

推荐阅读