【发布时间】:2021-01-19 05:49:37
【问题描述】:
我在 Flutter 中制作了这个应用程序,当我说什么时它会将其转换为文本,estoy usando el plugin tts,但你知道我如何将文本传递给输入,即将文本转换为可编辑的文本在一个输入(TextFormField)中,我在这里留下我的代码,希望你发自内心的帮助。
我已经在论坛中搜索了更多信息,但目前还没有。
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(
debugShowCheckedModeBanner: false,
title: 'Material App',
home: Scaffold(
appBar: _appBar(context),
body: Stack(children: <Widget>[TextVoice()]),
),
);
}
_appBar(BuildContext context) {
return PreferredSize(
preferredSize: Size.fromHeight(50),
child: Container(
alignment: Alignment.bottomCenter,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.centerLeft,
end: Alignment.centerRight,
colors: [Colors.pink[900], Colors.red[800]])),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
IconButton(
icon: Icon(Icons.chevron_left),
color: Colors.white,
onPressed: () {
Navigator.pop(context);
}),
Text('Texto a Voz',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w500,
color: Colors.white,
)),
IconButton(
icon: Icon(Icons.help_outline),
onPressed: () {},
color: Colors.white,
)
],
),
));
}
}
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();
}
_fondoApp() {
final gradiente = Container(
width: double.infinity,
height: double.infinity,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: FractionalOffset(0.0, 0.0),
end: FractionalOffset(0.0, 1.0),
colors: [Colors.black87, Colors.black])),
);
return Stack(
children: <Widget>[gradiente],
);
}
return Container(
alignment: Alignment.topCenter,
child: Stack(children: <Widget>[
_fondoApp(),
(SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Column(
children: [
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: 13,
),
),
OutlineButton.icon(
borderSide: BorderSide.none,
label: Text('Escuchar'),
textColor: Colors.green,
icon: Icon(Icons.play_arrow),
onPressed: () => _speak(textEditingController.text),
),
OutlineButton.icon(
borderSide: BorderSide.none,
textColor: Colors.red,
label: Text('Detener'),
icon: Icon(Icons.stop),
onPressed: () => _stop(),
)
],
),
)),
]),
);
}
}
它只是将结果传递给输入中的可编辑文本。
【问题讨论】:
-
抱歉,您的问题有点不清楚您到底遇到了什么错误?
-
我使用了你的代码,到目前为止我的理解是,当我在文本字段中输入内容然后按下绿色按钮时,我的手机就会准确说出文本字段中的内容。所以我的问题是你到底想要什么?
-
我需要文本从一个地方到脚手架或容器,传递到一个入口,并让它变成可修改的文本,就是这样。
-
也就是说,不是“只是文本”,而是转到输入中的可修改文本,或者在本例中为 TextFormField
标签: flutter text text-to-speech edit speech-to-text