|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | +import 'dart:typed_data'; |
| 7 | +import 'package:flutter/material.dart'; |
| 8 | +import 'package:getwidget/getwidget.dart'; |
| 9 | +import 'package:mediapipe_text/mediapipe_text.dart'; |
| 10 | +import 'enumerate.dart'; |
| 11 | + |
| 12 | +class LanguageDetectionDemo extends StatefulWidget { |
| 13 | + const LanguageDetectionDemo({super.key, this.detector}); |
| 14 | + |
| 15 | + final LanguageDetector? detector; |
| 16 | + |
| 17 | + @override |
| 18 | + State<LanguageDetectionDemo> createState() => _LanguageDetectionDemoState(); |
| 19 | +} |
| 20 | + |
| 21 | +class _LanguageDetectionDemoState extends State<LanguageDetectionDemo> |
| 22 | + with AutomaticKeepAliveClientMixin<LanguageDetectionDemo> { |
| 23 | + final TextEditingController _controller = TextEditingController(); |
| 24 | + final Completer<LanguageDetector> _completer = Completer<LanguageDetector>(); |
| 25 | + final results = <Widget>[]; |
| 26 | + String? _isProcessing; |
| 27 | + |
| 28 | + @override |
| 29 | + void initState() { |
| 30 | + super.initState(); |
| 31 | + _controller.text = 'Quiero agua, por favor'; |
| 32 | + _initDetector(); |
| 33 | + } |
| 34 | + |
| 35 | + Future<void> _initDetector() async { |
| 36 | + if (widget.detector != null) { |
| 37 | + return _completer.complete(widget.detector!); |
| 38 | + } |
| 39 | + |
| 40 | + ByteData? bytes = await DefaultAssetBundle.of(context) |
| 41 | + .load('assets/language_detector.tflite'); |
| 42 | + |
| 43 | + final detector = LanguageDetector( |
| 44 | + LanguageDetectorOptions.fromAssetBuffer( |
| 45 | + bytes.buffer.asUint8List(), |
| 46 | + ), |
| 47 | + ); |
| 48 | + _completer.complete(detector); |
| 49 | + bytes = null; |
| 50 | + } |
| 51 | + |
| 52 | + void _prepareForDetection() { |
| 53 | + setState(() { |
| 54 | + _isProcessing = _controller.text; |
| 55 | + results.add(const CircularProgressIndicator.adaptive()); |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + Future<void> _detect() async { |
| 60 | + _prepareForDetection(); |
| 61 | + _completer.future.then((detector) async { |
| 62 | + final result = await detector.detect(_controller.text); |
| 63 | + _showDetectionResults(result); |
| 64 | + result.dispose(); |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + void _showDetectionResults(LanguageDetectorResult result) { |
| 69 | + setState( |
| 70 | + () { |
| 71 | + results.last = Card( |
| 72 | + key: Key('prediction-"$_isProcessing" ${results.length}'), |
| 73 | + margin: const EdgeInsets.all(10), |
| 74 | + child: Column( |
| 75 | + children: [ |
| 76 | + Padding( |
| 77 | + padding: const EdgeInsets.all(10), |
| 78 | + child: Text(_isProcessing!), |
| 79 | + ), |
| 80 | + Padding( |
| 81 | + padding: const EdgeInsets.all(10.0), |
| 82 | + child: Wrap( |
| 83 | + children: <Widget>[ |
| 84 | + ...result.predictions |
| 85 | + .enumerate<Widget>( |
| 86 | + (prediction, index) => _languagePrediction( |
| 87 | + prediction, |
| 88 | + predictionColors[index], |
| 89 | + ), |
| 90 | + // Take first 4 because the model spits out dozens of |
| 91 | + // astronomically low probability language predictions |
| 92 | + max: predictionColors.length, |
| 93 | + ) |
| 94 | + .toList(), |
| 95 | + ], |
| 96 | + ), |
| 97 | + ), |
| 98 | + ], |
| 99 | + ), |
| 100 | + ); |
| 101 | + _isProcessing = null; |
| 102 | + }, |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + static final predictionColors = <Color>[ |
| 107 | + Colors.blue[300]!, |
| 108 | + Colors.orange[300]!, |
| 109 | + Colors.green[300]!, |
| 110 | + Colors.red[300]!, |
| 111 | + ]; |
| 112 | + |
| 113 | + Widget _languagePrediction(LanguagePrediction prediction, Color color) { |
| 114 | + return Padding( |
| 115 | + padding: const EdgeInsets.only(right: 8), |
| 116 | + child: GFButton( |
| 117 | + onPressed: null, |
| 118 | + text: '${prediction.languageCode} :: ' |
| 119 | + '${prediction.probability.roundTo(8)}', |
| 120 | + shape: GFButtonShape.pills, |
| 121 | + color: color, |
| 122 | + ), |
| 123 | + ); |
| 124 | + } |
| 125 | + |
| 126 | + @override |
| 127 | + Widget build(BuildContext context) { |
| 128 | + super.build(context); |
| 129 | + return Scaffold( |
| 130 | + body: SafeArea( |
| 131 | + child: Padding( |
| 132 | + padding: const EdgeInsets.all(16.0), |
| 133 | + child: SingleChildScrollView( |
| 134 | + child: Column( |
| 135 | + children: <Widget>[ |
| 136 | + TextField(controller: _controller), |
| 137 | + ...results.reversed, |
| 138 | + ], |
| 139 | + ), |
| 140 | + ), |
| 141 | + ), |
| 142 | + ), |
| 143 | + floatingActionButton: FloatingActionButton( |
| 144 | + onPressed: |
| 145 | + _isProcessing != null && _controller.text != '' ? null : _detect, |
| 146 | + child: const Icon(Icons.search), |
| 147 | + ), |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + @override |
| 152 | + bool get wantKeepAlive => true; |
| 153 | +} |
| 154 | + |
| 155 | +extension on double { |
| 156 | + double roundTo(int decimalPlaces) => |
| 157 | + double.parse(toStringAsFixed(decimalPlaces)); |
| 158 | +} |
0 commit comments