From c8965dab4ed4084d6ca33849b266c68f6eb76b68 Mon Sep 17 00:00:00 2001 From: Sergey Elpashev Date: Tue, 4 Jun 2024 12:59:35 +0300 Subject: [PATCH] Add: Second club page --- lib/mobile_example.dart | 125 +++++++++++++++++++++++++++++++++++----- lib/pages/basket.dart | 2 +- 2 files changed, 110 insertions(+), 17 deletions(-) diff --git a/lib/mobile_example.dart b/lib/mobile_example.dart index ac18d73..8e60bbb 100644 --- a/lib/mobile_example.dart +++ b/lib/mobile_example.dart @@ -11,6 +11,22 @@ void main() { runApp(const MyExampleApp()); } +Future getToken(String token, String clientId) async { + debugPrint(token); + var url = Uri.http('gymlink.freemyip.com:8080', 'api/auth/authorize_client'); + try { + var response = await http.post(url, + body: {'GymKey': token, 'id': clientId}); // Just testing token + var decodedBody = jsonDecode(response.body) as Map; + if (decodedBody['payload'] == null) { + return ''; + } + return decodedBody['payload']['token']; + } catch (e) { + return ''; + } +} + class MyExampleApp extends StatelessWidget { const MyExampleApp({super.key}); @@ -45,6 +61,20 @@ Widget getDrawer(BuildContext context) => Drawer( ), ), ), + ListTile( + leading: const Icon(Icons.sell), + title: const Text('Club 2'), + onTap: () => Navigator.of(context).push( + MaterialPageRoute( + builder: (context) => ChangeNotifierProvider( + create: (_) => GymLinkProvider(), + child: Consumer( + builder: (_, value, __) => const ExampleClub2Page(), + ), + ), + ), + ), + ), ListTile( leading: const Icon(Icons.search), title: const Text('Example page'), @@ -97,7 +127,7 @@ class _ExamplePageState extends State { } Future _setToken() async { - final token = await _getToken(); + final token = await getToken('eeb42dcb-8e5b-4f21-825a-3fc7ada43445', '123'); if (token != '') { context.read().onTokenReceived(token); } else { @@ -105,21 +135,84 @@ class _ExamplePageState extends State { } } - Future _getToken() async { - var url = - Uri.http('gymlink.freemyip.com:8080', 'api/auth/authorize_client'); - try { - var response = await http.post(url, body: { - 'GymKey': 'eeb42dcb-8e5b-4f21-825a-3fc7ada43445', - 'id': '123' - }); // Just testing token - var decodedBody = jsonDecode(response.body) as Map; - if (decodedBody['payload'] == null) { - return ''; - } - return decodedBody['payload']['token']; - } catch (e) { - return ''; + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text('GymLink Example App'), + ), + resizeToAvoidBottomInset: false, + drawer: getDrawer(context), + body: Column( + children: [ + const Text('test'), + IconButton( + icon: const Icon(Icons.abc), + onPressed: () { + context.read().onTokenReceived('token123'); + }, + ), + const Expanded( + child: MyApp(), + ), + const SizedBox( + height: 20, + ), + const Text('Bottom text') + ], + ), + floatingActionButton: IconButton( + icon: const Icon(Icons.search), + onPressed: () { + context + .read() + .changeTheme(Random().nextInt(0xffffff + 1)); + }, + ), + ); + } +} + +class ExampleClub2Page extends StatefulWidget { + const ExampleClub2Page({super.key}); + + @override + State createState() => _ExampleClub2PageState(); +} + +class _ExampleClub2PageState extends State { + @override + void initState() { + super.initState(); + // Future.microtask( + // () => context.read().onTokenReceived('token123')); + Future.microtask(() => context + .read() + .setTheme(ThemeData.light(useMaterial3: true))); + + Future.microtask(() => context.read().setOnError(() { + const snackBar = SnackBar( + content: Text('Ошибка подключения'), + duration: Duration(seconds: 3), // Длительность отображения Snackbar + behavior: SnackBarBehavior + .fixed, // Поведение Snackbar (fixed или floating) + ); + ScaffoldMessenger.of(context).showSnackBar(snackBar); + Future.delayed(const Duration(seconds: 3)) + .then((value) => _setToken()); + })); + + Future.microtask(() async { + await _setToken(); + }); + } + + Future _setToken() async { + final token = await getToken('a8622a61-3142-487e-8db8-b6aebd4f04aa', '123'); + if (token != '') { + context.read().onTokenReceived(token); + } else { + context.read().onError(); } } diff --git a/lib/pages/basket.dart b/lib/pages/basket.dart index 3d70823..50fdbb7 100644 --- a/lib/pages/basket.dart +++ b/lib/pages/basket.dart @@ -296,7 +296,7 @@ class _BasketPageState extends State { child: Column( children: [ Text( - 'Итого: $totalPrice', + 'Итого: $totalPrice руб.', ), ElevatedButton( onPressed: () => Navigator.of(context).push(