add_friend.dart 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. String _friendSharedId = "";
  10. void _onTextUpdate(String newFriendSharedId) {
  11. setState(() {
  12. _friendSharedId = newFriendSharedId;
  13. });
  14. }
  15. void _addFriend() async {
  16. try {
  17. await FriendsService.addFriend(_friendSharedId);
  18. setState(() {
  19. _friendSharedId = "";
  20. });
  21. _showSnackBar("Friend request sent!", Colors.green);
  22. } catch (error) {
  23. _showSnackBar(error.toString(), Colors.red);
  24. }
  25. }
  26. void _showSnackBar(String message, Color color) {
  27. ScaffoldMessenger.of(context).showSnackBar(SnackBar(
  28. content: Text(message),
  29. backgroundColor: color,
  30. duration: const Duration(milliseconds: 1500),
  31. ));
  32. }
  33. @override
  34. Widget build(BuildContext context) {
  35. return Column(
  36. mainAxisAlignment: MainAxisAlignment.center,
  37. children: [
  38. TextField(
  39. onSubmitted: (_) => _addFriend(),
  40. onChanged: _onTextUpdate,
  41. decoration: const InputDecoration(
  42. border: OutlineInputBorder(),
  43. hintText: "Enter a friend's ID to add them",
  44. ),
  45. ),
  46. const SizedBox(height: 32),
  47. ElevatedButton(
  48. onPressed: _addFriend,
  49. style: ElevatedButton.styleFrom(
  50. padding: const EdgeInsets.symmetric(vertical: 16, horizontal: 32),
  51. ),
  52. child: const Text("ADD",
  53. style: TextStyle(
  54. fontSize: 20,
  55. fontWeight: FontWeight.bold,
  56. letterSpacing: 1.3,
  57. )),
  58. )
  59. ],
  60. );
  61. }
  62. }