简体   繁体   English

Flutter 将数据从一个 class 传递到另一个(有状态小部件)

[英]Flutter pass data from one class to another(statefull widget)

How can i pass the data that i get from user via _textController and save it on a variable on the other class?我如何传递通过_textController 从用户那里获得的数据并将其保存在另一个 class 上的变量中? I want to get the data that i get from the user and save it on a variable on the other class我想获取从用户那里获取的数据并将其保存在另一个 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我不知道如何传递数据

I think u can use Riverpod StateProvider.我想你可以使用Riverpod StateProvider。

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

For using this valueProvider u must use ConsumerStatefulWidget instead of StatefulWidget要使用此valueProvider ,您必须使用 ConsumerStatefulWidget 而不是 StatefulWidget

Editing valueProvider编辑valueProvider

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

Using valueProvider from another ConsumerStatefulWidget使用来自另一个 ConsumerStatefulWidget 的valueProvider

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.我建议你看看Flutter 主题中的 State 管理,我认为这是 Flutter 开发的关键部分。

u could simply call the showtoast() function in the onpressed() for any button and pass it the parameters which you want to use,您可以简单地在 onpressed() 中为任何按钮调用 showtoast() function 并将其传递给您要使用的参数,

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Flutter 的表单中将数据从一个小部件传递到另一个小部件 - Pass data from one widget to another in a Form in Flutter 如何从同一屏幕上的另一个有状态小部件接收数据? - How to receive data from another statefull widget on the same screen? 如何将数据从扩展 DataTableSource 的 class 传递到 StateFull Class - How to pass data from class of extends DataTableSource to StateFull Class Flutter - 如何在搜索时将搜索文本从一个小部件传递到另一个 class 小部件? - Flutter - How to pass search text from one widget to its another class widget while searching? 如何从 Flutter 中的其他有状态 Class 重建或刷新有状态 Class? - How to Rebuild or Refresh a Statefull Class from an other Statefull Class in Flutter? Flutter 错误:如何将一个小部件 class 的数据传递给另一个正常的 class? - Flutter Error: How to pass data one widget class to another normal class? 将变量从有状态小部件传递到 state - pass a variable from a statefull widget to state 我如何将数据从一个小部件传递到 flutter 中的另一个小部件? - How i can pass Data from one widget to another widget in flutter? 我如何将 QuerySnapshot 数据从一个小部件传递到 flutter 中的另一个小部件? - How i can pass QuerySnapshot data from one widget to another widget in flutter? 如何将数据从一个类传递到另一个类? - How to pass data from one class to another in flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM