我想将按下的函数传递给MyOutlineButton类,以便将其用于onPressed(这是OutilineButton小部件的可选属性的打开),但是当我将按下的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,
    );
  }

}```

最佳答案

我认为您应该在pressed后面加上大括号,应该像这样;

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

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

07-26 04:53