Playing sound when certain word in string is pressed in Flutter - string

I am making Flutter application that will display stories for kids. In the story text I am using there are certain words such as rustling, honking, etc. For example, "Mimi and Sami heard a honking sound", and I want my application to play corresponding sounds when these words are clicked. My question is that how can I control the behaviour of any word in String so that I am able to play relevant sounds when those buttons are clicked?
So far I have not been able to find any solution for this. My question is not about playing sound in general, but controlling behavior of any word in a String. I understant TextSpan might be of any help, but I guess it would be hectic to use it.

You can use the GestureRecognizer from import 'package:flutter/gestures.dart';
RichText(
text: TextSpan(
children: [
TextSpan(
text: 'Single tap',
recognizer: TapGestureRecognizer()..onTap = () {
// play music here
},
),
TextSpan(
text: ' Double tap',
recognizer: DoubleTapGestureRecognizer()..onDoubleTap = () {
// play music here
}
),
TextSpan(
text: 'Long press',
recognizer: LongPressGestureRecognizer()..onLongPress = () {
// Long Pressed.
},
),
],
),
)

Related

How to add custom (string based) bullet list into wagtail WYSIWYG

I am looking for a way to register a new editor feature to the wagtail WYSIWYG editor.
The following code adds the bullet list to the editor. (It is also built-in).
#hooks.register('register_rich_text_features')
def register_arrow_styling(features):
""" Add the <arrow> to the ritch text editoe."""
feature_name = "somename"
type_ = "unordered-list-item"
tag = "li"
control = {
'type': type_,
'label': 'arrow',
'description': 'list item',
}
features.register_editor_plugin(
'draftail',
feature_name,
draftail_features.BlockFeature(control)
)
db_conversion = {
'from_database_format': {
"ul": ListElementHandler(type_),
tag: ListItemElementHandler(),
},
'to_database_format': {
'block_map': {type_: {
"element": tag,
"wrapper": "ul",
"props": {
"class": "li-arrow",
}
}}
},
}
features.register_converter_rule(
'contentstate',
feature_name,
db_conversion
)
# This will register this feature with all editors by default
features.default_features.append(feature_name)
What I need is to make the editor adds based list points (say "-") instead of the regular bullet points, so I can style it as a text, e.g. change color.
The main goal actually is to get the freedom of changing the bullet points colors.
Thanks

How do you make a text clickable in jetpack compose ? I would also like to toggle it to non clickable after selecting once

Text(
text = "Resend OTP",
fontSize = 20.sp,
color = Textfieldcolor,
style = TextStyle(textDecoration = TextDecoration.Underline)
)
//This is my code this text should be selectable once and then be disabled .
You can add the clickable modifier or can use the ClickableText:
var enabled by remember { mutableStateOf(true)}
ClickableText(
text = AnnotatedString(text) ,
onClick = {
if (enabled) {
enabled = false
text = "Disabled"
}
})
Like #Arpit mentioned it would be better to use a TextButton for this purpose.
But if you absolutely want to use Text, You can use following snippet.
#Composable
fun OneTimeClickableText(text : String, onClick : () -> Unit){
var enabled by rememberSaveable{ mutableStateOf(true)}
Text(
modifier = Modifier
.clickable(enabled = enabled) {
enabled = false
onClick()
},
text = text
)
}
That said I this code is strictly for one-time clickable text. I won't recommend using it for something like OTP button; as user won't be able to click it unless they restart your app. You can pull the enabled variable and manage it from outside(e.g. keeping it disabled for certain amount of time rather than permanently).

type 'String' is not a subtype of type 'List<String>' in type cast?

