MyView.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import 'package:colorize_logger/colorize_logger.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:get_it/get_it.dart';
  4. import 'package:ustest/Services/Profile.dart';
  5. import 'package:ustest/Services/UserService.dart';
  6. class MyView extends StatelessWidget {
  7. const MyView();
  8. @override
  9. Widget build(BuildContext context) {
  10. print("build Myview");
  11. GlobalKey myViewKey = GlobalKey();
  12. return Scaffold(
  13. body: Center(
  14. child: FutureBuilder<Profile?>(
  15. key: myViewKey,
  16. future: loadUserData(),
  17. builder: (context, snapshot) {
  18. if (snapshot.hasError) {
  19. return const Center(
  20. child: Text('An error has occurred!'),
  21. );
  22. } else if (snapshot.hasData) {
  23. var user = snapshot.data!;
  24. if (UserService.DefaultNotLoginId == user.id) {
  25. return Column(
  26. children: [
  27. CircleAvatar(
  28. backgroundColor: Colors.greenAccent[400],
  29. radius: 40,
  30. child: Text(
  31. 'GeeksForGeeks',
  32. style: TextStyle(fontSize: 25, color: Colors.white),
  33. ), //Text
  34. ),
  35. TextButton(
  36. child: const Text("Sign in"),
  37. onPressed: () => _showSignInScreen(context))
  38. ],
  39. );
  40. } else {
  41. return Column(
  42. children: [
  43. Row(
  44. children: [
  45. CircleAvatar(
  46. backgroundColor: Colors.greenAccent[400],
  47. radius: 40,
  48. child: Text(
  49. 'GeeksForGeeks',
  50. style: TextStyle(fontSize: 25, color: Colors.white),
  51. ), //Text
  52. ),
  53. Column(
  54. children: [Text(user.userName)],
  55. )
  56. ],
  57. ),
  58. Row(
  59. children: [
  60. Text('Name:${user.userName}, Token:${user.accessToken}'),
  61. TextButton.icon(
  62. onPressed: () => onLogout(context),
  63. icon: Icon(Icons.logout),
  64. label: Text("注銷"))
  65. ],
  66. )
  67. ],
  68. );
  69. }
  70. } else {
  71. return const Center(
  72. child: CircularProgressIndicator(),
  73. );
  74. }
  75. },
  76. )),
  77. );
  78. }
  79. Future<Profile?> loadUserData() async {
  80. try {
  81. var service = GetIt.instance.get<UserService>();
  82. var profile = await service.getCurrentUser();
  83. return profile;
  84. } catch (ex) {
  85. Logger.warning('loadUserData ex:$ex');
  86. return null;
  87. }
  88. }
  89. void _showSignInScreen(BuildContext context) {
  90. Future.delayed(Duration.zero, () {
  91. Navigator.of(context).pushNamed('/signin');
  92. });
  93. }
  94. void onLogout(context) {
  95. var service = GetIt.instance.get<UserService>();
  96. service.logout();
  97. Navigator.of(context).pushNamed('/signin');
  98. }
  99. }