Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dart 将方法作为参数传递给小部件_Dart_Flutter - Fatal编程技术网

Dart 将方法作为参数传递给小部件

Dart 将方法作为参数传递给小部件,dart,flutter,Dart,Flutter,我有一个自定义按钮小部件: class Button extends StatelessWidget { final String text; Button(this.text); @override Widget build(BuildContext context) { return Container( height: 50, child: SizedBox( width: double.infinity,

我有一个自定义按钮小部件:

class Button extends StatelessWidget {
  final String text;

  Button(this.text);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 50,
      child: SizedBox(
        width: double.infinity,
        child: RaisedButton(
          onPressed: () => {}, // Use the function from parent Widget
          child: Padding(
              padding: EdgeInsets.symmetric(vertical: 13),
              child: Text(
                text,
                style: TextStyle(fontWeight: FontWeight.bold),
              )),
          color: COLOR_BLUE,
          textColor: Colors.white,
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(25)),
        ),
      ),
    );
  }
}
...
myMethod () => {
   // do some stuff
}
...
Padding(
    padding: EdgeInsets.only(bottom: 10),
    child: Button("Log in", myMethod),
),
...
然后在父窗口小部件中,我想将一个
onPressed
方法传递给这个按钮小部件:

class Button extends StatelessWidget {
  final String text;

  Button(this.text);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 50,
      child: SizedBox(
        width: double.infinity,
        child: RaisedButton(
          onPressed: () => {}, // Use the function from parent Widget
          child: Padding(
              padding: EdgeInsets.symmetric(vertical: 13),
              child: Text(
                text,
                style: TextStyle(fontWeight: FontWeight.bold),
              )),
          color: COLOR_BLUE,
          textColor: Colors.white,
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(25)),
        ),
      ),
    );
  }
}
...
myMethod () => {
   // do some stuff
}
...
Padding(
    padding: EdgeInsets.only(bottom: 10),
    child: Button("Log in", myMethod),
),
...

如何告诉按钮小部件使用
myMethod
进行
onPress

使用
VoidCallback
类型,如下所示:

class Button extends StatelessWidget {
  final String text;
  final VoidCallback callback;

  Button(this.text, this.callback);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 50,
      child: SizedBox(
        width: double.infinity,
        child: RaisedButton(
          onPressed: callback, // Simply put the function name here, DON'T use ()
          child: Padding(
              padding: EdgeInsets.symmetric(vertical: 13),
              child: Text(
                text,
                style: TextStyle(fontWeight: FontWeight.bold),
              )),
          color: COLOR_BLUE,
          textColor: Colors.white,
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(25)),
        ),
      ),
    );
  }
}
onPressed: () { },
typedef VoidCallback = void Function();
onPressed: (value) { },
typedef ValueSetter<T> = void Function(T value);
onPressed: () => value,
typedef ValueGetter<T> = T Function();
onEvent: (context, child) => value,
typedef MyEventCallback = int Function(BuildContext context, Widget widget);

已经存在一些预定义类型

无效回调 如果要创建类似以下内容的参数:

class Button extends StatelessWidget {
  final String text;
  final VoidCallback callback;

  Button(this.text, this.callback);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 50,
      child: SizedBox(
        width: double.infinity,
        child: RaisedButton(
          onPressed: callback, // Simply put the function name here, DON'T use ()
          child: Padding(
              padding: EdgeInsets.symmetric(vertical: 13),
              child: Text(
                text,
                style: TextStyle(fontWeight: FontWeight.bold),
              )),
          color: COLOR_BLUE,
          textColor: Colors.white,
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(25)),
        ),
      ),
    );
  }
}
onPressed: () { },
typedef VoidCallback = void Function();
onPressed: (value) { },
typedef ValueSetter<T> = void Function(T value);
onPressed: () => value,
typedef ValueGetter<T> = T Function();
onEvent: (context, child) => value,
typedef MyEventCallback = int Function(BuildContext context, Widget widget);
然后您可以在类中定义它,如下所示:

