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:

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

Leave a Reply