structureing and loading indicator when starting app

This commit is contained in:
lukas 2021-12-11 00:01:44 +01:00
parent 201585eb30
commit 209473daa8
5 changed files with 45 additions and 31 deletions

View File

@ -1,18 +1,19 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:logger/logger.dart'; import 'package:logger/logger.dart';
import 'package:openmediacentermobile/api/token.dart'; import 'package:openmediacentermobile/login/login_screen.dart';
import 'package:openmediacentermobile/login_screen.dart';
import 'logincontext.dart'; import 'login/logincontext.dart';
import 'video_feed.dart'; import 'video_feed.dart';
class App extends StatefulWidget { // class App extends StatefulWidget {
const App({Key? key}) : super(key: key); // const App({Key? key}) : super(key: key);
@override // @override
State<StatefulWidget> createState() => AppState(); // State<StatefulWidget> createState() => AppState();
} // }
class App extends StatelessWidget {
const App({Key? key}) : super(key: key);
class AppState extends State<App> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
var loginctx = LoginContext.of(context); var loginctx = LoginContext.of(context);
@ -44,19 +45,4 @@ class AppState extends State<App> {
body: VideoFeed())); 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);
}
});
}
} }

View File

@ -1,4 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:logger/logger.dart';
import '../api/token.dart';
class LoginContainer extends StatefulWidget { class LoginContainer extends StatefulWidget {
const LoginContainer({Key? key, required this.child}) : super(key: key); const LoginContainer({Key? key, required this.child}) : super(key: key);
@ -10,12 +13,40 @@ class LoginContainer extends StatefulWidget {
class _LoginContainerState extends State<LoginContainer> { class _LoginContainerState extends State<LoginContainer> {
bool loggedIn = false; 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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return LoginContext( return LoginContext(
LoggedIn: loggedIn, LoggedIn: loggedIn,
child: widget.child, child: loading
? const MaterialApp(
home: Center(child: CircularProgressIndicator()),
)
: widget.child,
onLoggin: (bool login) { onLoggin: (bool login) {
if (loggedIn != login) { if (loggedIn != login) {
setState(() { setState(() {
@ -38,10 +69,6 @@ class LoginContext extends InheritedWidget {
final bool LoggedIn; final bool LoggedIn;
final void Function(bool) onLoggin; final void Function(bool) onLoggin;
setLoggedIn(bool login) {
onLoggin(login);
}
static LoginContext of(BuildContext context) { static LoginContext of(BuildContext context) {
final LoginContext? result = final LoginContext? result =
context.dependOnInheritedWidgetOfExactType<LoginContext>(); context.dependOnInheritedWidgetOfExactType<LoginContext>();

View File

@ -1,7 +1,8 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:logger/logger.dart'; import 'package:logger/logger.dart';
import 'package:openmediacentermobile/app.dart'; import 'package:openmediacentermobile/app.dart';
import 'package:openmediacentermobile/logincontext.dart';
import 'login/logincontext.dart';
// main app entry point // main app entry point
void main() { void main() {

View File

@ -239,5 +239,5 @@ packages:
source: hosted source: hosted
version: "2.1.1" version: "2.1.1"
sdks: sdks:
dart: ">=2.16.0-85.0.dev <3.0.0" dart: ">=2.15.0 <3.0.0"
flutter: ">=2.0.0" flutter: ">=2.0.0"