Home > Software design >  Playing sound when certain word in string is pressed in Flutter
Playing sound when certain word in string is pressed in Flutter

Time:11-07

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.

CodePudding user response:

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