How can I fix that? I can't run the code because there is an error at String
Text(
questions[_questionsIndex]['questionText'] as String,
style: TextStyle(color: Colors.orange, fontSize: 30),
),
...(questions[_questionsIndex]['questionText'] as List<String>).map((answer) {
return Answer(_answerQuestions, answer);
}).toList()
The problem is this:
questions[_questionsIndex]['questionText'] as String
questions[_questionsIndex]['questionText'] as List<String>
How can questions[_questionsIndex]['questionText'] be a String and a List<String> at the same time?
I can't know for sure since you didn't show whats inside of questions variable and we don't know what you want to do but you probably want to do something like this:
...(questions as List<Map<String, dynamic>>).map((question) {
return Answer(_answerQuestions, question['questionText']);
}).toList()
I decided to do an online course at Udemy (flutter) and this code was shown, to load the questions & answers by a quiz:
Question(
questions[_questionsIndex]['questionText'],
),
...(questions[_questionsIndex]['questionText'] as List<String>).map((answer) {
return Answer(_answerQuestions, answer);
}).toList()
but actually it doesn't work. I tried to put 'as String' to the first Line, but the Emulator does show an error. So I tried your Version but it doesn't helped me

Chat Bot Hero card title message not wrapped properly

I have created hero card for messages with prompt buttons from qna maker. The hero card embedded response has a title and buttons. The buttons are displayed properly and worked as expected, but the title words are not wrapped properly.
if (resResult) {
var answer = resResult.answer;
var resultContext = resResult.context;
var prompts = resultContext && resultContext.prompts;
if (prompts && prompts.length) {
var card = CardFactory.heroCard(
answer,
[],
prompts.map(prompt => ({
type: 'messageBack',
title: prompt.displayText,
displayText: prompt.displayText,
text: prompt.displayText,
value: {
qnaId: prompt.qnaId
}
}))
);
answer = MessageFactory.attachment(card);
}
await context.sendActivity(answer);
}
The output response in chat window / Emulator is
The title text which is displayed needs to wrapped and font style and color should align with common text styles of chat bot.
Thanks in advance

How to write condition statements in flutter?

I wanted to learn how can I apply "if" condition in my flutter code? As I am new to dart language coding.
Suppose in a code like below, i want to add condition that if the counter's value is 1 then "You have pushed the button $_counter time" else "You have pushed the button $_counter times"
children: <Widget>[
new Text(
'You have pushed the button $_counter times:',
)/*,
new Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),*/
]
P.S. its just a simple example for me to understand how to use if condition in flutter.
For such simple cases you can use the ternary if ?: inside string interpolation:
new Text(
'You have pushed the button $_counter time${_counter != 1 ? 's' : ''}:',
)
for the case of the conditions you have just a single condition then you can use the basic ternary operators,
child: Text(
'{fee=="FREE"?"Free":fee}',
),
But if you have multiple conditions or value that need to compare from the index position(i.e. you have to fetch value and then need to put in condition) in listview then you can add method as your text value in your Widget as follows:
child: Text(
'${checkForPrice(index)}',
),
),
checkForPrice(int index) {
String strFee = data['payload']['webinar'][index]['fee'];
String finalFee = "";
if (strFee == "FREE") {
finalFee = 'FREE';
} else {
finalFee = '\$ ${data['payload']['webinar'][index]['fee']}';
}
return finalFee;}
This will definitely help you in showing data with multiple conditions.
at the first time i tried flutter i have no idea what dart language is but after some browsing i found this useful documentation. You can find anything you looking for right there including if statement that you are asking about. But i can tell you the if statement works the same as any other language such as JavaScript, Java, etc.
Edit: here i give you some example how to use it in flutter
Future<Null> toTheHomeScreen() async {
await _signInWithGoogle();
SharedPreferences sprefs = await SharedPreferences.getInstance();
if (sprefs.getString('idToken').isNotEmpty) {
new Future.delayed(new Duration(seconds: 3), () {
new CircularProgressIndicator();
Navigator.of(context).push(
new MaterialPageRoute(
builder: (BuildContext context) {
return new HomeScreen();
}
)
);
});
} else {
Navigator.of(context).pop();
}
}
the if statement check if there is any key named 'idToken' stored in sharedpref, if it not empty user will redirect to home page.
I hope this will help you.

Resources