fix(url): URL 열기 시 canLaunchUrl 제거 및 try-catch 적용

- fetched_restaurant_json_view.dart: _launchNaverUrl try-catch 패턴 적용
- restaurant_card.dart: 카드 클릭 시 URL 열기, 상세보기 팝업 제거
- 웹 환경 호환성 개선 (platformDefault 폴백)
This commit is contained in:
JiWoong Sul
2026-01-28 18:54:51 +09:00
parent 48c22d76d0
commit b989981464
2 changed files with 88 additions and 109 deletions

View File

@@ -1,4 +1,5 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import '../../../../core/constants/app_colors.dart'; import '../../../../core/constants/app_colors.dart';
import '../../../../core/constants/app_typography.dart'; import '../../../../core/constants/app_typography.dart';
@@ -17,6 +18,7 @@ class FetchedRestaurantJsonView extends StatefulWidget {
final TextEditingController longitudeController; final TextEditingController longitudeController;
final TextEditingController naverUrlController; final TextEditingController naverUrlController;
final ValueChanged<String> onFieldChanged; final ValueChanged<String> onFieldChanged;
final String? naverUrl; // 순수 URL (클릭 가능한 링크용)
const FetchedRestaurantJsonView({ const FetchedRestaurantJsonView({
super.key, super.key,
@@ -32,6 +34,7 @@ class FetchedRestaurantJsonView extends StatefulWidget {
required this.longitudeController, required this.longitudeController,
required this.naverUrlController, required this.naverUrlController,
required this.onFieldChanged, required this.onFieldChanged,
this.naverUrl,
}); });
@override @override
@@ -126,7 +129,7 @@ class _FetchedRestaurantJsonViewState extends State<FetchedRestaurantJsonView> {
controller: widget.jibunAddressController, controller: widget.jibunAddressController,
icon: Icons.map, icon: Icons.map,
), ),
_buildCoordinateFields(context), _buildNaverUrlField(context),
_buildJsonField( _buildJsonField(
context, context,
label: '전화번호', label: '전화번호',
@@ -154,78 +157,79 @@ class _FetchedRestaurantJsonViewState extends State<FetchedRestaurantJsonView> {
); );
} }
Widget _buildCoordinateFields(BuildContext context) { Widget _buildNaverUrlField(BuildContext context) {
final border = OutlineInputBorder(borderRadius: BorderRadius.circular(8)); final url = widget.naverUrl ?? '';
if (url.isEmpty) return const SizedBox.shrink();
return Column( return Padding(
crossAxisAlignment: CrossAxisAlignment.start, padding: const EdgeInsets.only(bottom: 12),
children: [ child: Column(
Row( crossAxisAlignment: CrossAxisAlignment.start,
children: const [ children: [
Icon(Icons.my_location, size: 16), Row(
SizedBox(width: 8), children: const [
Text('좌표'), Icon(Icons.link, size: 16),
], SizedBox(width: 8),
), Text('네이버 지도:'),
const SizedBox(height: 6), ],
Row( ),
children: [ const SizedBox(height: 6),
Expanded( InkWell(
child: TextFormField( onTap: () => _launchNaverUrl(url),
controller: widget.latitudeController, borderRadius: BorderRadius.circular(8),
keyboardType: const TextInputType.numberWithOptions( child: Container(
decimal: true, width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 14),
decoration: BoxDecoration(
border: Border.all(
color: widget.isDark
? AppColors.darkDivider
: AppColors.lightDivider,
), ),
decoration: InputDecoration( borderRadius: BorderRadius.circular(8),
labelText: '위도', color: widget.isDark
border: border, ? AppColors.darkSurface
isDense: true, : AppColors.lightSurface,
), ),
onChanged: widget.onFieldChanged, child: Row(
validator: (value) { children: [
if (value != null && value.isNotEmpty) { Expanded(
final latitude = double.tryParse(value); child: Text(
if (latitude == null || latitude < -90 || latitude > 90) { url,
return '올바른 위도값을 입력해주세요'; style: TextStyle(
} color: Colors.blue[700],
} decoration: TextDecoration.underline,
return null; ),
}, overflow: TextOverflow.ellipsis,
),
),
const SizedBox(width: 8),
Icon(
Icons.open_in_new,
size: 18,
color: Colors.blue[700],
),
],
), ),
), ),
const SizedBox(width: 12), ),
Expanded( ],
child: TextFormField( ),
controller: widget.longitudeController,
keyboardType: const TextInputType.numberWithOptions(
decimal: true,
),
decoration: InputDecoration(
labelText: '경도',
border: border,
isDense: true,
),
onChanged: widget.onFieldChanged,
validator: (value) {
if (value != null && value.isNotEmpty) {
final longitude = double.tryParse(value);
if (longitude == null ||
longitude < -180 ||
longitude > 180) {
return '올바른 경도값을 입력해주세요';
}
}
return null;
},
),
),
],
),
const SizedBox(height: 12),
],
); );
} }
Future<void> _launchNaverUrl(String url) async {
final uri = Uri.tryParse(url);
if (uri == null) return;
try {
await launchUrl(uri, mode: LaunchMode.externalApplication);
} catch (_) {
// 웹 환경에서 실패 시 platformDefault 모드로 재시도
await launchUrl(uri, mode: LaunchMode.platformDefault);
}
}
Widget _buildCategoryField(BuildContext context) { Widget _buildCategoryField(BuildContext context) {
return RawAutocomplete<String>( return RawAutocomplete<String>(
textEditingController: widget.categoryController, textEditingController: widget.categoryController,

View File

@@ -1,8 +1,9 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:lunchpick/core/constants/app_colors.dart'; import 'package:lunchpick/core/constants/app_colors.dart';
import 'package:lunchpick/core/constants/app_typography.dart'; import 'package:lunchpick/core/constants/app_typography.dart';
import 'package:lunchpick/core/widgets/info_row.dart';
import 'package:lunchpick/domain/entities/restaurant.dart'; import 'package:lunchpick/domain/entities/restaurant.dart';
import 'package:lunchpick/presentation/providers/restaurant_provider.dart'; import 'package:lunchpick/presentation/providers/restaurant_provider.dart';
import 'edit_restaurant_dialog.dart'; import 'edit_restaurant_dialog.dart';
@@ -25,10 +26,13 @@ class RestaurantCard extends ConsumerWidget {
Widget build(BuildContext context, WidgetRef ref) { Widget build(BuildContext context, WidgetRef ref) {
final isDark = Theme.of(context).brightness == Brightness.dark; final isDark = Theme.of(context).brightness == Brightness.dark;
final hasNaverUrl = restaurant.naverUrl != null &&
restaurant.naverUrl!.isNotEmpty;
return Card( return Card(
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8), margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
child: InkWell( child: InkWell(
onTap: () => _showRestaurantDetail(context, isDark), onTap: hasNaverUrl ? () => _openNaverUrl(restaurant.naverUrl!) : null,
borderRadius: BorderRadius.circular(12), borderRadius: BorderRadius.circular(12),
child: Padding( child: Padding(
padding: const EdgeInsets.all(16), padding: const EdgeInsets.all(16),
@@ -211,6 +215,18 @@ class RestaurantCard extends ConsumerWidget {
); );
} }
Future<void> _openNaverUrl(String url) async {
final uri = Uri.tryParse(url);
if (uri == null) return;
try {
await launchUrl(uri, mode: LaunchMode.externalApplication);
} catch (_) {
// 웹 환경에서 실패 시 platformDefault 모드로 재시도
await launchUrl(uri, mode: LaunchMode.platformDefault);
}
}
IconData _getCategoryIcon(String category) { IconData _getCategoryIcon(String category) {
switch (category) { switch (category) {
case '한식': case '한식':
@@ -239,47 +255,6 @@ class RestaurantCard extends ConsumerWidget {
return daysSinceVisit == 0 ? '오늘 방문' : '$daysSinceVisit일 전 방문'; return daysSinceVisit == 0 ? '오늘 방문' : '$daysSinceVisit일 전 방문';
} }
void _showRestaurantDetail(BuildContext context, bool isDark) {
showDialog(
context: context,
builder: (context) => AlertDialog(
backgroundColor: isDark
? AppColors.darkSurface
: AppColors.lightSurface,
title: Text(restaurant.name),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
InfoRow(
label: '카테고리',
value: '${restaurant.category} > ${restaurant.subCategory}',
isDark: isDark,
),
if (restaurant.description != null)
InfoRow(label: '설명', value: restaurant.description!, isDark: isDark),
if (restaurant.phoneNumber != null)
InfoRow(label: '전화번호', value: restaurant.phoneNumber!, isDark: isDark),
InfoRow(label: '도로명 주소', value: restaurant.roadAddress, isDark: isDark),
InfoRow(label: '지번 주소', value: restaurant.jibunAddress, isDark: isDark),
if (restaurant.lastVisitDate != null)
InfoRow(
label: '마지막 방문',
value: '${restaurant.lastVisitDate!.year}${restaurant.lastVisitDate!.month}${restaurant.lastVisitDate!.day}',
isDark: isDark,
),
],
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('닫기'),
),
],
),
);
}
void _handleMenuAction( void _handleMenuAction(
_RestaurantMenuAction action, _RestaurantMenuAction action,
BuildContext context, BuildContext context,