OpenMediacenterMobileFlutter/lib/login/login_container.dart

63 lines
1.3 KiB
Dart

import 'package:flutter/material.dart';
import '../api/token.dart';
import '../log/log.dart';
import 'login_context.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();
_init();
}
void _init() async {
final token = await getToken();
Log.i("The token value is $token");
if (token != 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;
});
}
},
);
}
}