Home > Software engineering >  How to pass parameters using pushNamedAndRemoveUntil?
How to pass parameters using pushNamedAndRemoveUntil?

Time:05-03

I need to pass parameters to the initial page using pushNamedAndRemoveUntil . I need to re-pass an arg that takes color and textController.text to valueText and color on the initial page. You need to do this with pushNamedAndRemoveUntil. Please help implement this functionality. Screen with arg:

class TextValue extends StatefulWidget {
  static const routeName = '/text_value';

  const TextValue({Key? key}) : super(key: key);

  @override
  State<TextValue> createState() => _TextValueState();
}

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

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

  //arg variable
  ColorArguments? arg;

  @override
  Widget build(BuildContext context) {
    //get arg from ColorPickerScreen
    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: [
            TextFormField(
              controller: textController,
              keyboardType: TextInputType.number,
              inputFormatters: [
                FilteringTextInputFormatter.allow(RegExp(r'(^\d*\.?\d*)'))
              ],
              style: const 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: () {
                if (textController.text.isEmpty) {
                } else {
                  Navigator.pushNamedAndRemoveUntil(
                      context, HomeScreen.routeName, (route) => false,
                  );
                }
              },
              child: const Text('Done'),
            )
          ],
        ),
      ),
    );
  }
}

Initial Screen:

import 'package:flutter/material.dart';
import 'package:flutter_app/screens/color_picker_screen.dart';

class HomeScreen extends StatefulWidget {
  final String valueText;
  final ColorArguments? color;
  static const routeName = '/home';

  const HomeScreen({Key? key, required this.valueText, required this.color})
      : super(key: key);

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  // navigation to the next screen

  void _colorScreen() {
    Navigator.push(context,
        MaterialPageRoute(builder: (context) => const ColorPicker()));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Home'),
        centerTitle: true,
      ),
      body: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
                onPressed: _colorScreen, child: const Text('Choose a color')),
            const SizedBox(height: 30.0),
            TextFormField(
              readOnly: true,
              initialValue: widget.valueText,
            ),
            const SizedBox(height: 100),
            Container(
              width: 50,
              height: 50,
              color: widget.color?.color,
            ),
          ],
        ),
      ),
    );
  }
}

CodePudding user response:

You can use the named arguments parameter of the pushNamedAndRemoveUntil method. All you need to do is create a custom object for passing all the values you need to the push call. The arguments can be accessed in your initial screen by using

final args = ModalRoute.of(context)!.settings.arguments

You can refer to this flutter cookbook on navigation using named routes with arguments

CodePudding user response:

Why dont you consider using Class contructors and passing data like this

Navigator.push(context,
    MaterialPageRoute(builder: (context) => const ColorPicker(<Your_data>)));
  • Related