diff --git a/lib/interfaces/items.dart b/lib/interfaces/items.dart index 50f98cc..b210cff 100644 --- a/lib/interfaces/items.dart +++ b/lib/interfaces/items.dart @@ -186,7 +186,7 @@ class GymHistoryItemDetail { final String id; final String date; final String sum; - final String payUrl; + String? payUrl; final String receiver; final String email; final String address; @@ -196,7 +196,7 @@ class GymHistoryItemDetail { required this.id, required this.date, required this.sum, - required this.payUrl, + this.payUrl, required this.providers, required this.receiver, required this.email, diff --git a/lib/pages/order_info.dart b/lib/pages/order_info.dart index 2099432..ae3d696 100644 --- a/lib/pages/order_info.dart +++ b/lib/pages/order_info.dart @@ -1,15 +1,19 @@ +import 'dart:async'; + +import 'package:flutter/foundation.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/components/order_detail_item_card.dart'; import 'package:gymlink_module_web/interfaces/items.dart'; -import 'package:lazy_load_scrollview/lazy_load_scrollview.dart'; +import 'package:url_launcher/url_launcher.dart'; final GymHistoryItemDetail item = GymHistoryItemDetail.fromJson({ "id": "12345", "date": "01.01.1970", "sum": "45000", - "pay_url": "url", + "pay_url": "https://example.org", "receiver": "Иванов Иван Иванович", "email": "a@a.ru", "address": "г. Москва, ул. Пушкина, д. 17", @@ -46,17 +50,37 @@ class OrderInfoPage extends StatefulWidget { class _OrderInfoPageState extends State { GymHistoryItemDetail? detail; final _scrollController = ScrollController(); + late Timer _updateTimer; @override void initState() { super.initState(); - setState(() { - detail = item; - }); - debugPrint("AAAAAAAAAAAA${detail?.toRawJson()}"); + _updateTimer = Timer.periodic(const Duration(minutes: 1), _onRefresh); + _onRefresh(_updateTimer); } - Future _onRefresh() { - return Future.delayed(const Duration(milliseconds: 1000)); + @override + void dispose() { + _updateTimer.cancel(); + super.dispose(); + } + + Future _onRefresh(Timer timer) async { + return Future.delayed(const Duration(milliseconds: 1000), () { + setState(() { + detail = item; + }); + }); + } + + Future _goToPage() async { + if (detail?.payUrl != null) { + final Uri url = Uri.parse(detail?.payUrl ?? 'example.org'); + detail!.payUrl = null; + if (!await launchUrl(url, webOnlyWindowName: '_blank')) { + throw 'Could not launch $url'; + } + _onRefresh(_updateTimer); + } } Widget _buildContent() { @@ -64,47 +88,152 @@ class _OrderInfoPageState extends State { children: [ GymLinkHeader(title: "Заказ #${detail?.id} от ${detail?.date}"), Expanded( - child: LazyLoadScrollView( - onEndOfPage: _onRefresh, - child: Scrollbar( - controller: _scrollController, - child: ListView( - controller: _scrollController, - children: [ - ListView.builder( - itemCount: 3, - physics: const NeverScrollableScrollPhysics(), - shrinkWrap: true, - itemBuilder: (context, index) { - final provider = detail!.providers[0]; - return const Card( - child: Text('test'), - ); - }, - ), - Expanded( - child: Card( - elevation: 4, - child: Padding( - padding: const EdgeInsetsDirectional.symmetric( - horizontal: 10, vertical: 20), - child: Column( + child: RefreshIndicator( + onRefresh: () => _onRefresh(_updateTimer), + edgeOffset: 55, + child: Scrollbar( + controller: _scrollController, + child: ListView( + controller: _scrollController, + children: [ + const SizedBox(height: 10), + kIsWeb + ? Center( + child: ElevatedButton( + onPressed: () => _onRefresh(_updateTimer), + style: ElevatedButton.styleFrom( + backgroundColor: Theme.of(context).primaryColor, + shape: const RoundedRectangleBorder( + borderRadius: + BorderRadius.all(Radius.circular(50)), + ), + foregroundColor: Colors.white, + fixedSize: const Size(180, 40), + ), + child: const Row( mainAxisSize: MainAxisSize.min, children: [ - MarkdownBody( - data: - "### Адрес получателя: _${detail!.address}_"), - Markdown(data: '### Почта: _${detail!.email}_'), - // Markdown( - // data: '### ФИО: _${detail!.receiver}_'), + Text('Обновить'), + Spacer(), + Icon(Icons.refresh), + Spacer() ], ), ), + ) + : const SizedBox.shrink(), + const SizedBox(height: 10), + ListView.builder( + itemCount: 3, + physics: const NeverScrollableScrollPhysics(), + shrinkWrap: true, + itemBuilder: (context, index) { + final provider = detail!.providers[0]; + return Padding( + padding: const EdgeInsetsDirectional.symmetric( + vertical: 10, + horizontal: 5, + ), + child: Card( + elevation: 3, + child: Column( + children: [ + MarkdownBody(data: '# ${provider.name}'), + MarkdownBody( + data: '## Статус: ${provider.status}'), + const MarkdownBody(data: '### Состав:'), + for (final item in provider.items) + OrderDetailCardItemCard( + image: FutureBuilder( + future: precacheImage( + const AssetImage('assets/product.png'), + context), + builder: (context, snapshot) { + if (snapshot.connectionState == + ConnectionState.done) { + return const Image( + image: + AssetImage('assets/product.png'), + width: 50, + ); + } else { + return const CircularProgressIndicator(); + } + }, + ), + name: 'Протеин', + count: item.count, + price: double.parse(item.price), + ), + ], + ), + ), + ); + }, + ), + const SizedBox( + height: 10, + ), + SizedBox( + height: 200, + child: Padding( + padding: const EdgeInsetsDirectional.symmetric( + horizontal: 5, + ), + child: Card( + elevation: 4, + child: Padding( + padding: const EdgeInsetsDirectional.symmetric( + horizontal: 10, vertical: 20), + child: Column( + // mainAxisSize: MainAxisSize.min, + mainAxisAlignment: MainAxisAlignment.spaceBetween, + // crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + MarkdownBody( + data: + "### Адрес получателя: __${detail!.address}__"), + MarkdownBody( + data: '### Почта: __${detail!.email}__'), + MarkdownBody( + data: '### ФИО: _${detail!.receiver}_'), + ], + ), + detail?.payUrl == null + ? const SizedBox.shrink() + : Center( + child: ElevatedButton( + onPressed: () async { + await _goToPage(); + debugPrint('clicked'); + }, + style: ElevatedButton.styleFrom( + backgroundColor: + Theme.of(context).primaryColor, + shape: const RoundedRectangleBorder( + borderRadius: BorderRadius.all( + Radius.circular(50)), + ), + foregroundColor: Colors.white, + fixedSize: const Size(180, 40), + ), + child: const Text('Оплатить заказ'), + ), + ), + ], + ), ), ), - ], + ), ), - ))), + ], + ), + ), + ), + ), ], ); }