2

I can already take an argument from the navigator. this is the code:

  import 'package:flutter/material.dart';

  void main() => runApp(MyApp());

  class MyApp extends StatelessWidget {
    // This widget is the root of your application.
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        routes: {
          "/": (_) => Screen1(),
          "/2": (_) => Screen2(),
        },
      );
    }
  }

  class Screen1 extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        floatingActionButton: FloatingActionButton(
          onPressed: () =>
              Navigator.of(context).pushNamed("/2", arguments: "test"),
        ),
      );
    }
  }

  class Screen2 extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      final String param = ModalRoute.of(context).settings.arguments;
      print("This function is called when navigator poped");
      return Scaffold(
        body: Center(
          child: InkWell(
              onTap: () => Navigator.of(context).pop(), child: Text(param)),
        ),
      );
    }
  }

I don't want the build function to be called when the navigator is pop. I know if something has changed in the navigator, it will call the build function again. but I want that not to happen. how to pop to screen1 without calling build function of Screen2?

mosleim
  • 654
  • 6
  • 19
  • 1
    and your question is ...? – pskink Dec 12 '19 at 20:22
  • Are you trying to go back to Screen1 with param without refreshing Screen1? – Pro Dec 12 '19 at 20:22
  • @pskink i have updated the question. – mosleim Dec 12 '19 at 20:29
  • @Pro no. but, i want to go back to screen1 without refresing build screen2. build screen2 is called when i go back to screen1. – mosleim Dec 12 '19 at 20:31
  • `"how to pop to screen1 without updatting param?"` what param? what do you want to achieve in simple words? maybe this will help: https://stackoverflow.com/a/52249579/2252830? – pskink Dec 12 '19 at 20:31
  • @pskink sory my english not good. i have update my question and complete the code. – mosleim Dec 12 '19 at 23:50
  • I have the same problem. It is even worse, when you have a chain of widgets, accessing navigator parameters. ALL of them will call their build methods when either popping or pushing. – eugenekr Mar 01 '20 at 05:58

0 Answers0