Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.54 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:usahaku_flutter/pages/HomePage.dart';
  3. import 'package:usahaku_flutter/pages/ChatPage.dart';
  4. import 'package:usahaku_flutter/pages/TransactionPage.dart';
  5. import 'package:usahaku_flutter/pages/ProfilePage.dart';
  6. import 'package:usahaku_flutter/pages/ShopPage.dart';
  7. import './services/locator_service.dart';
  8. import './blocs/NavTab.dart';
  9.  
  10. void main() {
  11. setup();
  12. runApp(MyApp());
  13. }
  14.  
  15. class MyApp extends StatefulWidget {
  16. @override
  17. State<StatefulWidget> createState() {
  18. return MyAppState();
  19. }
  20. }
  21.  
  22. class MyAppState extends State<MyApp> {
  23. final _pageOption = [
  24. HomePage(),
  25. ChatPage(),
  26. UserProfile(),
  27. TransactionPage(),
  28. ShopPage(),
  29. ];
  30. int _currentTabIndex = 0;
  31. var _currentPage;
  32. @override
  33. initState() {
  34. super.initState();
  35. _currentPage = _pageOption[_currentTabIndex];
  36. sl.get<NavTab>().subject_currentTab.listen((data) {
  37. _currentTabIndex = data;
  38. _currentPage = _pageOption[_currentTabIndex];
  39. print(_currentPage);
  40. });
  41. }
  42.  
  43. @override
  44. Widget build(BuildContext context) {
  45. return MaterialApp(
  46. title: 'UsahaKu',
  47. theme: ThemeData(primarySwatch: Colors.brown),
  48. home: SafeArea(
  49. child: Scaffold(
  50. body: _currentPage,
  51. bottomNavigationBar: BottomNavigationBar(
  52. fixedColor: Colors.black87,
  53. currentIndex: _currentTabIndex,
  54. type: BottomNavigationBarType.fixed,
  55. items: [
  56. BottomNavigationBarItem(
  57. icon: Icon(Icons.home),
  58. title: Text('Home'),
  59. ),
  60. BottomNavigationBarItem(
  61. icon: Icon(Icons.chat),
  62. title: Text('Chat'),
  63. ),
  64. BottomNavigationBarItem(
  65. icon: Icon(Icons.person),
  66. title: Text('Profile'),
  67. ),
  68. BottomNavigationBarItem(
  69. icon: Icon(Icons.transform),
  70. title: Text('Transaksi'),
  71. ),
  72. BottomNavigationBarItem(
  73. icon: Icon(Icons.business_center),
  74. title: Text('Toko'),
  75. ),
  76. ],
  77. onTap: (index) {
  78. setState(() {
  79. sl.get<NavTab>().changeTab(index);
  80. });
  81. }),
  82. ),
  83. ));
  84. }
  85. }
  86.  
  87. import 'package:rxdart/rxdart.dart';
  88.  
  89. class NavTab {
  90. BehaviorSubject<int> subject_currentTab = new BehaviorSubject<int>();
  91. BehaviorSubject<int> subject_previousTab = new BehaviorSubject<int>();
  92.  
  93. // Observable<int> currentTab;
  94. // Observable<int> previousTab;
  95.  
  96. NavTab() {
  97. // currentTab = new Observable(subject_currentTab);
  98. // previousTab = new Observable(subject_previousTab);
  99. subject_currentTab.add(0);
  100. subject_previousTab.add(null);
  101. }
  102.  
  103. void changeTab(int c) {
  104. var cr = subject_currentTab.value;
  105. var pr = subject_previousTab.value;
  106. if (cr != pr) {
  107. subject_previousTab.add(cr);
  108. subject_currentTab.add(c);
  109. }else{
  110. subject_currentTab.add(c);
  111. }
  112. }
  113.  
  114. void backToPreviousTab() {
  115. var r = subject_previousTab.value;
  116. subject_currentTab.add(r);
  117. }
  118. }
  119.  
  120. import 'dart:async';
  121. import 'package:flutter/material.dart';
  122. import 'package:usahaku_flutter/services/Auth.dart';
  123. import 'package:usahaku_flutter/services/locator_service.dart';
  124. import './LoginPage.dart';
  125. import '../blocs/NavTab.dart';
  126.  
  127. class UserProfile extends StatefulWidget {
  128. @override
  129. UserProfileState createState() => UserProfileState();
  130. }
  131.  
  132. class UserProfileState extends State<UserProfile> {
  133. Map<String, dynamic> _profile;
  134. StreamSubscription _profileSubs = null;
  135.  
  136. @override
  137. initState() {
  138. super.initState();
  139. print('constructor');
  140. print('profile page');
  141. _profileSubs = sl.get<AuthService>().profile.listen((state) => setState(() {
  142. print('constructor');
  143. print('state');
  144. print(state);
  145. if (state == null) {
  146. print('navigate to loginpage');
  147. Navigator.push(context,
  148. MaterialPageRoute(builder: (context) => LoginPage())).then((t) {
  149. if (t == false) {
  150. sl.get<NavTab>().backToPreviousTab();
  151. }
  152. });
  153. } else {
  154. _profile = state;
  155. }
  156. }));
  157. }
  158.  
  159. @override
  160. void dispose() {
  161. _profileSubs.cancel();
  162. super.dispose();
  163. }
  164.  
  165. @override
  166. Widget build(BuildContext context) {
  167. return Scaffold(
  168. appBar: AppBar(
  169. title: Text('Profile'),
  170. ),
  171. body: Column(
  172. children: <Widget>[
  173. Text(_profile.toString()),
  174. MaterialButton(
  175. onPressed: () => sl.get<AuthService>().signOut(),
  176. child: Text('Logout'),
  177. )
  178. ],
  179. ),
  180. );
  181. }
  182. }
  183.  
  184. import 'dart:async';
  185. import 'package:flutter/material.dart';
  186. import 'package:rxdart/rxdart.dart';
  187. import 'package:usahaku_flutter/services/Auth.dart';
  188. import '../services/locator_service.dart';
  189.  
  190. class LoginPage extends StatefulWidget {
  191. @override
  192. LoginPageState createState() => LoginPageState();
  193. }
  194.  
  195. class LoginPageState extends State<LoginPage> {
  196. Observable _profileSubs = null;
  197.  
  198. @override
  199. initState() {
  200. super.initState();
  201. _profileSubs = sl.get<AuthService>().user.switchMap((u) {
  202. if (u != null) {
  203. Navigator.pop(context, true);
  204. }
  205. });
  206. }
  207.  
  208. @override
  209. void dispose() {
  210. super.dispose();
  211. }
  212.  
  213. @override
  214. Widget build(BuildContext context) {
  215. return Scaffold(
  216. appBar: AppBar(
  217. leading: IconButton(
  218. icon: Icon(Icons.chevron_left),
  219. onPressed: () => Navigator.pop(context, false),
  220. ),
  221. title: Text('Login'),
  222. ),
  223. body: Column(
  224. mainAxisAlignment: MainAxisAlignment.center,
  225. mainAxisSize: MainAxisSize.max,
  226. crossAxisAlignment: CrossAxisAlignment.center,
  227. children: <Widget>[
  228. MaterialButton(
  229. onPressed: () {
  230. sl.get<AuthService>().googleSignIn().then((value) {
  231. if (value != null) {
  232. Navigator.pop(context, true);
  233. }
  234. });
  235. },
  236. child: Row(
  237. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  238. crossAxisAlignment: CrossAxisAlignment.center,
  239. mainAxisSize: MainAxisSize.min,
  240. children: <Widget>[Icon(Icons.get_app), Text('Login')],
  241. ),
  242. )
  243. ],
  244. ));
  245. }
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement