@@ -8,6 +8,7 @@ import 'package:firebase_analytics/firebase_analytics.dart';
88import 'package:flutter/foundation.dart' ;
99import 'package:flutter/material.dart' ;
1010import 'package:twilio_voice/twilio_voice.dart' ;
11+ import 'package:twilio_voice_example/dialogs/update_token_dialog.dart' ;
1112import 'package:twilio_voice_example/screens/ui_call_screen.dart' ;
1213import 'package:twilio_voice_example/screens/ui_registration_screen.dart' ;
1314
@@ -103,25 +104,25 @@ class _AppState extends State<App> {
103104
104105 // Use for locally provided token generator e.g. Twilio's quickstarter project: https://github.com/twilio/voice-quickstart-server-node
105106 // if (!kIsWeb) {
106- bool success = false ;
107- // if not web, we use the requested registration method
108- switch (widget.registrationMethod) {
109- case RegistrationMethod .env:
110- success = await _registerFromEnvironment ();
111- break ;
112- case RegistrationMethod .url:
113- success = await _registerUrl ();
114- break ;
115- case RegistrationMethod .firebase:
116- success = await _registerFirebase ();
117- break ;
118- }
107+ bool success = false ;
108+ // if not web, we use the requested registration method
109+ switch (widget.registrationMethod) {
110+ case RegistrationMethod .env:
111+ success = await _registerFromEnvironment ();
112+ break ;
113+ case RegistrationMethod .url:
114+ success = await _registerUrl ();
115+ break ;
116+ case RegistrationMethod .firebase:
117+ success = await _registerFirebase ();
118+ break ;
119+ }
119120
120- if (success) {
121- setState (() {
122- twilioInit = true ;
123- });
124- }
121+ if (success) {
122+ setState (() {
123+ twilioInit = true ;
124+ });
125+ }
125126 // } else {
126127 // // for web, we always show the initialisation screen unless we specified an
127128 // if (widget.registrationMethod == RegistrationMethod.env) {
@@ -286,7 +287,7 @@ class _AppState extends State<App> {
286287
287288 switch (event) {
288289 case CallEvent .incoming:
289- // applies to web only
290+ // applies to web only
290291 if (kIsWeb || Platform .isAndroid) {
291292 final activeCall = TwilioVoice .instance.call.activeCall;
292293 if (activeCall != null && activeCall.callDirection == CallDirection .incoming) {
@@ -338,7 +339,8 @@ class _AppState extends State<App> {
338339 showDialog (
339340 // ignore: use_build_context_synchronously
340341 context: context,
341- builder: (context) => const AlertDialog (
342+ builder: (context) =>
343+ const AlertDialog (
342344 title: Text ("Error" ),
343345 content: Text ("Failed to register for calls" ),
344346 ),
@@ -357,6 +359,10 @@ class _AppState extends State<App> {
357359 appBar: AppBar (
358360 title: const Text ("Plugin example app" ),
359361 actions: [
362+ if (twilioInit) ...[
363+ const SizedBox (width: 8 ),
364+ const _UpdateTokenAction (),
365+ ],
360366 _LogoutAction (
361367 onSuccess: () {
362368 setState (() {
@@ -381,12 +387,12 @@ class _AppState extends State<App> {
381387 padding: const EdgeInsets .symmetric (horizontal: 8 ),
382388 child: twilioInit
383389 ? UICallScreen (
384- userId: userId,
385- onPerformCall: _onPerformCall,
386- )
390+ userId: userId,
391+ onPerformCall: _onPerformCall,
392+ )
387393 : UIRegistrationScreen (
388- onRegister: _onRegisterWithToken,
389- ),
394+ onRegister: _onRegisterWithToken,
395+ ),
390396 ),
391397 ),
392398 ),
@@ -463,3 +469,30 @@ class _LogoutAction extends StatelessWidget {
463469 icon: const Icon (Icons .logout, color: Colors .white));
464470 }
465471}
472+
473+ class _UpdateTokenAction extends StatelessWidget {
474+ const _UpdateTokenAction ({super .key});
475+
476+ @override
477+ Widget build (BuildContext context) {
478+ return TextButton .icon (
479+ onPressed: () async {
480+ // show dialog to enter new token
481+ final token = await showDialog <String ?>(
482+ context: context,
483+ builder: (context) => const UpdateTokenDialogContent (),
484+ );
485+ if (token? .isEmpty ?? true ) {
486+ return ;
487+ }
488+ final result = await TwilioVoice .instance.setTokens (accessToken: token! );
489+ final message = (result ?? false ) ? "Successfully updated token" : "Failed to update token" ;
490+ ScaffoldMessenger .of (context).showSnackBar (
491+ SnackBar (content: Text (message)),
492+ );
493+ },
494+ label: const Text ("Update Token" , style: TextStyle (color: Colors .white)),
495+ icon: const Icon (Icons .refresh, color: Colors .white),
496+ );
497+ }
498+ }
0 commit comments