我有这个将文本转换为音频的程序,它可以正常工作,但到目前为止我还没有能够整合和找到有关显示“播放”和“关闭”文本的信息,以便在播放时它显示“正在播放”,反之亦然,我非常感谢您的帮助,非常感谢。
这是我的代码。
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_tts/flutter_tts.dart';
class TextVoicePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: TextVoice(),
),
);
}
class TextVoice extends StatelessWidget {
final FlutterTts flutterTts = FlutterTts();
@override
Widget build(BuildContext context) {
TextEditingController textEditingController = TextEditingController();
Future _speak(String text) async {
await flutterTts.setLanguage("es-MX");
await flutterTts.setPitch(1);
await flutterTts.speak(text);
}
Future _stop() async {
await flutterTts.stop();
}
return Scaffold(
body: Stack(
children: <Widget>[
Padding(
padding: EdgeInsets.all(30.0),
child: TextFormField(
decoration: InputDecoration(
hintText: 'Introduce el texto.',
labelStyle: TextStyle(color: Colors.white70, fontSize: 16.0),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.pink[900]),
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.pink[50]),
),
hintStyle: TextStyle(color: Colors.white30, fontSize: 16.0),
border: OutlineInputBorder(),
),
controller: textEditingController,
cursorColor: Colors.white,
style: TextStyle(color: Colors.white70, fontSize: 16.0),
maxLines: null,
),
),
],
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
bottomNavigationBar: Container(
color: Colors.black,
height: 40.0,
alignment: Alignment.topCenter,
),
floatingActionButton: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
FloatingActionButton(
backgroundColor: Colors.green,
heroTag: "btn",
onPressed: () => _speak(textEditingController.text),
child: Icon(Icons.play_arrow),
),
SizedBox(
width: 40,
),
FloatingActionButton(
backgroundColor: Colors.red,
heroTag: "btn2",
onPressed: () => _stop(),
child: Icon(Icons.stop),
)
],
),
));
}
}