Skip to content

Commit

Permalink
Add tryToLaunchUrl function in the app_utils to have a single place t…
Browse files Browse the repository at this point in the history
…o open the URLs
  • Loading branch information
imaNNeo committed Nov 13, 2023
1 parent 0aaf7ee commit 87f0444
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 23 deletions.
35 changes: 24 additions & 11 deletions example/lib/presentation/menu/app_menu.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'package:dartx/dartx.dart';
import 'package:fl_chart_app/cubits/app/app_cubit.dart';
import 'package:fl_chart_app/presentation/resources/app_resources.dart';
import 'package:fl_chart_app/urls.dart';
import 'package:fl_chart_app/util/app_helper.dart';
import 'package:fl_chart_app/util/app_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:url_launcher/url_launcher.dart';
Expand Down Expand Up @@ -116,18 +118,29 @@ class _AppVersionRow extends StatelessWidget {
),
),
),
if (state.availableVersionToUpdate.isNotBlank)
TextButton(
onPressed: () {},
child: const Text(
'Update',
style: TextStyle(
color: AppColors.primary,
fontSize: 12,
fontWeight: FontWeight.bold,
state.availableVersionToUpdate.isNotBlank
? TextButton(
onPressed: () {},
child: Text(
'Update to ${state.availableVersionToUpdate}',
style: const TextStyle(
color: AppColors.primary,
fontSize: 12,
fontWeight: FontWeight.bold,
),
),
)
: TextButton(
onPressed: () => AppUtils().tryToLaunchUrl(Urls.aboutUrl),
child: const Text(
'About',
style: TextStyle(
color: AppColors.primary,
fontSize: 12,
fontWeight: FontWeight.bold,
),
),
),
),
),
],
),
);
Expand Down
7 changes: 2 additions & 5 deletions example/lib/presentation/pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import 'package:fl_chart_app/presentation/menu/app_menu.dart';
import 'package:fl_chart_app/presentation/resources/app_resources.dart';
import 'package:fl_chart_app/urls.dart';
import 'package:fl_chart_app/util/app_helper.dart';
import 'package:fl_chart_app/util/app_utils.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:url_launcher/url_launcher.dart';

import 'chart_samples_page.dart';

Expand Down Expand Up @@ -56,10 +56,7 @@ class HomePage extends StatelessWidget {
onBannerClicked: kIsWeb || needsDrawer
? () async {
if (kIsWeb) {
final url = Uri.parse(Urls.flChartUrl);
if (await canLaunchUrl(url)) {
await launchUrl(url);
}
await AppUtils().tryToLaunchUrl(Urls.flChartUrl);
return;
}
if (needsDrawer) {
Expand Down
9 changes: 2 additions & 7 deletions example/lib/presentation/widgets/chart_holder.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:fl_chart_app/presentation/resources/app_resources.dart';
import 'package:fl_chart_app/presentation/samples/chart_sample.dart';
import 'package:fl_chart_app/util/app_utils.dart';
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

class ChartHolder extends StatelessWidget {
final ChartSample chartSample;
Expand Down Expand Up @@ -30,12 +30,7 @@ class ChartHolder extends StatelessWidget {
),
Expanded(child: Container()),
IconButton(
onPressed: () async {
final url = Uri.parse(chartSample.url);
if (await canLaunchUrl(url)) {
await launchUrl(url);
}
},
onPressed: () => AppUtils().tryToLaunchUrl(chartSample.url),
icon: const Icon(
Icons.code,
color: AppColors.primary,
Expand Down
2 changes: 2 additions & 0 deletions example/lib/urls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ class Urls {
final chartDir = chartType.name.toLowerCase();
return 'https://github.com/imaNNeo/fl_chart/blob/master/repo_files/documentations/${chartDir}_chart.md';
}

static String get aboutUrl => '$flChartUrl/about';
}
10 changes: 10 additions & 0 deletions example/lib/util/app_utils.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'dart:math' as math;

import 'package:url_launcher/url_launcher.dart';

class AppUtils {
factory AppUtils() {
return _singleton;
Expand All @@ -15,4 +17,12 @@ class AppUtils {
double radianToDegree(double radian) {
return radian * 180 / math.pi;
}

Future<bool> tryToLaunchUrl(String url) async {
final uri = Uri.parse(url);
if (await canLaunchUrl(uri)) {
return await launchUrl(uri);
}
return false;
}
}

0 comments on commit 87f0444

Please sign in to comment.