add_friend.dart 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import 'package:flutter/material.dart';
  2. import 'package:physigo/friends/services/friends_service.dart';
  3. class AddFriend extends StatefulWidget {
  4. const AddFriend({Key? key}) : super(key: key);
  5. @override
  6. State<AddFriend> createState() => _AddFriendState();
  7. }
  8. class _AddFriendState extends State<AddFriend> {
  9. late final TextEditingController _controller;
  10. @override
  11. void initState() {
  12. _controller = TextEditingController();
  13. super.initState();
  14. }
  15. void _addFriend() async {
  16. try {
  17. await FriendsService.addFriend(_controller.text);
  18. _controller.clear();
  19. _showSnackBar("Friend request sent!", Colors.green);
  20. } catch (error) {
  21. _showSnackBar(error.toString(), Colors.red);
  22. }
  23. }
  24. void _showSnackBar(String message, Color color) {
  25. ScaffoldMessenger.of(context).showSnackBar(SnackBar(
  26. content: Text(message),
  27. backgroundColor: color,
  28. duration: const Duration(milliseconds: 1500),
  29. ));
  30. }
  31. @override
  32. Widget build(BuildContext context) {
  33. return Column(
  34. mainAxisAlignment: MainAxisAlignment.center,
  35. children: [
  36. TextField(
  37. onSubmitted: (_) => _addFriend(),
  38. controller: _controller,
  39. decoration: const InputDecoration(
  40. border: OutlineInputBorder(),
  41. hintText: "Enter a friend's ID to add them",
  42. ),
  43. ),
  44. const SizedBox(height: 32),
  45. ElevatedButton(
  46. onPressed: _addFriend,
  47. style: ElevatedButton.styleFrom(
  48. padding: const EdgeInsets.symmetric(vertical: 16, horizontal: 32),
  49. ),
  50. child: const Text("ADD",
  51. style: TextStyle(
  52. fontSize: 20,
  53. fontWeight: FontWeight.bold,
  54. letterSpacing: 1.3,
  55. )),
  56. )
  57. ],
  58. );
  59. }
  60. }