skip to Main Content

How can i pass the data that i get from user via _textController and save it on a variable on the other class? I want to get the data that i get from the user and save it on a variable on the other class

 final _textController = TextEditingController();
  String userPost = "";
  @override
  Widget build(BuildContext context) {
    final double height = MediaQuery.of(context).size.height;
    final double width = MediaQuery.of(context).size.width;
    return ScreenUtilInit(
        designSize: const Size(375, 812),
        minTextAdapt: true,
        splitScreenMode: true,
        builder: (context, child) {
          return MaterialApp(
            debugShowCheckedModeBanner: false,
            home: Scaffold(
              backgroundColor: Colors.amber,
              body: Container(
                decoration: const BoxDecoration(
                    gradient: LinearGradient(
                        begin: Alignment.topCenter,
                        end: Alignment.bottomCenter,
                        colors: [
                      Color.fromRGBO(95, 44, 130, 1),
                      Color.fromRGBO(73, 160, 157, 1),
                    ])),
                
                      Padding(
                        padding: EdgeInsets.all(60.sp),
                        child: TextFormField(
                          controller: _textController,
                          decoration: InputDecoration(
                              labelText: 'What s your name?',
                              suffixIcon: IconButton(
                                  onPressed: () {
                                    _textController.clear();
                                  },
                                  icon: const Icon(Icons.clear))),
                        ),
                      ),
                      SizedBox(
                        height: 5.h,
                      ),
                      MaterialButton(
                        elevation: 0,
                        onPressed: () {
                          setState(() {
                            userPost = _textController.text;
                            _textController.clear();
                          });
                        },

and displayed here :

 showToast();
                        NotificationService().showNotification(
                          1,
                          'You can, ',
                          randomName!,
                        );
                      },

I don t know how to pass the data

2

Answers


  1. I think u can use Riverpod StateProvider.

    final valueProvider = StateProvider<String>(
      (ref) => "",
    );
    

    For using this valueProvider u must use ConsumerStatefulWidget instead of StatefulWidget

    Editing valueProvider

    ref.watch(valueProvider.state).state = _textController.text
    

    Using valueProvider from another ConsumerStatefulWidget

    final text = ref.watch(valueProvider.notifier).state
    

    NOTE

    I recommend you look State Management in Flutter topic I think it is critical part of Flutter Development.

    Login or Signup to reply.
  2. u could simply call the showtoast() function in the onpressed() for any button and pass it the parameters which you want to use,

    as

    onpressed() 
    {
    showtoast('message to toast');
    other code--
    }
    

    and change this

     showToast();
                            NotificationService().showNotification(
                              1,
                              'You can, ',
                              randomName!,
                            );
                          },
    

    to

     showToast(String message);
                            NotificationService().showNotification(
                              1,
                              'You can, ',
                              message,
                            );
                          },
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search