OpenMediacenterMobileFlutter/lib/login/logincontext.dart

85 lines
1.9 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import '../api/token.dart';
import '../log/log.dart';
class LoginContainer extends StatefulWidget {
const LoginContainer({Key? key, required this.child}) : super(key: key);
final Widget child;
@override
_LoginContainerState createState() => _LoginContainerState();
}
class _LoginContainerState extends State<LoginContainer> {
bool _loggedIn = false;
bool _loading = true;
@override
void initState() {
super.initState();
final token = Token.getInstance();
token.getToken().then((value) {
Log.i("The token value is $value");
if (value != null) {
setState(() {
_loggedIn = true;
_loading = false;
});
} else {
setState(() {
_loggedIn = false;
_loading = false;
});
}
});
}
@override
Widget build(BuildContext context) {
return LoginContext(
loggedIn: _loggedIn,
child: _loading
? MaterialApp(
home: Container(
color: Colors.white,
child: const Center(child: CircularProgressIndicator()),
),
)
: widget.child,
onLoggin: (bool login) {
if (_loggedIn != login) {
setState(() {
_loggedIn = login;
});
}
},
);
}
}
class LoginContext extends InheritedWidget {
const LoginContext(
{Key? key,
required Widget child,
required this.loggedIn,
required this.onLoggin})
: super(key: key, child: child);
final bool loggedIn;
final void Function(bool) onLoggin;
static LoginContext of(BuildContext context) {
final LoginContext? result =
context.dependOnInheritedWidgetOfExactType<LoginContext>();
assert(result != null, 'No LoginContext found in context');
return result!;
}
@override
bool updateShouldNotify(LoginContext old) {
return loggedIn != old.loggedIn;
}
}