123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- import 'package:colorize_logger/colorize_logger.dart';
- import 'package:flutter/material.dart';
- import 'package:get_it/get_it.dart';
- import 'package:ustest/Services/Profile.dart';
- import 'package:ustest/Services/UserService.dart';
- class MyView extends StatelessWidget {
- const MyView();
- @override
- Widget build(BuildContext context) {
- print("build Myview");
- GlobalKey myViewKey = GlobalKey();
- return Scaffold(
- body: Center(
- child: FutureBuilder<Profile?>(
- key: myViewKey,
- future: loadUserData(),
- builder: (context, snapshot) {
- if (snapshot.hasError) {
- return const Center(
- child: Text('An error has occurred!'),
- );
- } else if (snapshot.hasData) {
- var user = snapshot.data!;
- if (UserService.DefaultNotLoginId == user.id) {
- return Column(
- children: [
- CircleAvatar(
- backgroundColor: Colors.greenAccent[400],
- radius: 40,
- child: Text(
- 'GeeksForGeeks',
- style: TextStyle(fontSize: 25, color: Colors.white),
- ), //Text
- ),
- TextButton(
- child: const Text("Sign in"),
- onPressed: () => _showSignInScreen(context))
- ],
- );
- } else {
- return Column(
- children: [
- Row(
- children: [
- CircleAvatar(
- backgroundColor: Colors.greenAccent[400],
- radius: 40,
- child: Text(
- 'GeeksForGeeks',
- style: TextStyle(fontSize: 25, color: Colors.white),
- ), //Text
- ),
- Column(
- children: [Text(user.userName)],
- )
- ],
- ),
- Row(
- children: [
- Text('Name:${user.userName}, Token:${user.accessToken}'),
- TextButton.icon(
- onPressed: () => onLogout(context),
- icon: Icon(Icons.logout),
- label: Text("注銷"))
- ],
- )
- ],
- );
- }
- } else {
- return const Center(
- child: CircularProgressIndicator(),
- );
- }
- },
- )),
- );
- }
- Future<Profile?> loadUserData() async {
- try {
- var service = GetIt.instance.get<UserService>();
- var profile = await service.getCurrentUser();
- return profile;
- } catch (ex) {
- Logger.warning('loadUserData ex:$ex');
- return null;
- }
- }
- void _showSignInScreen(BuildContext context) {
- Future.delayed(Duration.zero, () {
- Navigator.of(context).pushNamed('/signin');
- });
- }
- void onLogout(context) {
- var service = GetIt.instance.get<UserService>();
- service.logout();
- Navigator.of(context).pushNamed('/signin');
- }
- }
|