2021-12-10 17:36:59 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2021-12-10 23:01:44 +00:00
|
|
|
|
|
|
|
import '../api/token.dart';
|
2021-12-11 12:33:46 +00:00
|
|
|
import '../log/log.dart';
|
2021-12-10 17:36:59 +00:00
|
|
|
|
|
|
|
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> {
|
2022-08-25 17:48:09 +00:00
|
|
|
bool _loggedIn = false;
|
|
|
|
bool _loading = true;
|
2021-12-10 23:01:44 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
|
|
|
|
final token = Token.getInstance();
|
|
|
|
token.getToken().then((value) {
|
2021-12-11 12:33:46 +00:00
|
|
|
Log.i("The token value is $value");
|
2021-12-11 16:17:38 +00:00
|
|
|
if (value != null) {
|
2021-12-10 23:01:44 +00:00
|
|
|
setState(() {
|
2022-08-25 17:48:09 +00:00
|
|
|
_loggedIn = true;
|
|
|
|
_loading = false;
|
2021-12-10 23:01:44 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
setState(() {
|
2022-08-25 17:48:09 +00:00
|
|
|
_loggedIn = false;
|
|
|
|
_loading = false;
|
2021-12-10 23:01:44 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-12-10 17:36:59 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return LoginContext(
|
2022-08-25 17:48:09 +00:00
|
|
|
loggedIn: _loggedIn,
|
|
|
|
child: _loading
|
2021-12-11 12:33:46 +00:00
|
|
|
? MaterialApp(
|
|
|
|
home: Container(
|
|
|
|
color: Colors.white,
|
|
|
|
child: const Center(child: CircularProgressIndicator()),
|
|
|
|
),
|
2021-12-10 23:01:44 +00:00
|
|
|
)
|
|
|
|
: widget.child,
|
2021-12-10 17:36:59 +00:00
|
|
|
onLoggin: (bool login) {
|
2022-08-25 17:48:09 +00:00
|
|
|
if (_loggedIn != login) {
|
2021-12-10 17:36:59 +00:00
|
|
|
setState(() {
|
2022-08-25 17:48:09 +00:00
|
|
|
_loggedIn = login;
|
2021-12-10 17:36:59 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class LoginContext extends InheritedWidget {
|
|
|
|
const LoginContext(
|
|
|
|
{Key? key,
|
|
|
|
required Widget child,
|
2022-08-25 17:48:09 +00:00
|
|
|
required this.loggedIn,
|
2021-12-10 17:36:59 +00:00
|
|
|
required this.onLoggin})
|
|
|
|
: super(key: key, child: child);
|
|
|
|
|
2022-08-25 17:48:09 +00:00
|
|
|
final bool loggedIn;
|
2021-12-10 17:36:59 +00:00
|
|
|
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) {
|
2022-08-25 17:48:09 +00:00
|
|
|
return loggedIn != old.loggedIn;
|
2021-12-10 17:36:59 +00:00
|
|
|
}
|
|
|
|
}
|