我有一个我正在做的Ffltter项目,我不能把整个代码放进go ,因为它有500多行代码,所以我会试着问我的问题,就像我使用imp一样简单.部分代码.

我有一个有状态小部件,并且在扩展State<MusicPlayer>的类下的有状态小部件内有一些函数

文件lib\main.dart

只需使用一个简单的函数,如

class MyAppState extends State<MyApp>{
...
void printSample (){
  print("Sample text");
}
...

此函数位于Main类内的有状态小部件内.

还有另一个文件lib\MyApplication.dart

这个文件还有一个有状态的小部件,我是否可以做些什么,以便在这里调用函数printSample().

class MyApplicationState extends State<MyApplication>{
...
@override
  Widget build(BuildContext context) {
    return new FlatButton(
      child: new Text("Print Sample Text"),
      onPressed :(){
       // i want to cal the function here how is it possible to call the 
       // function 
       // printSample()  from here??  
      }
    );
  }
}

推荐答案

要调用父函数,可以使用回调模式.在本例中,函数(onColorSelected)被传递给子函数.按下按钮时,子元素会调用该函数:

import 'package:flutter/material.dart';

class Parent extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return ParentState();
  }
}

class ParentState extends State<Parent> {
  Color selectedColor = Colors.grey;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Container(
          color: selectedColor,
          height: 200.0,
        ),
        ColorPicker(
          onColorSelect: (Color color) {
            setState(() {
              selectedColor = color;
            });
          },
        )
      ],
    );
  }
}

class ColorPicker extends StatelessWidget {
  const ColorPicker({this.onColorSelect});

  final ColorCallback onColorSelect;

  @override
  Widget build(BuildContext context) {
    return Row(
      children: <Widget>[
        RaisedButton(
          child: Text('red'),
          color: Colors.red,
          onPressed: () {
            onColorSelect(Colors.red);
          },
        ),
        RaisedButton(
          child: Text('green'),
          color: Colors.green,
          onPressed: () {
            onColorSelect(Colors.green);
          },
        ),
        RaisedButton(
          child: Text('blue'),
          color: Colors.blue,
          onPressed: () {
            onColorSelect(Colors.blue);
          },
        )
      ],
    );
  }
}

typedef ColorCallback = void Function(Color color);

内部Flutter 小部件(如按钮或表单域)使用完全相同的模式.如果您只想调用不带任何参数的函数,则可以使用VoidCallback类型,而不是定义您自己的回调类型.


如果要通知更高级别的父级,只需在每个层次 struct 级别上重复此模式即可:

class ColorPickerWrapper extends StatelessWidget {
  const ColorPickerWrapper({this.onColorSelect});

  final ColorCallback onColorSelect;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(20.0),
      child: ColorPicker(onColorSelect: onColorSelect),
    )
  }
}

在Flatter中,不鼓励从父窗口小部件调用子窗口小部件的方法.相反,Flatter鼓励您将子对象的状态作为构造函数参数传递.您只需在父窗口小部件中调用setState来更新其子对象,而不是调用子对象的方法.


另一种方法是Flutter 中的controller个课程(ScrollController,AnimationController,.).这些也作为构造函数参数传递给子对象,并且它们包含无需对父对象调用setState就可以控制子对象状态的方法.示例:

scrollController.animateTo(200.0, duration: Duration(seconds: 1), curve: Curves.easeInOut);

然后要求子元素们收听这些更改以更新其内部状态.当然,您也可以实现自己的控制器类.如果需要,我建议您查看Ffltter的源代码以了解它是如何工作的.


期货和流是向下传递状态的另一种 Select ,也可以用来调用子对象的函数.

但是我真的不推荐这样做.如果您需要调用子窗口小部件的方法,很可能是您的应用程序架构有缺陷.Try to move the state up to the common ancestor!

Flutter相关问答推荐

我不断收到消息说死代码"

Riverpod生成器和生成util类

关闭视频通话,相机仍在运行!(Flutter WebRTC)

如何防止alert 对话框在收到通知时出现在某个flutter页面中

如何从外部控制有状态窗口小部件本身?

修复容器`Text`和容器表单之间的重叠

下拉图标未居中

如何在Flutter 中共享选定文本

使用持久的侧边菜单和顶部栏在Ffltter Web应用程序中的页面之间导航

抖动问题:检测到0个或2个或更多具有相同值的[DropdownMenuItem]

Flutter 中的 setState() 内部是如何工作的?

Flutter - Riverpod 2.0 - 如何访问 onDispose 回调?

在 Dart 中按土耳其语字母顺序对字符串进行排序

如何使Flutter 边框宽度在外面而不是在里面?

如何删除图像边框半径与其父容器边框半径之间的空白

为什么 ref.watch(otherProvider.stream) 在 Riverpod 2.x 中被弃用并且将在 3.x 中被删除?

Flutter:制作自定义小部件的最佳做法是什么?

避免长单词中断

如何在 flutter 中制作这种类型的扩展瓦片

Flutter:滚动一个ListWheelScrollView,无法获取选中项的状态