-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add competition banner on home page (#8)
* feat: add competition banner on home page * feat: add query intent for url launcher in android * fix: dart formatting
- Loading branch information
Sandip Pramanik
authored
Aug 7, 2024
1 parent
edb3e2a
commit 31a0483
Showing
13 changed files
with
274 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:sudoku/colors/colors.dart'; | ||
import 'package:sudoku/l10n/l10n.dart'; | ||
import 'package:sudoku/typography/typography.dart'; | ||
import 'package:sudoku/utilities/utilities.dart'; | ||
|
||
/// The url for the Gemini API Competition. | ||
const _competitionUrl = 'https://ai.google.dev/competition'; | ||
|
||
/// {@template competition_banner} | ||
/// Displays a banner in the Home Page that launches the Gemini | ||
/// API Competition details website. | ||
/// {@endtemplate} | ||
class CompetitionBanner extends StatelessWidget { | ||
/// {@macro competition_banner} | ||
const CompetitionBanner({super.key}); | ||
|
||
static const gradient = LinearGradient( | ||
colors: [SudokuColors.darkPurple, SudokuColors.darkPink], | ||
begin: Alignment.bottomLeft, | ||
end: Alignment.topRight, | ||
); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final l10n = context.l10n; | ||
return OutlinedButton( | ||
onPressed: () => openLink(Uri.parse(_competitionUrl)), | ||
style: OutlinedButton.styleFrom( | ||
padding: const EdgeInsets.fromLTRB(24, 0, 24, 2.5), | ||
), | ||
child: Text.rich( | ||
TextSpan( | ||
children: [ | ||
WidgetSpan( | ||
child: Text( | ||
l10n.competitionBannerText, | ||
style: SudokuTextStyle.caption.copyWith( | ||
fontWeight: SudokuFontWeight.medium, | ||
fontSize: 12, | ||
), | ||
), | ||
), | ||
WidgetSpan( | ||
child: ShaderMask( | ||
blendMode: BlendMode.srcIn, | ||
shaderCallback: (bounds) => gradient.createShader( | ||
Rect.fromLTWH(0, 0, bounds.width, bounds.height), | ||
), | ||
child: Text( | ||
l10n.competitionBannerCta, | ||
style: SudokuTextStyle.caption.copyWith( | ||
fontWeight: SudokuFontWeight.semiBold, | ||
fontSize: 12, | ||
), | ||
), | ||
), | ||
), | ||
], | ||
), | ||
textAlign: TextAlign.center, | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export 'competition_banner.dart'; | ||
export 'player_info.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:url_launcher/url_launcher.dart'; | ||
|
||
/// Opens the given [url] in a new tab of the host browser | ||
Future<void> openLink(Uri url, {VoidCallback? onError}) async { | ||
if (await canLaunchUrl(url)) { | ||
await launchUrl(url); | ||
} else if (onError != null) { | ||
onError(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export 'difficulty_extension.dart'; | ||
export 'link_helper.dart'; | ||
export 'time_formatter.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// ignore_for_file: prefer_const_constructors | ||
|
||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
import 'package:sudoku/utilities/utilities.dart'; | ||
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart'; | ||
|
||
import 'helpers.dart'; | ||
|
||
class _FakeLaunchOptions extends Fake implements LaunchOptions {} | ||
|
||
void main() { | ||
late UrlLauncherPlatform urlLauncher; | ||
|
||
setUp(() { | ||
urlLauncher = MockUrlLauncher(); | ||
UrlLauncherPlatform.instance = urlLauncher; | ||
}); | ||
|
||
setUpAll(() { | ||
registerFallbackValue(_FakeLaunchOptions()); | ||
}); | ||
|
||
group('openLink', () { | ||
final uri = Uri.parse('uri'); | ||
final url = uri.toString(); | ||
|
||
test('launches the link', () async { | ||
when(() => urlLauncher.canLaunch(url)).thenAnswer((_) async => true); | ||
when(() => urlLauncher.launchUrl(any(), any())) | ||
.thenAnswer((_) async => true); | ||
await openLink(uri); | ||
verify( | ||
() => urlLauncher.launchUrl(url, any()), | ||
).called(1); | ||
}); | ||
|
||
test('executes the onError callback when it cannot launch', () async { | ||
var wasCalled = false; | ||
when(() => urlLauncher.canLaunch(url)).thenAnswer((_) async => false); | ||
await openLink(uri, onError: () => wasCalled = true); | ||
|
||
await expectLater(wasCalled, isTrue); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// ignore_for_file: prefer_const_constructors | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
import 'package:sudoku/home/home.dart'; | ||
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart'; | ||
|
||
import '../../helpers/helpers.dart'; | ||
|
||
class _FakeLaunchOptions extends Fake implements LaunchOptions {} | ||
|
||
void main() { | ||
group('CompetitionBanner', () { | ||
late UrlLauncherPlatform urlLauncher; | ||
|
||
setUp(() { | ||
urlLauncher = MockUrlLauncher(); | ||
UrlLauncherPlatform.instance = urlLauncher; | ||
|
||
when(() => urlLauncher.canLaunch(any())).thenAnswer((_) async => true); | ||
when(() => urlLauncher.launchUrl(any(), any())) | ||
.thenAnswer((_) async => true); | ||
}); | ||
|
||
setUpAll(() { | ||
registerFallbackValue(_FakeLaunchOptions()); | ||
}); | ||
|
||
testWidgets('renders an [OutlinedButton]', (tester) async { | ||
await tester.pumpApp(CompetitionBanner()); | ||
expect(find.byType(OutlinedButton), findsOneWidget); | ||
}); | ||
|
||
testWidgets('opens a link when tapped', (tester) async { | ||
await tester.pumpApp(CompetitionBanner()); | ||
await tester.tap(find.byType(OutlinedButton)); | ||
|
||
verify( | ||
() => urlLauncher.launchUrl(any(), any()), | ||
).called(1); | ||
}); | ||
}); | ||
} |