Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to re-pass a parameter to another screen?

The last screen takes the color parameter from the previous screen – arg. I need that when you click on the button on the last screen, this parameter, which takes the last screen, is again transferred to the HomeScreenWidget. How can this be implemented?

My code:

class _TextValueScreenState extends State<TextValueScreen> {
  // controller for textField
  TextEditingController textController = TextEditingController();

  @override
  void dispose() {
    textController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    //get arg from ColorPickerScreen
    var arg = ModalRoute.of(context)!.settings.arguments as ColorArguments;

    return Scaffold(
      backgroundColor: arg.color,
      appBar: AppBar(
        title: const Text('Enter a value'),
        centerTitle: true,
      ),
      body: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            TextField(
              controller: textController,
              keyboardType: TextInputType.number,
              inputFormatters: [FilteringTextInputFormatter.digitsOnly],
              style: TextStyle(color: Colors.black),
              decoration: const InputDecoration(
                  hintText: 'Enter a value',
                  enabledBorder: UnderlineInputBorder(
                      borderSide: BorderSide(color: Colors.black, width: 2)),
                  focusedBorder: UnderlineInputBorder(
                      borderSide: BorderSide(color: Colors.black, width: 2))),
            ),
            const SizedBox(
              height: 20,
            ),
            ElevatedButton(
              onPressed: () {
                Navigator.pushAndRemoveUntil(
                  context,
                  MaterialPageRoute(
                    builder: (BuildContext context) =>
                        HomeScreenWidget(valueText: textController.text),
                  ),
                  (route) => false,
                );
              },
              child: const Text('Done'),
            )
          ],
        ),
      ),
    );
  }
}

Main.dart:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

routes: {
         HomeScreenWidget.routeName: (context) => HomeScreenWidget(valueText: '',),
          ColorPickerWidget.routeName: (context) => ColorPickerWidget(),
          TextValueScreen.routeName: (context) => TextValueScreen(),
        },
        initialRoute: HomeScreenWidget.routeName,

>Solution :

You can have HomeScreenWidget take a ColorArguments parameter in the constructor like so:

class HomeScreenWidget{
  final ColorArguments? colorArgs;
  final String valueText;

  HomeScreenWidget({
    Key? key,
    required this.valueText;
    required this.colorArgs;
 });
}

Then pass it to the page like so:

Navigator.pushAndRemoveUntil(
  context,
  MaterialPageRoute(
     builder: (BuildContext context) => HomeScreenWidget(valueText: textController.text, colorArgs: arg),), (route) => false,
);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading