Add: Second club page

This commit is contained in:
2024-06-04 12:59:35 +03:00
parent 97664fdb5a
commit c8965dab4e
2 changed files with 110 additions and 17 deletions

View File

@@ -11,6 +11,22 @@ void main() {
runApp(const MyExampleApp());
}
Future<String> 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<GymLinkProvider>(
builder: (_, value, __) => const ExampleClub2Page(),
),
),
),
),
),
ListTile(
leading: const Icon(Icons.search),
title: const Text('Example page'),
@@ -97,7 +127,7 @@ class _ExamplePageState extends State<ExamplePage> {
}
Future<void> _setToken() async {
final token = await _getToken();
final token = await getToken('eeb42dcb-8e5b-4f21-825a-3fc7ada43445', '123');
if (token != '') {
context.read<GymLinkProvider>().onTokenReceived(token);
} else {
@@ -105,21 +135,84 @@ class _ExamplePageState extends State<ExamplePage> {
}
}
Future<String> _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<GymLinkProvider>().onTokenReceived('token123');
},
),
const Expanded(
child: MyApp(),
),
const SizedBox(
height: 20,
),
const Text('Bottom text')
],
),
floatingActionButton: IconButton(
icon: const Icon(Icons.search),
onPressed: () {
context
.read<GymLinkProvider>()
.changeTheme(Random().nextInt(0xffffff + 1));
},
),
);
}
}
class ExampleClub2Page extends StatefulWidget {
const ExampleClub2Page({super.key});
@override
State<ExampleClub2Page> createState() => _ExampleClub2PageState();
}
class _ExampleClub2PageState extends State<ExampleClub2Page> {
@override
void initState() {
super.initState();
// Future.microtask(
// () => context.read<GymLinkProvider>().onTokenReceived('token123'));
Future.microtask(() => context
.read<GymLinkProvider>()
.setTheme(ThemeData.light(useMaterial3: true)));
Future.microtask(() => context.read<GymLinkProvider>().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<void> _setToken() async {
final token = await getToken('a8622a61-3142-487e-8db8-b6aebd4f04aa', '123');
if (token != '') {
context.read<GymLinkProvider>().onTokenReceived(token);
} else {
context.read<GymLinkProvider>().onError();
}
}

View File

@@ -296,7 +296,7 @@ class _BasketPageState extends State<BasketPage> {
child: Column(
children: [
Text(
'Итого: $totalPrice',
'Итого: $totalPrice руб.',
),
ElevatedButton(
onPressed: () => Navigator.of(context).push(