Android 如何将void函数传递给重写的OutlineButton类,以用作onPressed属性

Android 如何将void函数传递给重写的OutlineButton类,以用作onPressed属性,android,flutter,dart,Android,Flutter,Dart,我想将pressed函数传递给MyOutlineButton类,以便将其用于onPressed,这是OutilineButton小部件的可选属性之一,但当我将pressed void函数传递给MyOutlineButton类时,我在屏幕上看不到任何更改。。。 我希望当我点击播放按钮应用程序播放指定的歌曲。 我不知道我是否清楚 请帮帮我 代码如下: import 'package:flutter/material.dart'; import 'package:flutter/cupertino.d

我想将pressed函数传递给MyOutlineButton类,以便将其用于onPressed,这是OutilineButton小部件的可选属性之一,但当我将pressed void函数传递给MyOutlineButton类时,我在屏幕上看不到任何更改。。。 我希望当我点击播放按钮应用程序播放指定的歌曲。 我不知道我是否清楚 请帮帮我

代码如下:

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
void main() => runApp(ExampleApp());

class ExampleApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: App(),
    );
  }
}

class App extends StatefulWidget {

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

class _AppState extends State<App> {
  final assetsAudioPlayer = AssetsAudioPlayer();
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    void pressed = () {
      setState(() {
        assetsAudioPlayer.open(
          Audio("assets/audio/ebi.mp3"),
        );
        assetsAudioPlayer.play();
        print("oh yeah!");
      });
    };
    return Scaffold(
      appBar: AppBar(

      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Center(
            child: MyOutlineButton(text: "play",onPress: () => pressed,),
          )

        ],
      ),
    );
  }
}
class MyOutlineButton extends StatefulWidget {
  MyOutlineButton(
      {this.text, this.onPress, this.hover, this.focus, this.splash, this.highlight, this.textColor});

  final String text;
  final void onPress;
  final Color splash;
  final Color highlight;
  final Color focus;
  final Color hover;
  final Color textColor;
  @override
  _MyOutlineButtonState createState() => _MyOutlineButtonState();
}

class _MyOutlineButtonState extends State<MyOutlineButton>{
  @override
  Widget build(BuildContext context) {
    print("hello");
    return OutlineButton(
      textColor: widget.textColor,
      highlightColor: widget.highlight,
      splashColor: widget.splash,
      focusColor: widget.focus,
      hoverColor: widget.hover,
      child: Text(widget.text,),
      //padding: const EdgeInsets.all(5.0),
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8.0)),
      borderSide: BorderSide(
          color: Colors.blue
      ),
      onPressed: () => widget.onPress,
    );
  }

}```

导入“包装:颤振/材料.省道”;
进口“包装:颤振/cupertino.dart”;
void main()=>runApp(ExampleApp());
类ExampleApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
主页:App(),
);
}
}
类应用程序扩展StatefulWidget{
@凌驾
_AppState createState();
}
类_AppState扩展了状态{
最终assetsAudioPlayer=assetsAudioPlayer();
@凌驾
小部件构建(构建上下文){
//TODO:实现构建
空压=(){
设置状态(){
assetsAudioPlayer.open(
音频(“资产/音频/ebi.mp3”),
);
assetsAudioPlayer.play();
打印(“哦,耶!”);
});
};
返回脚手架(
appBar:appBar(
),
正文:专栏(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
居中(
子项:MyOutlineButton(文本:“播放”,按:()=>按,),
)
],
),
);
}
}
类MyOutlineButton扩展StatefulWidget{
MyOutlineButton(
{this.text,this.onPress,this.hover,this.focus,this.splash,this.highlight,this.textColor});
最终字符串文本;
最后一次付印无效;
最终色斑;
最终颜色高亮;
最终颜色焦点;
最终颜色悬停;
最终颜色文本颜色;
@凌驾
_MyOutlineButtonState createState()=>\u MyOutlineButtonState();
}
类_MyOutlineButtonState扩展状态{
@凌驾
小部件构建(构建上下文){
打印(“你好”);
返回框按钮(
textColor:widget.textColor,
highlightColor:widget.highlight,
splashColor:widget.splash,
focusColor:widget.focus,
hoverColor:widget.hover,
子:Text(widget.Text,),
//填充:常数边集全部(5.0),
形状:RoundedRectangleBorder(borderRadius:borderRadius.circular(8.0)),
边界边(
颜色:颜色。蓝色
),
onPress:()=>widget.onPress,
);
}
}```

我想你应该在按下
后放上大括号,应该是这样的

MyOutlineButton(text: "play",onPress: () => pressed(),)
或者更容易

MyOutlineButton(text: "play",onPress: pressed,)

您只需将函数设置为按钮。您正在这样做:

onPress:()=>widget.onPress
但这不起作用,因为您没有“调用”函数,有两种方法可以做到这一点:

onPressed:()=>widget.onPress?.call()

onPress:widget.onPress