我在stack over flow人中看到了太多相同的问题,但它不符合我的情况,因为他们要求调用function个从-到Statefull的小部件

我希望调用函数从非Statefull-Stateless Widget定位到State Full小部件中

我的代码很复杂,我会试着简明扼要地解释一下.

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

  @override
  _ExampleState createState() => _ExampleState();
}

class _ExampleState extends State<Example> {

  void myFunction(){
    print('hello dart');
  }
  ShowDialog showDialog = ShowDialog();
  @override
  Widget build(BuildContext context) {
    return TextButton(
      onPressed: (){
        showDialog.myDialog();
      },
        child: Text('tab me')
    );
  }
}


class ShowDialog {

  Widget myDialog(){
    return showDialog(
      builder: (BuildContext context) {
                return SimpleDialog(
                  
                  backgroundColor: Colors.deepPurple[900],
                  titleTextStyle: const TextStyle(
                      color: Colors.red, fontSize: 18),
                  children: [
                    ElevatedButton(
                        onPressed: () {
                         // here i need to call myFunction() Methood 
                        },
                        child: const Text("tab")
                    ),
                  ],
                )
      },
    );
  }

}

我该怎么办?

我希望能对你们有所帮助

推荐答案

您可以直接这样称呼它:

_ExampleState().myFunction();

完整代码:

class Example  extends StatefulWidget {
    const Example ({Key? key, required this.title}) : super(key: key);

      final String title;

      @override
      State<Example > createState() => _ExampleState ();
    }

    class _ExampleState  extends State<Example > {
    
      void myFunction(){
        print('hello dart');
              }
      ShowDialog showDialog = ShowDialog();

     @override
      Widget build(BuildContext context) {

        return Scaffold(
          appBar: AppBar(

            title: Text(widget.title),
          ),
          body: TextButton(
              onPressed: (){
                showDialog.myDialog(context);
              },
              child: Text('tab me')
          )
        );
      }
    }


    class ShowDialog {

      Future myDialog(BuildContext context){
        return showDialog(
            context: context,
          builder: (BuildContext context) {
            return SimpleDialog(

              backgroundColor: Colors.deepPurple[900],
              titleTextStyle: const TextStyle(
                  color: Colors.red, fontSize: 18),
              children: [
                ElevatedButton(
                    onPressed: () {
                      // here i need to call myFunction() Methood
                      _ExampleState().myFunction();
                    },
                    child: const Text("tab")
                ),
              ],
            );
          },
        );
      }

    }

结果是:

enter image description here

Flutter相关问答推荐

如何在Flutter 屏幕中添加箭头形状

Flutter:sqflite DB

如何处理Flutter Riverpod异步通知器中的状态和数据库

如何从DART中的事件列表中获取即将到来的日期

如何确定Flutter 中的文本 colored颜色 ?

在使用FutureProvider时,我想显示用于加载的指示器,但无法自定义大小

谷歌 map 在 flutter 应用程序中显示错误的当前位置

如何在 Flutter 中设计像 zomato 应用程序一样的搜索字段?

Flutter 构建 Web 分段故障 - 无法启动事件处理程序线程

如何将行推到列的底部

Flutter - 我需要显示文本,但应用程序不需要

面对Flutter 中的行和列问题

如何在图像上点或(图像的一部分)在 flutter 中点击?

如何在Flutter中制作带有波浪边框的圆圈来显示图像?

我正在try 使用 Firebase 在 Flutter 中使用 Google 注销,但它不起作用

如何使用列Flutter 中文本占用的空间来控制svg宽度

Flutter 中父容器顶部的 Gridview 间距额外区域

Android Studio 模拟器屏幕闪烁

Flutter_riverpod 安装时出现问题

Flutter 小部件中的视图和逻辑分离