[英]The functionality of 'onclick' for a button in Android in Flutter
I'm currently working in Flutter to develop an app in language learning.我目前在 Flutter 工作,开发语言学习应用程序。 Previously, I had developed a similar app for Android in Android Studio.
之前,我在 Android Studio 中为 Android 开发了一个类似的应用程序。 In that app I had buttons which when clicked would produce a local audio file in the form of a short spoken sentence.
在那个应用程序中,我有一些按钮,单击这些按钮会以简短的口语形式生成本地音频文件。 I would like to have a similarly functioning button in Flutter.
我想在 Flutter 中有一个类似功能的按钮。 I've been looking for quite a while now but without succes.
我一直在寻找很长一段时间,但没有成功。 Right now, when I click I get no audio.I'm now looking for help.
现在,当我单击时,我没有音频。我现在正在寻求帮助。 I would really appreciate some advice on this matter.
我真的很感激关于这个问题的一些建议。 Thanks.
谢谢。 Code:
代码:
import 'package:audioplayers/audio_cache.dart';
import 'package:audioplayers/audioplayers.dart';
class Lesson1Viet extends StatefulWidget {
@override
_Lesson1VietState createState() => _Lesson1VietState();
}
class _Lesson1VietState extends State<Lesson1Viet> {
AudioCache _audioCache;
@override
void initState() {
super.initState();
// create this only once
_audioCache = AudioCache(prefix: "assets/", fixedPlayer: AudioPlayer()..setReleaseMode(ReleaseMode.STOP));
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[350],
appBar: AppBar(
title: Text("Lesson 1"),
centerTitle: true,
backgroundColor: Colors.grey[600],),
body: ListView(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Image.asset('assets/hotel_reception_program_istock.jpg',
width: 350,
height: 300,
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
FlatButton(onPressed: () => _audioCache.play('assets/mijnopname.mp3'),
child: Icon (Icons.arrow_right) ,
color: Colors.grey[400],
),
],
),
Divider(
height: 60.0,
color: Colors.black,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly ,
children: [
Image.asset('assets/guest_at_reception.png',
width: 100.0,
height: 100.0,
),
SizedBox(width: 100.0,),
Image.asset('assets/receptionist_male.png',
width: 100.0,
height: 100.0,
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Text(' Hello.',
style: TextStyle(
fontWeight: FontWeight.bold
)
),
SizedBox(width: 100.0,),
Text('Hello and welcome. ',
style: TextStyle(
fontWeight: FontWeight.bold
)
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ButtonTheme(
minWidth: 50.0,
height: 10.0,
child: RaisedButton
(onPressed: () => _audioCache.play('assets/hello_and_welcome.mp4'),
child:Icon(Icons.arrow_right),
color: Colors.grey[400],
),
),
SizedBox(width:100.0),
ButtonTheme(
minWidth: 50.0,
height: 10.0,
child: RaisedButton
(onPressed: () => _audioCache.play('assets/hello_and_welcome.mp4'),
child: Icon(Icons.arrow_right),
color: Colors.grey[400],
),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text(' Xin chào.',
style: TextStyle(
fontWeight: FontWeight.bold
)
),
SizedBox(width:100.0),
Text('Xin chào và chào mừng.',
style: TextStyle(
fontWeight: FontWeight.bold
)
),
],
),
Divider(
height: 60.0,
color: Colors.black,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Text(' Thank you.',
style: TextStyle(
fontWeight: FontWeight.bold
)
),
SizedBox(width: 100.0,),
Text(' How may I help you?',
style: TextStyle(
fontWeight: FontWeight.bold
)
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ButtonTheme(
minWidth: 50.0,
height: 10.0,
child:
RaisedButton(onPressed: () => _audioCache.play('assets/hello_and_welcome.mp4'),
child: Icon(Icons.arrow_right),
color: Colors.grey[400],
),
),
SizedBox(width: 100.0,),
ButtonTheme(
minWidth: 50.0,
height: 10.0,
child:
RaisedButton(onPressed: () => _audioCache.play('assets/hello_and_welcome.mp4'),
child: Icon(Icons.arrow_right),
color: Colors.grey[400],
),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text(' Cảm ơn bạn.',
style: TextStyle(
fontWeight: FontWeight.bold
)
),
SizedBox(width:100.0),
Text('Làm thế nào để tôi giúp bạn?',
style: TextStyle(
fontWeight: FontWeight.bold
)
),
],
),
Divider(
height: 60.0,
color: Colors.black,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
RaisedButton(onPressed: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) =>
SimpleRecorder()),
);
},
child: Icon(Icons.arrow_right),
color: Colors.grey[400],
),
],
),
],
),
);
}
}
You can have a GestureDetector wrapped around your widget like so,您可以像这样将GestureDetector包裹在您的小部件周围,
GestureDetector(
onTap: () {
// your function here.
},
child: MyWidget()
),
or you can use InkWell to get the ripple effect aswell like so,或者你也可以像这样使用InkWell来获得涟漪效果,
InkWell(
onTap: () {
// your function here.
},
child: MyWidget()
)
For audio, take a look at audio players .对于音频,请查看音频播放器。
First, why not use https://pub.dev/packages/assets_audio_player ?首先,为什么不使用https://pub.dev/packages/assets_audio_player ? Second, why play local files?
二、为什么要播放本地文件? Why not loading it from an internet?
为什么不从互联网加载它? Third, you are using mix of mp3 and mp4 files.
第三,您正在混合使用 mp3 和 mp4 文件。 Last is video, I do not think it played by audio library you use.
最后是视频,我不认为它是由您使用的音频库播放的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.