类MyWidget扩展了无状态Widget{
MyWidget({Key-Key,this.onPressed}):超级(Key:Key);
最后一次按下按钮;
// ...
}
笔记
typedef
在源代码中定义如下:

class Button extends StatelessWidget {
  final String text;
  final VoidCallback callback;

  Button(this.text, this.callback);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 50,
      child: SizedBox(
        width: double.infinity,
        child: RaisedButton(
          onPressed: callback, // Simply put the function name here, DON'T use ()
          child: Padding(
              padding: EdgeInsets.symmetric(vertical: 13),
              child: Text(
                text,
                style: TextStyle(fontWeight: FontWeight.bold),
              )),
          color: COLOR_BLUE,
          textColor: Colors.white,
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(25)),
        ),
      ),
    );
  }
}
onPressed: () { },
typedef VoidCallback = void Function();
onPressed: (value) { },
typedef ValueSetter<T> = void Function(T value);
onPressed: () => value,
typedef ValueGetter<T> = T Function();
onEvent: (context, child) => value,
typedef MyEventCallback = int Function(BuildContext context, Widget widget);
异步版本是
AsyncCallback

typedef AsyncCallback = Future<void> Function();
然后您可以在类中定义它,如下所示:

类MyWidget扩展了无状态Widget{
MyWidget({Key-Key,this.onPressed}):超级(Key:Key);
终值设定人;
// ...
}
笔记 typedef在源代码中定义如下:

class Button extends StatelessWidget {
  final String text;
  final VoidCallback callback;

  Button(this.text, this.callback);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 50,
      child: SizedBox(
        width: double.infinity,
        child: RaisedButton(
          onPressed: callback, // Simply put the function name here, DON'T use ()
          child: Padding(
              padding: EdgeInsets.symmetric(vertical: 13),
              child: Text(
                text,
                style: TextStyle(fontWeight: FontWeight.bold),
              )),
          color: COLOR_BLUE,
          textColor: Colors.white,
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(25)),
        ),
      ),
    );
  }
}
onPressed: () { },
typedef VoidCallback = void Function();
onPressed: (value) { },
typedef ValueSetter<T> = void Function(T value);
onPressed: () => value,
typedef ValueGetter<T> = T Function();
onEvent: (context, child) => value,
typedef MyEventCallback = int Function(BuildContext context, Widget widget);
估价师 如果要创建类似以下内容的参数:

class Button extends StatelessWidget {
  final String text;
  final VoidCallback callback;

  Button(this.text, this.callback);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 50,
      child: SizedBox(
        width: double.infinity,
        child: RaisedButton(
          onPressed: callback, // Simply put the function name here, DON'T use ()
          child: Padding(
              padding: EdgeInsets.symmetric(vertical: 13),
              child: Text(
                text,
                style: TextStyle(fontWeight: FontWeight.bold),
              )),
          color: COLOR_BLUE,
          textColor: Colors.white,
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(25)),
        ),
      ),
    );
  }
}
onPressed: () { },
typedef VoidCallback = void Function();
onPressed: (value) { },
typedef ValueSetter<T> = void Function(T value);
onPressed: () => value,
typedef ValueGetter<T> = T Function();
onEvent: (context, child) => value,
typedef MyEventCallback = int Function(BuildContext context, Widget widget);
然后您可以在类中定义它,如下所示:

类MyWidget扩展了无状态Widget{
MyWidget({Key-Key,this.onPressed}):超级(Key:Key);
最终估价师已按下;
// ...
}
笔记 typedef在源代码中定义如下:

class Button extends StatelessWidget {
  final String text;
  final VoidCallback callback;

