From 209473daa8a7888c2328b862f1c8aaeef3436145 Mon Sep 17 00:00:00 2001 From: lukas Date: Sat, 11 Dec 2021 00:01:44 +0100 Subject: [PATCH] structureing and loading indicator when starting app --- lib/app.dart | 34 +++++++++------------------- lib/{ => login}/login_screen.dart | 0 lib/{ => login}/logincontext.dart | 37 ++++++++++++++++++++++++++----- lib/main.dart | 3 ++- pubspec.lock | 2 +- 5 files changed, 45 insertions(+), 31 deletions(-) rename lib/{ => login}/login_screen.dart (100%) rename lib/{ => login}/logincontext.dart (61%) diff --git a/lib/app.dart b/lib/app.dart index 07ee3c2..3d1b142 100644 --- a/lib/app.dart +++ b/lib/app.dart @@ -1,18 +1,19 @@ import 'package:flutter/material.dart'; import 'package:logger/logger.dart'; -import 'package:openmediacentermobile/api/token.dart'; -import 'package:openmediacentermobile/login_screen.dart'; +import 'package:openmediacentermobile/login/login_screen.dart'; -import 'logincontext.dart'; +import 'login/logincontext.dart'; import 'video_feed.dart'; -class App extends StatefulWidget { - const App({Key? key}) : super(key: key); - @override - State createState() => AppState(); -} +// class App extends StatefulWidget { +// const App({Key? key}) : super(key: key); +// @override +// State createState() => AppState(); +// } + +class App extends StatelessWidget { + const App({Key? key}) : super(key: key); -class AppState extends State { @override Widget build(BuildContext context) { var loginctx = LoginContext.of(context); @@ -44,19 +45,4 @@ class AppState extends State { body: VideoFeed())); } } - - @override - void initState() { - final token = Token.getInstance(); - token.getToken().then((value) { - // todo this context call might occur before app rendered correctly! - final loginctx = LoginContext.of(context); - Logger().i("The token value is $value"); - if (value != null && value != "") { - loginctx.onLoggin(false); - } else { - loginctx.onLoggin(true); - } - }); - } } diff --git a/lib/login_screen.dart b/lib/login/login_screen.dart similarity index 100% rename from lib/login_screen.dart rename to lib/login/login_screen.dart diff --git a/lib/logincontext.dart b/lib/login/logincontext.dart similarity index 61% rename from lib/logincontext.dart rename to lib/login/logincontext.dart index be75090..e1c4f9a 100644 --- a/lib/logincontext.dart +++ b/lib/login/logincontext.dart @@ -1,4 +1,7 @@ import 'package:flutter/material.dart'; +import 'package:logger/logger.dart'; + +import '../api/token.dart'; class LoginContainer extends StatefulWidget { const LoginContainer({Key? key, required this.child}) : super(key: key); @@ -10,12 +13,40 @@ class LoginContainer extends StatefulWidget { class _LoginContainerState extends State { bool loggedIn = false; + bool loading = true; + + @override + void initState() { + // TODO: implement initState + super.initState(); + + final token = Token.getInstance(); + token.getToken().then((value) { + // todo this context call might occur before app rendered correctly! + Logger().i("The token value is $value"); + if (value != null && value != "") { + setState(() { + loggedIn = true; + loading = false; + }); + } else { + setState(() { + loggedIn = false; + loading = false; + }); + } + }); + } @override Widget build(BuildContext context) { return LoginContext( LoggedIn: loggedIn, - child: widget.child, + child: loading + ? const MaterialApp( + home: Center(child: CircularProgressIndicator()), + ) + : widget.child, onLoggin: (bool login) { if (loggedIn != login) { setState(() { @@ -38,10 +69,6 @@ class LoginContext extends InheritedWidget { final bool LoggedIn; final void Function(bool) onLoggin; - setLoggedIn(bool login) { - onLoggin(login); - } - static LoginContext of(BuildContext context) { final LoginContext? result = context.dependOnInheritedWidgetOfExactType(); diff --git a/lib/main.dart b/lib/main.dart index 4954050..9937f51 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,7 +1,8 @@ import 'package:flutter/material.dart'; import 'package:logger/logger.dart'; import 'package:openmediacentermobile/app.dart'; -import 'package:openmediacentermobile/logincontext.dart'; + +import 'login/logincontext.dart'; // main app entry point void main() { diff --git a/pubspec.lock b/pubspec.lock index d96e4d6..2e8e122 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -239,5 +239,5 @@ packages: source: hosted version: "2.1.1" sdks: - dart: ">=2.16.0-85.0.dev <3.0.0" + dart: ">=2.15.0 <3.0.0" flutter: ">=2.0.0"