flutter_translate_plugin 0.0.1 flutter_translate_plugin: ^0.0.1 copied to clipboard
flutter 百度翻译插件
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter_translate_plugin/flutter_translate_plugin.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
String asrResult = '';
String transResult = '';
String errMsg = '';
@override
void initState() {
super.initState();
// FlutterTranslatePlugin.initSdk(APP_ID: '20201031000604415', SECRET_KEY: '6wzvN6lraoSRu1bIKEDk');
FlutterTranslatePlugin.initSdk(APP_ID: '20171129000100831', SECRET_KEY: 'drh0PUP3ULRCPtNTzhq8');
FlutterTranslatePlugin().onLocationChanged().listen((event) {
print(event.success);
setState(() {
if(event.success){
asrResult = event.asrResult;
transResult = event.transResult;
}else{
errMsg = event.errMsg;
}
});
});
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
platformVersion =
await FlutterTranslatePlugin.platformVersion ?? 'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Column(
children: [
Center(
child: Text('Running on: $_platformVersion\n'),
),
ElevatedButton(onPressed: () async {
FlutterTranslatePlugin.startRecognize(form: 'zh', to: 'en');
}, child: Text('开始翻译')),
ElevatedButton(onPressed: () async {
var res = await FlutterTranslatePlugin.stopRecognize();
print(res);
}, child: Text('结束翻译')),
Text('识别出的内容:' + asrResult.toString()),
Text('翻译结果:' + transResult.toString()),
Text('错误信息:' + errMsg.toString()),
],
),
),
);
}
}