  Button(this.text, this.callback);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 50,
      child: SizedBox(
        width: double.infinity,
        child: RaisedButton(
          onPressed: callback, // Simply put the function name here, DON'T use ()
          child: Padding(
              padding: EdgeInsets.symmetric(vertical: 13),
              child: Text(
                text,
                style: TextStyle(fontWeight: FontWeight.bold),
              )),
          color: COLOR_BLUE,
          textColor: Colors.white,
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(25)),
        ),
      ),
    );
  }
}
onPressed: () { },
typedef VoidCallback = void Function();
onPressed: (value) { },
typedef ValueSetter<T> = void Function(T value);
onPressed: () => value,
typedef ValueGetter<T> = T Function();
onEvent: (context, child) => value,
typedef MyEventCallback = int Function(BuildContext context, Widget widget);
定义您自己的类型 从上面的所有示例中可以看出,对于
函数
,一切都只是
typedef
。所以你很容易就能做出自己的决定

假设你想做这样的事情:

class Button extends StatelessWidget {
  final String text;
  final VoidCallback callback;

  Button(this.text, this.callback);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 50,
      child: SizedBox(
        width: double.infinity,
        child: RaisedButton(
          onPressed: callback, // Simply put the function name here, DON'T use ()
          child: Padding(
              padding: EdgeInsets.symmetric(vertical: 13),
              child: Text(
                text,
                style: TextStyle(fontWeight: FontWeight.bold),
              )),
          color: COLOR_BLUE,
          textColor: Colors.white,
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(25)),
        ),
      ),
    );
  }
}
onPressed: () { },
typedef VoidCallback = void Function();
onPressed: (value) { },
typedef ValueSetter<T> = void Function(T value);
onPressed: () => value,
typedef ValueGetter<T> = T Function();
onEvent: (context, child) => value,
typedef MyEventCallback = int Function(BuildContext context, Widget widget);
然后您将使typedef如下所示:

class Button extends StatelessWidget {
  final String text;
  final VoidCallback callback;

  Button(this.text, this.callback);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 50,
      child: SizedBox(
        width: double.infinity,
        child: RaisedButton(
          onPressed: callback, // Simply put the function name here, DON'T use ()
          child: Padding(
              padding: EdgeInsets.symmetric(vertical: 13),
              child: Text(
                text,
                style: TextStyle(fontWeight: FontWeight.bold),
              )),
          color: COLOR_BLUE,
          textColor: Colors.white,
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(25)),
        ),
      ),
    );
  }
}
onPressed: () { },
typedef VoidCallback = void Function();
onPressed: (value) { },
typedef ValueSetter<T> = void Function(T value);
onPressed: () => value,
typedef ValueGetter<T> = T Function();
onEvent: (context, child) => value,
typedef MyEventCallback = int Function(BuildContext context, Widget widget);
然后像这样使用它:

class Button extends StatelessWidget {
  final String text;
  final VoidCallback callback;

  Button(this.text, this.callback);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 50,
      child: SizedBox(
        width: double.infinity,
        child: RaisedButton(
          onPressed: callback, // Simply put the function name here, DON'T use ()
          child: Padding(
              padding: EdgeInsets.symmetric(vertical: 13),
              child: Text(
                text,
                style: TextStyle(fontWeight: FontWeight.bold),
              )),
          color: COLOR_BLUE,
          textColor: Colors.white,
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(25)),
        ),
      ),
    );
  }
}
onPressed: () { },
typedef VoidCallback = void Function();
onPressed: (value) { },
typedef ValueSetter<T> = void Function(T value);
onPressed: () => value,
typedef ValueGetter<T> = T Function();
onEvent: (context, child) => value,
typedef MyEventCallback = int Function(BuildContext context, Widget widget);
类MyWidget扩展了无状态Widget{
MyWidget({Key-Key,this.onEvent}):超级(Key:Key);
最后一次MyEventCallback onEvent;
// ...
}

有关更多信息,请参阅。

查看a+1的操作方法。如果您想要一个接受参数和/或返回结果的方法,也可以为方法定义自己的typedef:比如我创建了一个返回TextField的CustomTextField。如何为onChanged属性创建构造函数并正确传递它?我试图为CustomTextField创建一个构造函数,将函数正确地传递给里面的TextField。事情就像一次改变:一次改变。@IvánYoed,你可以做同样的事情。他们的
值发生了变化?一旦更改
,其中
值更改
定义为
无效函数(T值)