Skip to content

Commit f45a04a

Browse files
committed
Remove deprecated withOpacity and swap it for withValues
1 parent 571136d commit f45a04a

File tree

11 files changed

+63
-167
lines changed

11 files changed

+63
-167
lines changed

example/audio_classification/lib/main.dart

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,14 @@ class MyHomePage extends StatefulWidget {
5252
}
5353

5454
class _MyHomePageState extends State<MyHomePage> {
55-
static const platform =
56-
MethodChannel('org.tensorflow.audio_classification/audio_record');
55+
static const platform = MethodChannel('org.tensorflow.audio_classification/audio_record');
5756

5857
// The YAMNet/classifier model used in this code example accepts data that
5958
// represent single-channel, or mono, audio clips recorded at 16kHz in 0.975
6059
// second clips (15600 samples).
6160
static const _sampleRate = 16000; // 16kHz
6261
static const _expectAudioLength = 975; // milliseconds
63-
final int _requiredInputBuffer =
64-
(16000 * (_expectAudioLength / 1000)).toInt();
62+
final int _requiredInputBuffer = (16000 * (_expectAudioLength / 1000)).toInt();
6563
late AudioClassificationHelper _helper;
6664
List<MapEntry<String, double>> _classification = List.empty();
6765
final List<Color> _primaryProgressColorList = [
@@ -102,10 +100,7 @@ class _MyHomePageState extends State<MyHomePage> {
102100

103101
Future<bool> _requestPermission() async {
104102
try {
105-
return await platform.invokeMethod('requestPermissionAndCreateRecorder', {
106-
"sampleRate": _sampleRate,
107-
"requiredInputBuffer": _requiredInputBuffer
108-
});
103+
return await platform.invokeMethod('requestPermissionAndCreateRecorder', {"sampleRate": _sampleRate, "requiredInputBuffer": _requiredInputBuffer});
109104
} on Exception catch (e) {
110105
log("Failed to create recorder: '${e.toString()}'.");
111106
return false;
@@ -115,8 +110,7 @@ class _MyHomePageState extends State<MyHomePage> {
115110
Future<Float32List> _getAudioFloatArray() async {
116111
var audioFloatArray = Float32List(0);
117112
try {
118-
final Float32List result =
119-
await platform.invokeMethod('getAudioFloatArray');
113+
final Float32List result = await platform.invokeMethod('getAudioFloatArray');
120114
audioFloatArray = result;
121115
} on PlatformException catch (e) {
122116
log("Failed to get audio array: '${e.message}'.");
@@ -160,8 +154,7 @@ class _MyHomePageState extends State<MyHomePage> {
160154

161155
Future<void> _runInference() async {
162156
Float32List inputArray = await _getAudioFloatArray();
163-
final result =
164-
await _helper.inference(inputArray.sublist(0, _requiredInputBuffer));
157+
final result = await _helper.inference(inputArray.sublist(0, _requiredInputBuffer));
165158
setState(() {
166159
// take top 3 classification
167160
_classification = (result.entries.toList()
@@ -186,7 +179,7 @@ class _MyHomePageState extends State<MyHomePage> {
186179
backgroundColor: Colors.white,
187180
appBar: AppBar(
188181
title: Image.asset('assets/images/tfl_logo.png'),
189-
backgroundColor: Colors.black.withOpacity(0.5),
182+
backgroundColor: Colors.black.withValues(alpha: (0.5)),
190183
),
191184
body: _buildBody(),
192185
);
@@ -216,10 +209,8 @@ class _MyHomePageState extends State<MyHomePage> {
216209
),
217210
Flexible(
218211
child: LinearProgressIndicator(
219-
backgroundColor: _backgroundProgressColorList[
220-
index % _backgroundProgressColorList.length],
221-
color: _primaryProgressColorList[
222-
index % _primaryProgressColorList.length],
212+
backgroundColor: _backgroundProgressColorList[index % _backgroundProgressColorList.length],
213+
color: _primaryProgressColorList[index % _primaryProgressColorList.length],
223214
value: item.value,
224215
minHeight: 20,
225216
))

example/bertqa/lib/ui/qa_detail.dart

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ import 'package:bertqa/ml/qa_client.dart';
1919
import 'package:flutter/material.dart';
2020

2121
class QaDetail extends StatefulWidget {
22-
const QaDetail(
23-
{super.key,
24-
required this.title,
25-
required this.content,
26-
required this.questions});
22+
const QaDetail({super.key, required this.title, required this.content, required this.questions});
2723

2824
final String title;
2925
final String content;
@@ -66,8 +62,7 @@ class _QaDetailState extends State<QaDetail> {
6662
if (!trimQuestion.endsWith("?")) {
6763
trimQuestion += "?";
6864
}
69-
List<QaAnswer> answers =
70-
await _qaClient.runInference(trimQuestion, widget.content);
65+
List<QaAnswer> answers = await _qaClient.runInference(trimQuestion, widget.content);
7166
// Highlight the answer here
7267
_highlightAnswer(answers.first);
7368
}
@@ -115,36 +110,15 @@ class _QaDetailState extends State<QaDetail> {
115110
style: Theme.of(context).textTheme.bodyMedium,
116111
)
117112
: RichText(
118-
text: TextSpan(
119-
style: Theme.of(context).textTheme.bodyMedium,
120-
children: [
121-
if (_answerIndex > 0)
122-
TextSpan(
123-
text: widget.content
124-
.substring(0, _answerIndex)),
125-
TextSpan(
126-
style: TextStyle(
127-
background: Paint()
128-
..color = Colors.yellow),
129-
text: widget.content.substring(_answerIndex,
130-
_answerIndex + _qaAnswer!.text.length)),
131-
if ((_answerIndex + _qaAnswer!.text.length) <
132-
widget.content.length)
133-
TextSpan(
134-
text: widget.content.substring(
135-
_answerIndex + _qaAnswer!.text.length,
136-
widget.content.length))
137-
]),
113+
text: TextSpan(style: Theme.of(context).textTheme.bodyMedium, children: [
114+
if (_answerIndex > 0) TextSpan(text: widget.content.substring(0, _answerIndex)),
115+
TextSpan(style: TextStyle(background: Paint()..color = Colors.yellow), text: widget.content.substring(_answerIndex, _answerIndex + _qaAnswer!.text.length)),
116+
if ((_answerIndex + _qaAnswer!.text.length) < widget.content.length) TextSpan(text: widget.content.substring(_answerIndex + _qaAnswer!.text.length, widget.content.length))
117+
]),
138118
))),
139119
Container(
140120
padding: const EdgeInsets.all(16),
141-
decoration: BoxDecoration(color: Colors.white, boxShadow: [
142-
BoxShadow(
143-
color: Colors.grey.withOpacity(0.5),
144-
spreadRadius: 2,
145-
blurRadius: 5,
146-
offset: const Offset(0, 3))
147-
]),
121+
decoration: BoxDecoration(color: Colors.white, boxShadow: [BoxShadow(color: Colors.grey.withValues(alpha: (0.5)), spreadRadius: 2, blurRadius: 5, offset: const Offset(0, 3))]),
148122
// color: Colors.white,
149123
child: Column(
150124
children: [
@@ -157,8 +131,7 @@ class _QaDetailState extends State<QaDetail> {
157131
child: ListView.separated(
158132
shrinkWrap: true,
159133
scrollDirection: Axis.horizontal,
160-
separatorBuilder: (BuildContext context, int index) =>
161-
const Divider(
134+
separatorBuilder: (BuildContext context, int index) => const Divider(
162135
indent: 16,
163136
),
164137
itemCount: widget.questions.length,
@@ -175,9 +148,7 @@ class _QaDetailState extends State<QaDetail> {
175148
Expanded(
176149
child: TextField(
177150
controller: _controller,
178-
decoration: const InputDecoration(
179-
border: UnderlineInputBorder(),
180-
labelText: "Text query"),
151+
decoration: const InputDecoration(border: UnderlineInputBorder(), labelText: "Text query"),
181152
onChanged: (text) {
182153
setState(() {
183154
_currentQuestion = text;
@@ -194,9 +165,7 @@ class _QaDetailState extends State<QaDetail> {
194165
_answerQuestion();
195166
}
196167
: null,
197-
style: ElevatedButton.styleFrom(
198-
disabledBackgroundColor: Colors.grey,
199-
backgroundColor: const Color(0xFFFFA800)),
168+
style: ElevatedButton.styleFrom(disabledBackgroundColor: Colors.grey, backgroundColor: const Color(0xFFFFA800)),
200169
child: const Icon(
201170
Icons.east,
202171
color: Colors.white,

example/digit_classification/lib/main.dart

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,13 @@ class _MyHomePageState extends State<MyHomePage> {
6565

6666
Future<void> _predictNumber() async {
6767
// capture sketch area
68-
RenderRepaintBoundary boundary =
69-
_globalKey.currentContext!.findRenderObject() as RenderRepaintBoundary;
68+
RenderRepaintBoundary boundary = _globalKey.currentContext!.findRenderObject() as RenderRepaintBoundary;
7069
ui.Image image = await boundary.toImage();
7170
final byteData = await image.toByteData(format: ui.ImageByteFormat.png);
7271
final inputImageData = byteData?.buffer.asUint8List();
7372

7473
final stopwatch = Stopwatch()..start();
75-
final (number, confidence) =
76-
await _digitClassifierHelper.runInference(inputImageData!);
74+
final (number, confidence) = await _digitClassifierHelper.runInference(inputImageData!);
7775
stopwatch.stop();
7876

7977
setState(() {
@@ -91,7 +89,7 @@ class _MyHomePageState extends State<MyHomePage> {
9189
title: Center(
9290
child: Image.asset('assets/images/tfl_logo.png'),
9391
),
94-
backgroundColor: Colors.black.withOpacity(0.5),
92+
backgroundColor: Colors.black.withValues(alpha: (0.5)),
9593
),
9694
body: Center(
9795
child: Column(
@@ -107,9 +105,7 @@ class _MyHomePageState extends State<MyHomePage> {
107105
children: [
108106
const Spacer(),
109107
const Text("Predicted number:"),
110-
if (_predictedNumber != null && _predictedConfidence != null)
111-
Text(
112-
"$_predictedNumber (${_predictedConfidence?.toStringAsFixed(3)})"),
108+
if (_predictedNumber != null && _predictedConfidence != null) Text("$_predictedNumber (${_predictedConfidence?.toStringAsFixed(3)})"),
113109
const Spacer(),
114110
Text("Inference Time: $_inferenceTime (ms)"),
115111
Padding(
@@ -133,8 +129,7 @@ class _MyHomePageState extends State<MyHomePage> {
133129
}
134130

135131
Widget sketchArea() {
136-
return LayoutBuilder(
137-
builder: (BuildContext context, BoxConstraints constraints) {
132+
return LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) {
138133
return GestureDetector(
139134
onPanUpdate: (DragUpdateDetails details) {
140135
final width = constraints.maxWidth;

example/gesture_classification/lib/main.dart

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ class MyApp extends StatelessWidget {
3939
colorScheme: ColorScheme.fromSeed(seedColor: Colors.orange),
4040
useMaterial3: true,
4141
),
42-
home: const MyHomePage(
43-
title:
44-
'An end-to-end example of gesture classification using Flutter and TensorFlow Lite'),
42+
home: const MyHomePage(title: 'An end-to-end example of gesture classification using Flutter and TensorFlow Lite'),
4543
);
4644
}
4745
}
@@ -64,13 +62,8 @@ class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
6462

6563
// init camera
6664
_initCamera() {
67-
_cameraDescription = _cameras.firstWhere(
68-
(element) => element.lensDirection == CameraLensDirection.front);
69-
_cameraController = CameraController(
70-
_cameraDescription, ResolutionPreset.high,
71-
imageFormatGroup: Platform.isIOS
72-
? ImageFormatGroup.bgra8888
73-
: ImageFormatGroup.yuv420);
65+
_cameraDescription = _cameras.firstWhere((element) => element.lensDirection == CameraLensDirection.front);
66+
_cameraController = CameraController(_cameraDescription, ResolutionPreset.high, imageFormatGroup: Platform.isIOS ? ImageFormatGroup.bgra8888 : ImageFormatGroup.yuv420);
7467
_cameraController!.initialize().then((value) {
7568
_cameraController!.startImageStream(_imageAnalysis);
7669
if (mounted) {
@@ -85,8 +78,7 @@ class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
8578
return;
8679
}
8780
_isProcessing = true;
88-
_classification =
89-
await _gestureClassificationHelper.inferenceCameraFrame(cameraImage);
81+
_classification = await _gestureClassificationHelper.inferenceCameraFrame(cameraImage);
9082
_isProcessing = false;
9183
if (mounted) {
9284
setState(() {});
@@ -116,8 +108,7 @@ class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
116108
_cameraController?.stopImageStream();
117109
break;
118110
case AppLifecycleState.resumed:
119-
if (_cameraController != null &&
120-
!_cameraController!.value.isStreamingImages) {
111+
if (_cameraController != null && !_cameraController!.value.isStreamingImages) {
121112
await _cameraController!.startImageStream(_imageAnalysis);
122113
}
123114
break;
@@ -166,7 +157,7 @@ class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
166157
title: Center(
167158
child: Image.asset('assets/images/tfl_logo.png'),
168159
),
169-
backgroundColor: Colors.black.withOpacity(0.5),
160+
backgroundColor: Colors.black.withValues(alpha: (0.5)),
170161
),
171162
body: Center(
172163
// Center is a layout widget. It takes a single child and positions it
@@ -193,11 +184,7 @@ class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
193184
padding: const EdgeInsets.all(8),
194185
color: Colors.white,
195186
child: Row(
196-
children: [
197-
Text(e.key),
198-
const Spacer(),
199-
Text(e.value.toStringAsFixed(2))
200-
],
187+
children: [Text(e.key), const Spacer(), Text(e.value.toStringAsFixed(2))],
201188
),
202189
),
203190
),

example/image_classification_mobilenet/lib/main.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,10 @@ class BottomNavigationBarExample extends StatefulWidget {
4141
const BottomNavigationBarExample({super.key});
4242

4343
@override
44-
State<BottomNavigationBarExample> createState() =>
45-
_BottomNavigationBarExampleState();
44+
State<BottomNavigationBarExample> createState() => _BottomNavigationBarExampleState();
4645
}
4746

48-
class _BottomNavigationBarExampleState
49-
extends State<BottomNavigationBarExample> {
47+
class _BottomNavigationBarExampleState extends State<BottomNavigationBarExample> {
5048
late CameraDescription cameraDescription;
5149
int _selectedIndex = 0;
5250
List<Widget>? _widgetOptions;
@@ -88,7 +86,7 @@ class _BottomNavigationBarExampleState
8886
return Scaffold(
8987
appBar: AppBar(
9088
title: Image.asset('assets/images/tfl_logo.png'),
91-
backgroundColor: Colors.black.withOpacity(0.5),
89+
backgroundColor: Colors.black.withValues(alpha: (0.5)),
9290
),
9391
body: Center(
9492
child: _widgetOptions?.elementAt(_selectedIndex),

0 commit comments

Comments
 (0)