Flutter/Dart - How can I play sounds from a list using onPressed with a flatButton in flutter_Swiper package? - flutter

G'day Stackoverflowers!
I have started learning Flutter/Dart for a personal project and I came across a problem I can't solve. I would really appreciate a bit of help, please.
I'm building an application to learn English (I'm an English teacher and author) and one of the pages of the app is where you can see pictures of different foods and drinks with their name written and the possibility to listen to the pronunciation of the food or drink.
here is a snippet of the code so far:
Swiper(
itemWidth: MediaQuery.of(context).size.width,
itemHeight: MediaQuery.of(context).size.height,
layout: SwiperLayout.TINDER,
itemCount: 43,
indicatorLayout: PageIndicatorLayout.WARM,
itemBuilder: (BuildContext context, int index,) {
return Card(
elevation: 5.0,
child: Column(
children: <Widget>[
FlatButton(
onPressed: () => {
foodSounds[index],
},
child: Row(
children: <Widget>[
SizedBox(
width: 70.0,
),
Icon(Icons.volume_up),
SizedBox(
width: 30.0,
),
Text(foodNouns[index],
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w500,
)),
],
),
),
Image.asset(foodPictures[index], fit: BoxFit.contain),
],
),
);
As you can probably guess, I have no problem displaying the pictures I want and their respective nouns using a list and the index of this list.
Here are the lists I've got for the pictures and nouns
final List foodPictures = [
'images/foodnouns/apple.jpg',
'images/foodnouns/banana.jpg',
'images/foodnouns/bar.jpg',
'images/foodnouns/beer.jpg',
'images/foodnouns/biscuits.jpg',
'images/foodnouns/bread.jpg',
'images/foodnouns/breakfast.jpg',
'images/foodnouns/butter.jpg',
'images/foodnouns/cafe.jpg',
'images/foodnouns/cake.jpg',
'images/foodnouns/cheese.jpg',
'images/foodnouns/chocolate.jpg',
'images/foodnouns/coffee.jpg',
'images/foodnouns/cup.jpg',
'images/foodnouns/cup_of_coffee.jpg',
'images/foodnouns/dinner.jpg',
'images/foodnouns/egg.jpg',
'images/foodnouns/fish.jpg',
'images/foodnouns/food.jpg',
'images/foodnouns/fries.jpg',
'images/foodnouns/fruits.jpg',
'images/foodnouns/glass.jpg',
'images/foodnouns/ice_cream.jpg',
'images/foodnouns/juice.jpg',
'images/foodnouns/knife.jpg',
'images/foodnouns/lunch.jpg',
'images/foodnouns/meal.jpg',
'images/foodnouns/meat.jpg',
'images/foodnouns/milk.jpg',
'images/foodnouns/orange.jpg',
'images/foodnouns/picnic.jpg',
'images/foodnouns/pizza.jpg',
'images/foodnouns/plate.jpg',
'images/foodnouns/potatoes.jpg',
'images/foodnouns/rice.jpg',
'images/foodnouns/salt.jpg',
'images/foodnouns/sandwich.jpg',
'images/foodnouns/soup.jpg',
'images/foodnouns/sugar.jpg',
'images/foodnouns/tea.jpg',
'images/foodnouns/tomato.jpg',
'images/foodnouns/vegetables.jpg',
'images/foodnouns/wine.jpg',];
final List foodNouns =[
'apple',
'banana',
'bar',
'beer',
'biscuits',
'bread',
'breakfast',
'butter',
'cafe',
'cake',
'cheese',
'chocolate',
'coffee',
'cup',
'cup_of_coffee',
'dinner',
'egg',
'fish',
'food',
'fries',
'fruits',
'glass',
'ice_cream',
'juice',
'knife',
'lunch',
'meal',
'meat',
'milk',
'orange',
'picnic',
'pizza',
'plate',
'potatoes',
'rice',
'salt',
'sandwich',
'soup',
'sugar',
'tea',
'tomato',
'vegetables',
'wine',];
This is how it looks like so far:
overview of the page
Now I would like to be able to play a sound for each card. I have the sounds in my assets in the following directory : assets: -assets/foodsounds/
I want to be able to play the sound from a list of my sounds. here is the list :
final List foodSounds=[
'assets/foodsounds/apple.mp3',
'assets/foodsounds/banana.mp3',
'assets/foodsounds/bar.mp3',
'assets/foodsounds/beer.mp3',
'assets/foodsounds/biscuits.mp3',
'assets/foodsounds/bread.mp3',
'assets/foodsounds/breakfast.mp3',
'assets/foodsounds/butter.mp3',
'assets/foodsounds/café.mp3',
'assets/foodsounds/cake.mp3',
'assets/foodsounds/cheese.mp3',
'assets/foodsounds/chocolate.mp3',
'assets/foodsounds/coffee.mp3',
'assets/foodsounds/cup.mp3',
'assets/foodsounds/cup_of_coffee.mp3',
'assets/foodsounds/dinner.mp3',
'assets/foodsounds/egg.mp3',
'assets/foodsounds/fish.mp3',
'assets/foodsounds/food.mp3',
'assets/foodsounds/fries.mp3',
'assets/foodsounds/fruits.mp3',
'assets/foodsounds/glass.mp3',
'assets/foodsounds/ice_cream.mp3',
'assets/foodsounds/juice.mp3',
'assets/foodsounds/knife.mp3',
'assets/foodsounds/lunch.mp3',
'assets/foodsounds/meal.mp3',
'assets/foodsounds/meat.mp3',
'assets/foodsounds/milk.mp3',
'assets/foodsounds/orange.mp3',
'assets/foodsounds/picnic.mp3',
'assets/foodsounds/pizza.mp3',
'assets/foodsounds/plate.mp3',
'assets/foodsounds/potatoes.mp3',
'assets/foodsounds/rice.mp3',
'assets/foodsounds/salt.mp3',
'assets/foodsounds/sandwich.mp3',
'assets/foodsounds/soup.mp3',
'assets/foodsounds/sugar.mp3',
'assets/foodsounds/tea.mp3',
'assets/foodsounds/tomato.mp3',
'assets/foodsounds/vegetables.mp3',
'assets/foodsounds/wine.mp3',];
I thought about using the audioplayers-0.14.0 package like so :
void playFoodSound(int foodSounds) {
final player = AudioCache(prefix: 'foodsounds/');
player.play('$foodSounds.mp3');
}
and use it in Swiper like the following:
FlatButton(
onPressed: () => {
foodSounds[index],
},
This method doesn't work. I have tried a few others and I really can't figure out how to play those sounds...
I thank you all very much in advance for your help because I really don't want to have to hardcode those sounds.

Turns out I finally found my answer.
I used the assets_audio_player 1.2.3 package.
Declared the following vaiable:
final AssetsAudioPlayer playFoodSound= AssetsAudioPlayer();
and used the following statement in my code :
FlatButton(onPressed: () =>{
playFoodSound.open(foodSounds[index],),
},
And it works like a charm.

Related

Flutter searchfield -how to connect it to API-

I would like to know how to connect an API with the --searchfield-- since at the moment in the documentation I don't fully understand how it works, with an API
final List<String> _suggestions = ['Equipo', 'Marca', 'Modelo'];
and this serial is the one that normally has the documentation, but I can't connect it to an api like
https://jsonplaceholder.typicode.com/albums
SearchField(
controller: con.etiqueta,
suggestionState: Suggestion.expand,
suggestionAction: SuggestionAction.next,
suggestions:
_suggestions.map((e) => SearchFieldListItem(e)).toList(),
textInputAction: TextInputAction.next,
//controller: _searchController,
hint: 'Etiqueta',
// initialValue: SearchFieldListItem(_suggestions[2], SizedBox()),
maxSuggestionsInViewPort: 3,
itemHeight: 45,
onSuggestionTap: (x) {},
),

Hint text is not visible in login_fresh package of flutter

I want to show hint text over the input box in flutter. And I did not find any hint key related to this package. It is neither visible on the login nor the sign-up page.
LoginFresh buildLoginFresh() {
List<LoginFreshTypeLoginModel> listLogin = [
LoginFreshTypeLoginModel(
callFunction: (BuildContext _buildContext) {
// develop what they want the facebook to do when the user clicks
},
logo: TypeLogo.facebook),
LoginFreshTypeLoginModel(
callFunction: (BuildContext _buildContext) {
// develop what they want the Google to do when the user clicks
},
logo: TypeLogo.google),
LoginFreshTypeLoginModel(
callFunction: (BuildContext _buildContext) {
Navigator.of(_buildContext).push(
MaterialPageRoute(
builder: (_buildContext) => widgetLoginFreshUserAndPassword(),
),
);
},
logo: TypeLogo.userPassword,
),
];
return LoginFresh(
pathLogo: 'assets/images/horario.jpg',
backgroundColor: Colors.black,
textColor: Colors.black,
isExploreApp: false,
functionExploreApp: () {
// develop what they want the ExploreApp to do when the user clicks
},
isFooter: false,
widgetFooter: widgetFooter(),
typeLoginModel: listLogin,
isSignUp: true,
widgetSignUp: widgetLoginFreshSignUp(),
);
}
Click here is view screenshot
Wrap your LoginFreshUserAndPassword with the Theme widget like this :
Theme(
data: ThemeData(hintColor: Colors.red),
child: LoginFreshUserAndPassword(),
)
it should work now.

Flutter "Rate my App" always requires a Review when using native dialog on Android

I'm using the 'Rate my App' package to ask for rates in my app. https://pub.dev/packages/rate_my_app
I'm using the same code shown in the package page, but i'm setting the 'ignoreNativeDialog' parameter to false in order to use the native API even in Android. But when the dialog appears on android always requires a review after a rate.
Is it any way to set the review to be optional?
RateMyApp rateMyApp = RateMyApp(
preferencesPrefix: 'rateMyApp_',
minDays: 7,
minLaunches: 10,
remindDays: 7,
remindLaunches: 10,
);
rateMyApp.init().then((_) {
if (rateMyApp.shouldOpenDialog) {
rateMyApp.showStarRateDialog(
context,
title: 'Rate this app',
message: 'You like this app ? Then take a little bit of your time to leave a rating :',
actionsBuilder: (context, stars) {
return [
FlatButton(
child: Text('OK'),
onPressed: () async {
print('Thanks for the ' + (stars == null ? '0' : stars.round().toString()) + ' star(s) !');
await rateMyApp.callEvent(RateMyAppEventType.rateButtonPressed);
Navigator.pop<RateMyAppDialogButton>(context, RateMyAppDialogButton.rate);
},
),
];
},
ignoreNativeDialog: false, // This is the one parameter i set to false
dialogStyle: DialogStyle(
titleAlign: TextAlign.center,
messageAlign: TextAlign.center,
messagePadding: EdgeInsets.only(bottom: 20),
),
starRatingOptions: StarRatingOptions(),
onDismissed: () => rateMyApp.callEvent(RateMyAppEventType.laterButtonPressed),
);
}
});
Now it's clear.
The review was required becouse I was using it on a registered tester account. I tested with another account and it worked fine.

Reading static variable during its initialization => Flutter Navigator.push

I have a list of statefull widget which I use to navigate from pages. but when I was trying to push these List element in Navigator.push(), it says "Reading static variable during its initialization".
here's the list:
List<ActionScreen> fullBodyNavigation = [
ActionScreen(title: 'demo data', number: 20, gifDirectory: "assets/gifs/demodata.gif", currentList: fullBodyNavigation,
frameMin: 0, frameMax: 17, milisecondAnimation: 800, milisecondTimer: 800, sessionNumber: 1,),
ActionScreen(title: 'demodata', number: 15, gifDirectory: "assets/gifs/demodata.gif", currentList: fullBodyNavigation,
frameMin: 0, frameMax: 30, milisecondAnimation: 1200, milisecondTimer: 1200, sessionNumber: 2,),
ActionScreen(title: 'demodata', number: 15, gifDirectory: "assets/gifs/demodata.gif", currentList: fullBodyNavigation,
frameMin: 0, frameMax: 2, milisecondAnimation: 2000, milisecondTimer: 2000, sessionNumber: 3,),
.............
]
here's the push method:
onPress: () {
Navigator.push(context, MaterialPageRoute(builder: (context) => fullBodyNavigation[0],),);
},
I foud a solution in stackoverflow which said I have to use 'data' such as "fullBodyNavigation.data[0]". but ide said "getter data is not defined". can anyone pls help?
here's that solution's link: Reading static variable during its initialization | Flutter
The problem is in the way you have created the navigation pages list:
List<ActionScreen> fullBodyNavigation = [
ActionScreen(title: 'demo data',
number: 20,
gifDirectory: "assets/gifs/demodata.gif",
currentList: fullBodyNavigation,
frameMin: 0,
frameMax: 17,
milisecondAnimation: 800,
milisecondTimer: 800,
sessionNumber: 1,
),
.............
]
Why do you want to pass currentList: fullBodyNavigation to ActionScreen. So basically, you are trying to access the list inside the list itself. The chicken and egg problem. THat's what compiler is warning you with the error.
To fix this, dont pass currentList: fullBodyNavigation to ActionScreen. Instead make fullBodyNavigation a static field and in ActionScreen just access it with the class name. That should do your thing and also make the compiler happy

using 2 formtextfield throws exception '<optimized out>': is not true

I'm using 2 FormTextField in a form, both take numbers , first for landline and second for mobile number,when I use only one field the apps works fine but when I add the second field I get this exception when the page loads :
════════ Exception caught by rendering library ══════
The following assertion was thrown during paint():
'dart:ui/text.dart': Failed assertion: line 1372: '': is not true.
this is the code :
Flexible(
flex: 1,
fit: FlexFit.loose,
child: Form_Text_Field_Number(
tf_label: 'Landline # ',
fieldController: landlineFieldController,
icon: Icons.phone,
),
),
Flexible(
flex: 1,
fit: FlexFit.loose,
child: Form_Text_Field_Number(
tf_label: 'Mobile #',
fieldController: mobileFieldController,
icon: Icons.phone_android,
),
),
the design of both fields is the same based on FormTextField..only field name differ.
I tried some suggestions like placing the text field in a Container ...but non solved the issue.
why this happen only when adding the second field , what could be the reason ?
and how to solve.