From 1e5b235a6cf93dd3a5fb241b2d3fde7a86813e3b Mon Sep 17 00:00:00 2001 From: Sergey Elpashev Date: Tue, 4 Jun 2024 15:57:17 +0300 Subject: [PATCH] Enhancement: back button to main menu reload --- lib/components/heading.dart | 13 +++++++++-- lib/pages/basket.dart | 7 +++++- lib/pages/detail.dart | 43 ++++++++++++++++++++++-------------- lib/pages/order_history.dart | 5 ++++- 4 files changed, 48 insertions(+), 20 deletions(-) diff --git a/lib/components/heading.dart b/lib/components/heading.dart index 030363a..d3145ea 100644 --- a/lib/components/heading.dart +++ b/lib/components/heading.dart @@ -1,8 +1,11 @@ import 'package:flutter/material.dart'; +import 'package:gymlink_module_web/pages/main.dart'; +import 'package:gymlink_module_web/tools/routes.dart'; class GymLinkHeader extends StatelessWidget { final String title; - const GymLinkHeader({super.key, required this.title}); + final bool toMain; + const GymLinkHeader({super.key, required this.title, this.toMain = false}); @override Widget build(BuildContext context) { @@ -13,7 +16,13 @@ class GymLinkHeader extends StatelessWidget { Row( children: [ IconButton( - onPressed: () => Navigator.pop(context), + onPressed: () => toMain + ? Navigator.pushAndRemoveUntil( + context, + CustomPageRoute( + builder: (context) => const MainPage()), + (route) => route.isFirst) + : Navigator.pop(context), icon: const Icon(Icons.arrow_back)), Text(title, style: Theme.of(context).textTheme.titleLarge), ], diff --git a/lib/pages/basket.dart b/lib/pages/basket.dart index 50fdbb7..14c37a5 100644 --- a/lib/pages/basket.dart +++ b/lib/pages/basket.dart @@ -3,6 +3,7 @@ import 'package:gymlink_module_web/components/app_bar.dart'; import 'package:gymlink_module_web/components/basket_item_card.dart'; import 'package:gymlink_module_web/components/heading.dart'; import 'package:gymlink_module_web/interfaces/items.dart'; +import 'package:gymlink_module_web/pages/main.dart'; import 'package:gymlink_module_web/pages/order_confirmation.dart'; import 'package:gymlink_module_web/providers/cart.dart'; import 'package:gymlink_module_web/tools/items.dart'; @@ -244,7 +245,11 @@ class _BasketPageState extends State { style: Theme.of(context).textTheme.bodyLarge), const SizedBox(height: 10), ElevatedButton( - onPressed: () => Navigator.pop(context, true), + onPressed: () => Navigator.pushAndRemoveUntil( + context, + CustomPageRoute( + builder: (_) => const MainPage()), + (route) => route.isFirst), style: ElevatedButton.styleFrom( backgroundColor: Theme.of(context).primaryColor, shape: const RoundedRectangleBorder( diff --git a/lib/pages/detail.dart b/lib/pages/detail.dart index caceb6b..27c4b2b 100644 --- a/lib/pages/detail.dart +++ b/lib/pages/detail.dart @@ -3,6 +3,7 @@ import 'dart:math'; import 'package:carousel_slider/carousel_slider.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_markdown/flutter_markdown.dart'; import 'package:gymlink_module_web/components/app_bar.dart'; import 'package:gymlink_module_web/components/heading.dart'; import 'package:gymlink_module_web/interfaces/items.dart'; @@ -30,7 +31,7 @@ class _DetailPageState extends State { bool isInCart = false; int quantity = 0; GymItem? item; - String categoryName = ''; + String? categoryName; final CarouselController _carouselController = CarouselController(); int _currentImage = 0; @@ -68,12 +69,14 @@ class _DetailPageState extends State { }); if (mounted) { getCategories(context).then((value) { - categoryName = value - .firstWhere( - (element) => element.id == (item!.categoryId), - orElse: () => GymCategory(id: item!.categoryId, name: ''), - ) - .name; + setState(() { + categoryName = value + .firstWhere( + (element) => element.id == (item!.categoryId), + orElse: () => GymCategory(id: item!.categoryId, name: ''), + ) + .name; + }); }); } } @@ -147,10 +150,12 @@ class _DetailPageState extends State { IconButton( icon: const Icon(Icons.add), onPressed: () async { - await addItemToCart(widget.id); - setState(() { - quantity++; - }); + if (item!.count > quantity) { + await addItemToCart(widget.id); + setState(() { + quantity++; + }); + } }, ), ], @@ -260,15 +265,21 @@ class _DetailPageState extends State { }).toList(), ), ]) - : Image.network(item!.images[0].url, - width: min( - 550, MediaQuery.sizeOf(context).width)), + : Image.network( + item!.images[0].url, + height: 400, + ), Padding( padding: const EdgeInsets.symmetric(vertical: 10), child: Center( - child: Text( - 'Категория: ${categoryName == "" ? "Без категории" : categoryName}'), + child: Text(categoryName != null + ? 'Категория: ${categoryName == "" ? "Без категории" : categoryName}' + : ''), )), + Center( + child: MarkdownBody( + data: '### Отстаток: _${item!.count}_', + )), item!.description != '' ? Padding( padding: const EdgeInsetsDirectional.all(30), diff --git a/lib/pages/order_history.dart b/lib/pages/order_history.dart index 779f072..80a0766 100644 --- a/lib/pages/order_history.dart +++ b/lib/pages/order_history.dart @@ -91,7 +91,10 @@ class _HistoryPageState extends State { body: Column( mainAxisAlignment: MainAxisAlignment.start, children: [ - const GymLinkHeader(title: 'История заказов'), + const GymLinkHeader( + title: 'История заказов', + toMain: true, + ), Expanded( child: Row( children: [