Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2024
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.50 KB | Source Code | 0 0
  1.  
  2. class _MyAppState extends State<MyApp> {
  3. @override
  4. Widget build(BuildContext context) {
  5. return MaterialApp(
  6. debugShowCheckedModeBanner: false,
  7. title: 'Habito',
  8. theme: AppTheme.lightThemeMode,
  9. home: const HomePage(),
  10. // TODO: change this once auth and user session are created
  11. );
  12. }
  13. }
  14.  
  15. //Homepage (routing)
  16.  
  17.  
  18. class HomePage extends StatefulWidget {
  19. static route() => MaterialPageRoute(builder: (context) => HomePage());
  20. const HomePage({super.key});
  21.  
  22. @override
  23. State<HomePage> createState() => _HomePageState();
  24. }
  25.  
  26. class _HomePageState extends State<HomePage> {
  27. int _selectedIndex = 0;
  28.  
  29. final List<Widget> _pages = [
  30. UserDashboardPage(),
  31. HabitSelectionPage(),
  32. ViewHabitsPage(),
  33. ViewHabitsPage(),
  34. ];
  35.  
  36. void _onItemTapped(int index) {
  37. setState(() {
  38. _selectedIndex = index;
  39. });
  40. }
  41.  
  42. @override
  43. Widget build(BuildContext context) {
  44. return Scaffold(
  45. body: IndexedStack(
  46. index: _selectedIndex,
  47. children: _pages,
  48. ),
  49. floatingActionButton: FloatingActionButton(
  50. onPressed: () {
  51. showModalBottomSheet(
  52. context: context,
  53. isScrollControlled: true,
  54. backgroundColor: Colors.transparent,
  55. builder: (BuildContext context) {
  56. return AddHabitBottomSheet();
  57. });
  58. },
  59. backgroundColor: AppPallete.backgroundColor,
  60. child: Icon(Icons.add, size: 30),
  61. ),
  62. floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
  63. bottomNavigationBar: BottomAppBar(
  64. // color: Colors.white,
  65. shape: CircularNotchedRectangle(), // Makes room for FAB
  66. notchMargin: 10, // Space between FAB and bar
  67. child: Container(
  68. height: 70, // Adjust height to make space for the rounded shape
  69. decoration: BoxDecoration(
  70. color: Colors.white,
  71. borderRadius: BorderRadius.vertical(
  72. top: Radius.circular(10), //
  73. bottom: Radius.circular(10), //Rounded corners on top
  74. ),
  75. boxShadow: [
  76. BoxShadow(
  77. color: Colors.black12,
  78. blurRadius: 10,
  79. ),
  80. ],
  81. ),
  82. child: Row(
  83. mainAxisAlignment: MainAxisAlignment.spaceAround,
  84. children: <Widget>[
  85. IconButton(
  86. icon: Icon(Icons.home),
  87. color: _selectedIndex == 0 ? Colors.blue : Colors.grey,
  88. onPressed: () => _onItemTapped(0),
  89. ),
  90. IconButton(
  91. icon: Icon(Icons.list),
  92. color: _selectedIndex == 1 ? Colors.blue : Colors.grey,
  93. onPressed: () => _onItemTapped(1),
  94. ),
  95. SizedBox(width: 50), // Space for the FAB
  96. IconButton(
  97. icon: Icon(Icons.favorite),
  98. color: _selectedIndex == 2 ? Colors.blue : Colors.grey,
  99. onPressed: () => _onItemTapped(2),
  100. ),
  101. IconButton(
  102. icon: Icon(Icons.person),
  103. color: _selectedIndex == 3 ? Colors.blue : Colors.grey,
  104. onPressed: () => _onItemTapped(3),
  105. ),
  106. ],
  107. ),
  108. ),
  109. ),
  110. );
  111. }
  112. }
  113.  
  114. // Dialog (bottomsheet)
  115.  
  116. class AddHabitBottomSheet extends StatefulWidget {
  117. @override
  118. State<AddHabitBottomSheet> createState() => _AddHabitBottomSheetState();
  119. }
  120.  
  121. class _AddHabitBottomSheetState extends State<AddHabitBottomSheet> {
  122. final TextEditingController habitController = TextEditingController();
  123. final TextEditingController entryController = TextEditingController();
  124. Habit? selectedHabit;
  125. String? selectedEffort;
  126. final _formKey = GlobalKey<FormState>();
  127.  
  128. void _saveHabitEntry() {
  129. if (selectedEffort == null || selectedHabit == null) {
  130. showOverlaySnackBar(context, 'Missing habit or effort, please select one');
  131. return;
  132. }
  133. if (_formKey.currentState!.validate()) {
  134. print('hitting dat butttong');
  135.  
  136. var entry = new HabitEntrySave(
  137. habitId: selectedHabit!.id!,
  138. habitName: selectedHabit!.name,
  139. effort: selectedEffort!,
  140. );
  141. print(entry.effort);
  142. context.read<HabitsBloc>().add(HabitEntrySave(
  143. effort: selectedEffort!,
  144. habitId: selectedHabit!.id!,
  145. habitName: selectedHabit!.name,
  146. ));
  147. }
  148. }
  149.  
  150. @override
  151. void initState() {
  152. super.initState();
  153. context.read<HabitsBloc>().add(HabitsFetchAllHabits());
  154. }
  155.  
  156. @override
  157. void dispose() {
  158. super.dispose();
  159. }
  160.  
  161. @override
  162. Widget build(BuildContext context) {
  163. return Container(
  164. // color: Colors.black.withOpacity(0.2),
  165. padding: const EdgeInsets.all(16.0),
  166. // Wrap in a Container and set width and margin
  167. child: BlocConsumer<HabitsBloc, HabitsState>(
  168. listener: (context, state) {
  169. // Successfull Save of HabitEntry
  170. if (state is HabitEntrySaveSuccess) {
  171. print('HabitEntry Success here.');
  172.  
  173. Future.delayed(Duration(milliseconds: 500), () {
  174. context.read<HabitsBloc>().add(HabitEntrySaveCompleted());
  175.  
  176. showOverlaySnackBar(context, "success!!!");
  177. Navigator.of(context).pop(true);
  178.  
  179.  
  180. Navigator.pushAndRemoveUntil(context, HomePage.route(), (route) => false);
  181.  
  182. } else if (state is HabitEntrySaveFailure) {
  183. Navigator.of(context).pop(true);
  184. showOverlaySnackBar(context, state.error);
  185. }
  186.  
  187. },
  188. builder: (context, state) {
  189. if (state is HabitsLoading) {
  190. return const Loader();
  191. }
  192.  
  193. if (state is HabitsGetSuccess) {
  194. return Container(
  195. height: MediaQuery.of(context).size.height * 0.75,
  196. width: MediaQuery.of(context).size.width * 9, // Decrease width to 90% of the screen
  197. padding: const EdgeInsets.all(16.0),
  198. decoration: BoxDecoration(
  199. color: Colors.white.withOpacity(0.8), // Background color of the bottom sheet
  200. borderRadius: BorderRadius.all(Radius.circular(20)),
  201. boxShadow: [
  202. BoxShadow(
  203. color: Colors.black.withOpacity(0.1),
  204. blurRadius: 10,
  205. spreadRadius: 2,
  206. offset: Offset(0, -2), // Shadow above the bottom sheet
  207. ),
  208. ],
  209. ),
  210. child: Form(
  211. key: _formKey,
  212. child: Column(
  213. children: [
  214. Text(
  215. 'Habit Entry',
  216. style: AppTextStyles.heading2,
  217. ),
  218. SizedBox(height: 10),
  219. Column(
  220. children: [
  221. GridView.builder(
  222. shrinkWrap: true,
  223. physics: NeverScrollableScrollPhysics(),
  224. gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
  225. crossAxisCount: 4,
  226. crossAxisSpacing: 10,
  227. mainAxisSpacing: 10,
  228. childAspectRatio: 1,
  229. ),
  230. itemCount: state.habits.length,
  231. itemBuilder: (context, index) {
  232. Habit habit = state.habits[index];
  233. HabitInfo habitInfo = HabitInfoHelper.getHabitInfo(habit.name);
  234. // Check if this habit is selected.
  235. bool isSelected = habit == selectedHabit;
  236. return InkWell(
  237. onTap: () {
  238. setState(() {
  239. // Update selectedHabit
  240. selectedHabit = habit;
  241. });
  242. print('selected: ' + habit.name);
  243. },
  244. child: Container(
  245. // width: 50,
  246. // height: 50,
  247. decoration: BoxDecoration(
  248. color: isSelected ? AppPallete.backgroundColor.withOpacity(1) : Colors.white,
  249. borderRadius: BorderRadius.circular(60),
  250. border: Border.all(color: isSelected ? Colors.grey.shade500 : Colors.grey.shade200),
  251. boxShadow: isSelected
  252. ? [
  253. BoxShadow(
  254. color: Colors.grey,
  255. blurRadius: 3,
  256. spreadRadius: 1,
  257. )
  258. ]
  259. : [],
  260. ),
  261. child: Column(
  262. mainAxisAlignment: MainAxisAlignment.center,
  263. crossAxisAlignment: CrossAxisAlignment.center,
  264. children: [
  265. Icon(
  266. habitInfo.icon,
  267. color: habitInfo.color,
  268. size: 32,
  269. ),
  270. Text(
  271. habit.name,
  272. style: TextStyle(
  273. fontSize: 12,
  274. fontWeight: FontWeight.w500,
  275. ),
  276. textAlign: TextAlign.center,
  277. )
  278. ],
  279. ),
  280. ),
  281. );
  282. },
  283. ),
  284. ],
  285. ),
  286. // CircularSlider(),
  287. if (selectedHabit != null)
  288. TimeTrackerWidget(
  289. habit: selectedHabit,
  290. onEffortChanged: (effort) {
  291. setState(() {
  292. selectedEffort = effort;
  293. });
  294. }),
  295.  
  296. SizedBox(height: 20),
  297. SubmitButton(
  298. buttonText: 'Save Entry',
  299. onSubmit: _saveHabitEntry,
  300. ),
  301. ],
  302. ),
  303. ),
  304. );
  305. }
  306. return const SizedBox();
  307. },
  308. ),
  309. );
  310. }
  311. }
  312.  
  313.  
  314. // PAge example to navigate to which is wrapped inside a blocconsumer
  315.  
  316. if (state is HabitsGetSuccess) {
  317. return Padding(
  318. padding: const EdgeInsets.all(12.0),
  319. child: ListView(
  320. children: [
  321. Padding(
  322. padding: const EdgeInsets.symmetric(horizontal: 2),
  323. // Heading
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement