Skip to content

Commit

Permalink
[feat] 카페 이미지 handle marker tap에서 가져오는 것으로 변경(임시) #387
Browse files Browse the repository at this point in the history
  • Loading branch information
jm3789 committed May 29, 2024
1 parent 88dcf81 commit 1fa37ac
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 37 deletions.
36 changes: 3 additions & 33 deletions frontend/lib/screen/cafe_details.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ class CafeDetails extends StatefulWidget {
final String cafeId;
final String cafeName;
final List<String> cafeDetailsArguments;
final Image? cafeImage;

const CafeDetails({
super.key,
this.cafeId = "defaultCafeId",
this.cafeName = "defaultCafeName",
this.cafeDetailsArguments = const [],
this.cafeImage,
});

@override
Expand Down Expand Up @@ -118,27 +120,9 @@ class _CafeDetailsState extends State<CafeDetails>
autoOfflineService.autoOffline();
}

Future<void> getPlacePhotoUri() async {
try {
PlacesDetailsResponse place =
await places.getDetailsByPlaceId(widget.cafeDetailsArguments[9]);
if (place.isOkay && place.result.photos.isNotEmpty) {
String photoReference = place.result.photos[0].photoReference;
photoUrl =
'https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=$photoReference&key=${dotenv.env['googleApiKey']}';
setState(() {});
} else {
throw Exception('No photo found for this place.');
}
} catch (e) {
print('Error: $e');
}
}

@override
void initState() {
super.initState();
getPlacePhotoUri();
tabController = TabController(length: 2, vsync: this);
}

Expand Down Expand Up @@ -181,21 +165,7 @@ class _CafeDetailsState extends State<CafeDetails>
),
body: Column(
children: [
Center(
child: photoUrl.isNotEmpty
? Image.network(
photoUrl,
width: 450,
height: 250,
fit: BoxFit.cover,
)
: Image.asset(
"assets/no_image.png",
width: 450,
height: 250,
fit: BoxFit.fitWidth,
),
),
Center(child: widget.cafeImage),
TabBar(
controller: tabController,
indicatorColor: Colors.black,
Expand Down
53 changes: 49 additions & 4 deletions frontend/lib/screen/map_place.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:frontend/model/matching_info_model.dart';
import 'package:frontend/service/api_service.dart';
import 'package:frontend/widgets/dialog/yn_dialog.dart';
import 'package:frontend/widgets/dialog/one_button_dialog.dart';
import 'package:google_maps_webservice/places.dart';
import 'package:stomp_dart_client/stomp.dart';
import 'package:frontend/service/stomp_service.dart';
import 'package:provider/provider.dart';
Expand Down Expand Up @@ -231,13 +232,57 @@ class _GoogleMapWidgetState extends State<Google_Map> {
cafeId,
];

// cafeId를 인자로 받아 photoUrl을 만들고 image를 반환하는 메소드
Future<Image> cafeIdToCafeImage(String cafeId) async {
final places = GoogleMapsPlaces(apiKey: "${dotenv.env['googleApiKey']}");
String photoUrl = '';
try {
PlacesDetailsResponse place = await places.getDetailsByPlaceId(cafeId);
if (place.isOkay && place.result.photos.isNotEmpty) {
String photoReference = place.result.photos[0].photoReference;
photoUrl =
'https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=$photoReference&key=${dotenv.env['googleApiKey']}';
} else {
throw Exception('No photo found for this place.');
}
} catch (e) {
print('Error: $e');
}
return photoUrl.isNotEmpty
? Image.network(
photoUrl,
width: 450,
height: 250,
fit: BoxFit.cover,
)
: Image.asset(
"assets/no_image.png",
width: 450,
height: 250,
fit: BoxFit.fitWidth,
);
}

Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CafeDetails(
cafeId: cafeId,
cafeName: cafeName,
cafeDetailsArguments: detailsArguments,
builder: (context) => FutureBuilder<Image>(
future: cafeIdToCafeImage(cafeId),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
}
return CafeDetails(
cafeId: cafeId,
cafeName: cafeName,
cafeDetailsArguments: detailsArguments,
cafeImage: snapshot.data!,
);
} else {
return const Center(child: CircularProgressIndicator());
}
},
),
),
);
Expand Down

0 comments on commit 1fa37ac

Please sign in to comment.