11import 'package:flutter/material.dart' ;
22import 'package:english_words/english_words.dart' ;
3+ import 'package:shared_preferences/shared_preferences.dart' ;
34import 'package:url_launcher/url_launcher.dart' ;
45
5- import 'dart:io' ;
66import 'dart:async' ;
77import 'dart:convert' as convert;
88
@@ -135,8 +135,8 @@ class MyHomePage extends StatefulWidget {
135135}
136136
137137class _MyHomePageState extends State <MyHomePage > {
138- int _counter = 0 ;
139- String _display = "hoge" ;
138+ int ? _counter = 0 ;
139+ String ? _display = "hoge" ;
140140 String ? _token;
141141
142142 // Platform呼出
@@ -147,18 +147,15 @@ class _MyHomePageState extends State<MyHomePage> {
147147
148148 String ? _codeVerifier;
149149 String ? _authorizationCode;
150- String ? _refreshToken;
151150 String ? _accessToken;
152151
153152 // final String _clientId = 'interactive.public';
154153 // final String _redirectUrl = 'io.identityserver.demo:/oauthredirect';
155- // final String _issuer = 'https://demo.identityserver.io';
156154 // final String _discoveryUrl =
157155 // 'https://demo.identityserver.io/.well-known/openid-configuration';
158156
159157 final String _clientId = '40319c0100f94ff3aab3004c8bdb5e52' ;
160158 final String _redirectUrl = 'com.opentouryo:/oauthredirect' ;
161- final String _issuer = 'https://ssoauth.opentouryo.com' ;
162159 final String _discoveryUrl =
163160 'https://mpos-opentouryo.ddo.jp/MultiPurposeAuthSite/.well-known/openid-configuration' ;
164161
@@ -167,20 +164,23 @@ class _MyHomePageState extends State<MyHomePage> {
167164 'email'
168165 ];
169166
170- // final AuthorizationServiceConfiguration _serviceConfiguration =
171- // const AuthorizationServiceConfiguration(
172- // 'https://demo.identityserver.io/connect/authorize',
173- // 'https://demo.identityserver.io/connect/token');
167+ /* final AuthorizationServiceConfiguration _serviceConfiguration =
168+ const AuthorizationServiceConfiguration(
169+ 'https://demo.identityserver.io/connect/authorize',
170+ 'https://demo.identityserver.io/connect/token');*/
174171
175- final AuthorizationServiceConfiguration _serviceConfiguration =
172+ /* final AuthorizationServiceConfiguration _serviceConfiguration =
176173 const AuthorizationServiceConfiguration(
177174 'https://mpos-opentouryo.ddo.jp/MultiPurposeAuthSite/authorize',
178- 'https://mpos-opentouryo.ddo.jp/MultiPurposeAuthSite/token' );
175+ 'https://mpos-opentouryo.ddo.jp/MultiPurposeAuthSite/token');*/
179176
180177 @override
181178 void initState () {
182179 super .initState ();
183180
181+ // this._counterの初期化
182+ this ._getCounterValue ();
183+
184184 // ターミネーテッド状態でプッシュ通知からアプリを起動した時のアクションを実装
185185 FirebaseMessaging .instance
186186 .getInitialMessage ()
@@ -200,7 +200,7 @@ class _MyHomePageState extends State<MyHomePage> {
200200 print ("ローカル通知で擬似的に通知メッセージを表示" );
201201 RemoteNotification ? notification = message? .notification;
202202 AndroidNotification ? android = message? .notification? .android;
203- if (channel != null && flutterLocalNotificationsPlugin != null
203+ if (flutterLocalNotificationsPlugin != null
204204 && notification != null && android != null && ! kIsWeb) {
205205
206206 flutterLocalNotificationsPlugin? .show (
@@ -229,23 +229,72 @@ class _MyHomePageState extends State<MyHomePage> {
229229 });
230230 }
231231
232+ // this._counterのインクリメント
232233 void _incrementCounter () {
233234 setState (() {
234235 // This call to setState tells the Flutter framework that something has
235236 // changed in this State, which causes it to rerun the build method below
236237 // so that the display can reflect the updated values. If we changed
237238 // _counter without calling setState(), then the build method would not be
238239 // called again, and so nothing would appear to happen.
239- this ._display = (this ._counter++ ).toString ();
240+ this ._counter = this ._counter! + 1 ;
241+ this ._display = this ._counter? .toString ();
242+ });
243+ this ._setCounterValue ();
244+ }
245+
246+ // this._counterのリセット
247+ void _resetCounter () {
248+ this ._removeCounterValue ();
249+ setState (() {
250+ // This call to setState tells the Flutter framework that something has
251+ // changed in this State, which causes it to rerun the build method below
252+ // so that the display can reflect the updated values. If we changed
253+ // _counter without calling setState(), then the build method would not be
254+ // called again, and so nothing would appear to happen.
255+ this ._counter = 0 ;
256+ this ._display = this ._counter? .toString ();
240257 });
241258 }
242259
260+ /// english_words
243261 void _englishWords () {
244262 setState (() {
245263 this ._display = WordPair .random ().asPascalCase;
246264 });
247265 }
248266
267+ /// shared_preferences
268+ /// this._counterの永続化対応
269+ void _getCounterValue () async {
270+ SharedPreferences prefs = await SharedPreferences .getInstance ();
271+ setState (() {
272+ int ? temp = prefs.getInt ('counter' );
273+ if (temp == null ){
274+ this ._counter = 0 ;
275+ }
276+ else {
277+ this ._counter = temp;
278+ }
279+ });
280+ }
281+ /// shared_preferences
282+ /// this._counterの永続化対応
283+ void _setCounterValue () async {
284+ SharedPreferences prefs = await SharedPreferences .getInstance ();
285+ prefs.setInt ('counter' , this ._counter! );
286+ }
287+ /// shared_preferences
288+ /// this._counterの永続化対応
289+ void _removeCounterValue () async {
290+ SharedPreferences prefs = await SharedPreferences .getInstance ();
291+ setState (() {
292+ this ._counter = 0 ;
293+ prefs.remove ('counter' );
294+ });
295+ }
296+
297+ /// url_launcher
249298 void _urlLauncher () async {
250299 const url = "https://www.osscons.jp/jo5v2ne7n-537/" ;
251300 if (await canLaunch (url)) {
@@ -255,6 +304,7 @@ class _MyHomePageState extends State<MyHomePage> {
255304 }
256305 }
257306
307+ /// MethodChannel
258308 Future <void > _getBatteryLevel () async {
259309 String batteryLevel;
260310 try {
@@ -268,6 +318,7 @@ class _MyHomePageState extends State<MyHomePage> {
268318 });
269319 }
270320
321+ /// WebAPI
271322 Future <void > _getBooks () async {
272323 var url =
273324 Uri .https ('www.googleapis.com' , '/books/v1/volumes' , {'q' : '{http}' });
@@ -286,6 +337,7 @@ class _MyHomePageState extends State<MyHomePage> {
286337 }
287338 }
288339
340+ /// AppAuth1
289341 Future <void > _signInWithNoCodeExchange () async {
290342 try {
291343 final AuthorizationResponse ? result
@@ -308,6 +360,7 @@ class _MyHomePageState extends State<MyHomePage> {
308360 }
309361 }
310362
363+ /// AppAuth2
311364 Future <void > _exchangeCode () async {
312365 try {
313366 final TokenResponse ? result = await this ._appAuth.token (TokenRequest (
@@ -330,6 +383,7 @@ class _MyHomePageState extends State<MyHomePage> {
330383 }
331384 }
332385
386+ /// AppAuth3
333387 Future <void > _testApi () async {
334388 final http.Response httpResponse = await http.get (
335389 Uri .parse ('http://mpos-opentouryo.ddo.jp/MultiPurposeAuthSite/userinfo' ),
@@ -340,26 +394,6 @@ class _MyHomePageState extends State<MyHomePage> {
340394 });
341395 }
342396
343- Future <void > _sendPushMessage () async {
344- if (_token == null ) {
345- print ('Unable to send FCM message, no token exists.' );
346- return ;
347- }
348-
349- try {
350- await http.post (
351- Uri .parse ('https://api.rnfirebase.io/messaging/send' ),
352- headers: < String , String > {
353- 'Content-Type' : 'application/json; charset=UTF-8' ,
354- },
355- body: constructFCMPayload (_token),
356- );
357- print ('FCM request for device sent!' );
358- } catch (e) {
359- print (e);
360- }
361- }
362-
363397 Future <void > _onActionSelected (String value) async {
364398 switch (value) {
365399 case 'subscribe' :
@@ -408,7 +442,7 @@ class _MyHomePageState extends State<MyHomePage> {
408442 title: Text (widget.title),
409443 actions: < Widget > [
410444 PopupMenuButton (
411- onSelected: _onActionSelected,
445+ onSelected: this . _onActionSelected,
412446 itemBuilder: (BuildContext context) {
413447 return [
414448 const PopupMenuItem (
@@ -439,7 +473,7 @@ class _MyHomePageState extends State<MyHomePage> {
439473 'You have pushed the button this many times:' ,
440474 ),
441475 Text (
442- '$ _display ' ,
476+ this . _display ?? "" ,
443477 style: Theme .of (context).textTheme.headline4,
444478 ),
445479 ],
@@ -448,69 +482,70 @@ class _MyHomePageState extends State<MyHomePage> {
448482 mainAxisAlignment: MainAxisAlignment .spaceAround,
449483 children: < Widget > [
450484 ElevatedButton (
451- child: const Text ('EnglishWords Button' ),
485+ child: const Text ('Reset Button' ),
452486 style: ElevatedButton .styleFrom (
453487 primary: Colors .orange,
454488 onPrimary: Colors .white,
455489 ),
456- onPressed: this ._englishWords ,
490+ onPressed: this ._resetCounter ,
457491 ),
458492 ElevatedButton (
459- child: const Text ('UrlLauncher Button' ),
493+ child: const Text ('EnglishWords Button' ),
460494 style: ElevatedButton .styleFrom (
461495 primary: Colors .orange,
462496 onPrimary: Colors .white,
463497 ),
464- onPressed: this ._urlLauncher ,
498+ onPressed: this ._englishWords ,
465499 ),
466500 ]
467501 ),
468502 Row (
469503 mainAxisAlignment: MainAxisAlignment .spaceAround,
470504 children: < Widget > [
471505 ElevatedButton (
472- child: const Text ('BatteryLevel Button' ),
506+ child: const Text ('UrlLauncher Button' ),
473507 style: ElevatedButton .styleFrom (
474508 primary: Colors .orange,
475509 onPrimary: Colors .white,
476510 ),
477- onPressed: this ._getBatteryLevel ,
511+ onPressed: this ._urlLauncher ,
478512 ),
479513 ElevatedButton (
480- child: const Text ('GetBooks Button' ),
514+ child: const Text ('BatteryLevel Button' ),
481515 style: ElevatedButton .styleFrom (
482516 primary: Colors .orange,
483517 onPrimary: Colors .white,
484518 ),
485- onPressed: this ._getBooks ,
519+ onPressed: this ._getBatteryLevel ,
486520 ),
521+
487522 ]
488523 ),
489524 Row (
490525 mainAxisAlignment: MainAxisAlignment .spaceAround,
491526 children: < Widget > [
492527 ElevatedButton (
493- child: const Text ('SignIn Button' ),
528+ child: const Text ('GetBooks Button' ),
494529 style: ElevatedButton .styleFrom (
495530 primary: Colors .orange,
496531 onPrimary: Colors .white,
497532 ),
498- onPressed: this ._signInWithNoCodeExchange ,
533+ onPressed: this ._getBooks ,
499534 ),
500535 ElevatedButton (
501- child: const Text ('SendPushMessage Button' ),
536+ child: const Text ('SignIn Button' ),
502537 style: ElevatedButton .styleFrom (
503538 primary: Colors .orange,
504539 onPrimary: Colors .white,
505540 ),
506- onPressed: this ._sendPushMessage ,
541+ onPressed: this ._signInWithNoCodeExchange ,
507542 ),
508543 ]
509544 ),
510545 Column (children: [
511546 MetaCard ('Permissions' , Permissions ()),
512547 MetaCard ('FCM Token' , TokenMonitor ((token) {
513- _token = token;
548+ this . _token = token;
514549 return token == null
515550 ? const CircularProgressIndicator ()
516551 : Text (token, style: const TextStyle (fontSize: 12 ));
@@ -521,7 +556,7 @@ class _MyHomePageState extends State<MyHomePage> {
521556 ),
522557 ),
523558 floatingActionButton: FloatingActionButton (
524- onPressed: _incrementCounter,
559+ onPressed: this . _incrementCounter,
525560 tooltip: 'Increment' ,
526561 child: Icon (Icons .add),
527562 ),
@@ -568,23 +603,4 @@ class _MyHomePageState extends State<MyHomePage> {
568603 ),
569604 );
570605 }
571- }
572-
573- // Crude counter to make messages unique
574- int _messageCount = 0 ;
575-
576- /// The API endpoint here accepts a raw FCM payload for demonstration purposes.
577- String constructFCMPayload (String ? token) {
578- _messageCount++ ;
579- return convert.jsonEncode ({
580- 'token' : token,
581- 'data' : {
582- 'via' : 'FlutterFire Cloud Messaging!!!' ,
583- 'count' : _messageCount.toString (),
584- },
585- 'notification' : {
586- 'title' : 'Hello FlutterFire!' ,
587- 'body' : 'This notification (#$_messageCount ) was created via FCM!' ,
588- },
589- });
590- }
606+ }
0 commit comments