Home > Back-end >  how to display input value from text field to other pages?
how to display input value from text field to other pages?

Time:08-24

I'm trying to display the input value below onto another page that contains radio buttons . my aim is that every time i write or change a value in a textfield it gets updated and displays onto a different page with the radio options . The snippet of code below is for the textfield - i used riverpod to connect it to firestore:

    class AddMealPage extends ConsumerWidget {
  const AddMealPage({Key? key}) : super(key: key);
  static const String route = "/addMeal";

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final model = ref.read(addMealProvider);
    return LoadingLayer(
      child: Scaffold(
        appBar: AppBar(
          elevation: 0,
          iconTheme: const IconThemeData(
            color: Colors.black,
          ),
          title: const Text(
            "Create Meal",
            style: TextStyle(color: Colors.black, fontWeight: FontWeight.bold),
          ),
          backgroundColor: Colors.white,
        ),
        bottomNavigationBar: Padding(
          padding: const EdgeInsets.fromLTRB(24, 0, 24, 24),
          child: MaterialButton(
            padding: const EdgeInsets.all(16),
            color: Colors.black,
            onPressed: model.enabled
                ? () async {
                    try {
                      await model.writeMeal();
                      Navigator.pop(context);
                    } catch (e) {
                      AppSnackbar(context).error(e);
                    }
                  }
                : null,
            child: const Text(
              "Add meal",
              style: TextStyle(color: Color.fromARGB(255, 247, 245, 245)),
            ),
          ),
        ),
        body: SingleChildScrollView(
          child: Container(
            padding:
                const EdgeInsets.symmetric(vertical: 60.0, horizontal: 10.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                GestureDetector(
                  onTap: () async {
                    final picked = await ImagePicker()
                        .pickImage(source: ImageSource.gallery);
                    if (picked != null) {
                      model.file = File(picked.path);
                    }
                  },
                  child: Container(
                    margin: const EdgeInsets.only(),
                    width: MediaQuery.of(context).size.width,
                    height: 210.0,
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(20),
                        image: (model.mealUrl != null || model.file != null)
                            ? DecorationImage(
                                image: model.file != null
                                    ? FileImage(model.file!)
                                    : NetworkImage(model.mealUrl!)
                                        as ImageProvider,
                                fit: BoxFit.cover,
                              )
                            : null),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.end,
                      crossAxisAlignment: CrossAxisAlignment.stretch,
                      children: [
                        if (model.mealUrl == null && model.file == null)
                          const Expanded(
                            child: Center(
                              child: Icon(
                                Icons.photo,
                              ),
                            ),
                          ),
                        const Material(
                          color: Colors.transparent,
                          child: Padding(
                            padding: EdgeInsets.all(8.0),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
                const SizedBox(
                  height: 10,
                ),
                const Padding(
                  padding: EdgeInsets.only(left: 15.0),
                  child: Text(
                    "Meal Name",
                    style: TextStyle(fontSize: 17, fontWeight: FontWeight.w500),
                  ),
                ),
                Container(
                  height: 50,
                  padding: const EdgeInsets.only(top: 10),
                  width: MediaQuery.of(context).size.width,
                  margin: const EdgeInsets.symmetric(horizontal: 20),
                  child: TextFormField(
                    textInputAction: TextInputAction.done,
                    initialValue: model.mealName,
                    maxLines: 1,
                    decoration: InputDecoration(
                      enabledBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(10),
                        borderSide: const BorderSide(color: Colors.grey),
                      ),
                      focusedBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(5),
                        borderSide: const BorderSide(color: Colors.grey),
                      ),
                    ),
                    onChanged: (v) => model.mealName = v,
                  ),
                ),

I want to display the input value i get from the textfield above in the format of a radioButton on a seperate page :

    enum Variant { mealName }

class RadioOption extends StatefulWidget {
  const RadioOption({Key? key}) : super(key: key);

  @override
  State<RadioOption> createState() => _RadioOptionState();
}

class _RadioOptionState extends State<RadioOption> {
  Variant? _character = Variant.mealName;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        RadioListTile<Variant>(
          title: const Text('$mealName'),
          value: Variant.MealName,
          groupValue: _character,
          onChanged: (Variant? value) {
            
            setState(() {
              _character = value;
            });
          },
        ),
    

CodePudding user response:

I don't see the whole architecture of the app. But if you want to transfer model.mealName, you can make String value (mealName) in your RadioOption, and pass it through the constructor:

final mealName;    
RadioOption({required this.mealName});

And then access it in _RadioOptionState by:

title: const Text(widget.mealName);

CodePudding user response:

You can use TextEditingController and add this controller to your TextFormField which have controller properties but you can't use initialValue and controller both so avoid initialValue. To put value you can use controller.text = your_value; You value will be filled in text field. To get value from TextFormField Use var newValue = controller.text; and send this newValue to another page in constructor.

  • Related