@@ -2,6 +2,7 @@ import 'dart:async';
2
2
import 'dart:typed_data' ;
3
3
4
4
import 'package:flutter/material.dart' ;
5
+ import 'package:flutter_foreground_task/flutter_foreground_task.dart' ;
5
6
import 'package:google_speech/endless_streaming_service_v2.dart' ;
6
7
import 'package:google_speech/generated/google/cloud/speech/v2/cloud_speech.pb.dart' ;
7
8
import 'package:google_speech/google_speech.dart' ;
@@ -14,8 +15,14 @@ import 'package:simple_frame_app/tx/code.dart';
14
15
import 'package:simple_frame_app/tx/plain_text.dart' ;
15
16
import 'package:simple_frame_app/text_utils.dart' ;
16
17
import 'package:simple_frame_app/simple_frame_app.dart' ;
18
+ import 'foreground_service.dart' ;
17
19
18
- void main () => runApp (const MainApp ());
20
+ void main () {
21
+ // Set up Android foreground service
22
+ initializeForegroundService ();
23
+
24
+ runApp (const MainApp ());
25
+ }
19
26
20
27
final _log = Logger ("MainApp" );
21
28
@@ -329,79 +336,82 @@ class MainAppState extends State<MainApp> with SimpleFrameAppState {
329
336
330
337
@override
331
338
Widget build (BuildContext context) {
332
- return MaterialApp (
333
- title: 'Transcribe - Google Cloud Speech' ,
334
- theme: ThemeData .dark (),
335
- home: Scaffold (
336
- appBar: AppBar (
337
- title: const Text ('Transcribe - Google Cloud Speech' ),
338
- actions: [getBatteryWidget ()]
339
- ),
340
- body: Center (
341
- child: Container (
342
- margin: const EdgeInsets .symmetric (horizontal: 16 ),
343
- child: Column (
344
- mainAxisAlignment: MainAxisAlignment .center,
345
- children: [
346
- TextField (controller: _serviceAccountJsonController, obscureText: false , decoration: const InputDecoration (hintText: 'Enter Service Account JSON' ),),
347
- TextField (controller: _projectIdController, obscureText: false , decoration: const InputDecoration (hintText: 'Enter Project Id' ),),
348
- TextField (controller: _languageCodeController, obscureText: false , decoration: const InputDecoration (hintText: 'Enter Language Code e.g. en-US' ),),
349
- if (_errorMsg != null ) Text (_errorMsg! , style: const TextStyle (backgroundColor: Colors .red)),
350
- ElevatedButton (onPressed: _savePrefs, child: const Text ('Save' )),
351
-
352
- Expanded (child: Column (
353
- mainAxisAlignment: MainAxisAlignment .center,
354
- children: [
355
- Expanded (
356
- child: ListView .builder (
357
- controller: _transcriptController, // Auto-scroll controller
358
- itemCount: _transcript.length,
359
- itemBuilder: (context, index) {
360
- return Text (
361
- _transcript[index],
362
- style: _textStyle,
363
- );
364
- },
365
- ),
366
- ),
367
- const Divider (),
368
- ConstrainedBox (
369
- constraints: BoxConstraints (
370
- maxHeight: _textStyle.fontSize! * 5
339
+ startForegroundService ();
340
+ return WithForegroundTask (
341
+ child: MaterialApp (
342
+ title: 'Transcribe - Google Cloud Speech' ,
343
+ theme: ThemeData .dark (),
344
+ home: Scaffold (
345
+ appBar: AppBar (
346
+ title: const Text ('Transcribe - Google Cloud Speech' ),
347
+ actions: [getBatteryWidget ()]
348
+ ),
349
+ body: Center (
350
+ child: Container (
351
+ margin: const EdgeInsets .symmetric (horizontal: 16 ),
352
+ child: Column (
353
+ mainAxisAlignment: MainAxisAlignment .center,
354
+ children: [
355
+ TextField (controller: _serviceAccountJsonController, obscureText: false , decoration: const InputDecoration (hintText: 'Enter Service Account JSON' ),),
356
+ TextField (controller: _projectIdController, obscureText: false , decoration: const InputDecoration (hintText: 'Enter Project Id' ),),
357
+ TextField (controller: _languageCodeController, obscureText: false , decoration: const InputDecoration (hintText: 'Enter Language Code e.g. en-US' ),),
358
+ if (_errorMsg != null ) Text (_errorMsg! , style: const TextStyle (backgroundColor: Colors .red)),
359
+ ElevatedButton (onPressed: _savePrefs, child: const Text ('Save' )),
360
+
361
+ Expanded (child: Column (
362
+ mainAxisAlignment: MainAxisAlignment .center,
363
+ children: [
364
+ Expanded (
365
+ child: ListView .builder (
366
+ controller: _transcriptController, // Auto-scroll controller
367
+ itemCount: _transcript.length,
368
+ itemBuilder: (context, index) {
369
+ return Text (
370
+ _transcript[index],
371
+ style: _textStyle,
372
+ );
373
+ },
374
+ ),
371
375
),
372
- child: Align (alignment: Alignment .centerLeft,
373
- child: SingleChildScrollView (
374
- controller: _partialResultController,
375
- child: Text (_partialResult, style: _textStyle)
376
- )
376
+ const Divider (),
377
+ ConstrainedBox (
378
+ constraints: BoxConstraints (
379
+ maxHeight: _textStyle.fontSize! * 5
380
+ ),
381
+ child: Align (alignment: Alignment .centerLeft,
382
+ child: SingleChildScrollView (
383
+ controller: _partialResultController,
384
+ child: Text (_partialResult, style: _textStyle)
385
+ )
386
+ ),
377
387
),
378
- ) ,
379
- ] ,
380
- )) ,
381
- ] ,
388
+ ] ,
389
+ )) ,
390
+ ] ,
391
+ ) ,
382
392
),
383
393
),
394
+ floatingActionButton: Stack (
395
+ children: [
396
+ if (_transcript.isNotEmpty) Positioned (
397
+ bottom: 90 ,
398
+ right: 20 ,
399
+ child: FloatingActionButton (
400
+ onPressed: () {
401
+ Share .share (_transcript.join ('\n ' ));
402
+ },
403
+ child: const Icon (Icons .share)),
404
+ ),
405
+ Positioned (
406
+ bottom: 20 ,
407
+ right: 20 ,
408
+ child: getFloatingActionButtonWidget (const Icon (Icons .mic), const Icon (Icons .mic_off)) ?? Container (),
409
+ ),
410
+ ]
411
+ ),
412
+ persistentFooterButtons: getFooterButtonsWidget (),
384
413
),
385
- floatingActionButton: Stack (
386
- children: [
387
- if (_transcript.isNotEmpty) Positioned (
388
- bottom: 90 ,
389
- right: 20 ,
390
- child: FloatingActionButton (
391
- onPressed: () {
392
- Share .share (_transcript.join ('\n ' ));
393
- },
394
- child: const Icon (Icons .share)),
395
- ),
396
- Positioned (
397
- bottom: 20 ,
398
- right: 20 ,
399
- child: getFloatingActionButtonWidget (const Icon (Icons .mic), const Icon (Icons .mic_off)) ?? Container (),
400
- ),
401
- ]
402
- ),
403
- persistentFooterButtons: getFooterButtonsWidget (),
404
- ),
414
+ )
405
415
);
406
416
}
407
417
}
0 commit comments