Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for web #103

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Pick a unique package name across Play Store and App Store then update these fil
- `macos/fastlane/Appfile`
- `macos/fastlane/Fastfile` notarize
- `macos/fastlane/Matchfile`
- `web/manifest.json`
- `windows/runner/Runner.rc`
- `pubspec.yaml` msix_config>identity_name

Expand Down
7 changes: 5 additions & 2 deletions lib/src/abstracts/error_reporting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import 'package:flutter/foundation.dart';

import 'firebase.dart' as firebase;

final backend =
firebase.isSupported ? Backend.firebaseCrashlytics : Backend.none;
final backend = kIsWeb
? Backend.none
: firebase.isSupported
? Backend.firebaseCrashlytics
: Backend.none;

void crash() => FirebaseCrashlytics.instance.crash();

Expand Down
4 changes: 3 additions & 1 deletion lib/src/abstracts/firebase.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'dart:io';

import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/foundation.dart';

final isSupported = Platform.isAndroid || Platform.isIOS || Platform.isMacOS;
final isSupported =
kIsWeb || Platform.isAndroid || Platform.isIOS || Platform.isMacOS;

Future<void> initializeApp() async {
if (!isSupported) return;
Expand Down
3 changes: 2 additions & 1 deletion lib/src/abstracts/uni_links.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import 'dart:async';
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:uni_links/uni_links.dart' as lib;

final isSupported = Platform.isAndroid || Platform.isIOS;
final isSupported = !kIsWeb && (Platform.isAndroid || Platform.isIOS);

Future<String> getInitialLink() async {
if (!isSupported) return null;
Expand Down
Binary file added web/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added web/icons/Icon-192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added web/icons/Icon-512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
115 changes: 115 additions & 0 deletions web/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<!DOCTYPE html>
<html>
<head>
<!--
If you are serving your web app in a path other than the root, change the
href value below to reflect the base path you are serving from.

The path provided below has to start and end with a slash "/" in order for
it to work correctly.

For more details:
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base
-->
<base href="/">

<meta charset="UTF-8">
<meta content="IE=Edge" http-equiv="X-UA-Compatible">
<meta name="description" content="Tinh tế mobile apps built with Flutter for evaluation purposes.">

<!-- iOS meta tags & icons -->
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-title" content="ttdemo">
<link rel="apple-touch-icon" href="icons/Icon-192.png">

<title>ttdemo</title>
<link rel="manifest" href="manifest.json">

<script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-messaging.js"></script>
</head>

<body>
<script>
const firebaseConfig = {
apiKey: "AIzaSyD9D74F32bkOoQfK0SftyUlaAOyZ5-fEsg",
authDomain: "flutterttdemo.firebaseapp.com",
databaseURL: "https://flutterttdemo.firebaseio.com",
projectId: "flutterttdemo",
storageBucket: "flutterttdemo.appspot.com",
messagingSenderId: "638176411768",
appId: "1:638176411768:web:7ac6decfebd1a13b556d8d"
};
firebase.initializeApp(firebaseConfig);
</script>

<!-- This script installs service_worker.js to provide PWA functionality to
application. For more information, see:
https://developers.google.com/web/fundamentals/primers/service-workers -->
<script>
var serviceWorkerVersion = null;
var scriptLoaded = false;
function loadMainDartJs() {
if (scriptLoaded) {
return;
}
scriptLoaded = true;
var scriptTag = document.createElement('script');
scriptTag.src = 'main.dart.js';
scriptTag.type = 'application/javascript';
document.body.append(scriptTag);
}

if ('serviceWorker' in navigator) {
// Service workers are supported. Use them.
window.addEventListener('load', function () {
// Wait for registration to finish before dropping the <script> tag.
// Otherwise, the browser will load the script multiple times,
// potentially different versions.
var serviceWorkerUrl = 'flutter_service_worker.js?v=' + serviceWorkerVersion;
navigator.serviceWorker.register(serviceWorkerUrl)
.then((reg) => {
function waitForActivation(serviceWorker) {
serviceWorker.addEventListener('statechange', () => {
if (serviceWorker.state == 'activated') {
console.log('Installed new service worker.');
loadMainDartJs();
}
});
}
if (!reg.active && (reg.installing || reg.waiting)) {
// No active web worker and we have installed or are installing
// one for the first time. Simply wait for it to activate.
waitForActivation(reg.installing ?? reg.waiting);
} else if (!reg.active.scriptURL.endsWith(serviceWorkerVersion)) {
// When the app updates the serviceWorkerVersion changes, so we
// need to ask the service worker to update.
console.log('New service worker available.');
reg.update();
waitForActivation(reg.installing);
} else {
// Existing service worker is still good.
console.log('Loading app from service worker.');
loadMainDartJs();
}
});

// If service worker doesn't succeed in a reasonable amount of time,
// fallback to plaint <script> tag.
setTimeout(() => {
if (!scriptLoaded) {
console.warn(
'Failed to load app from service worker. Falling back to plain <script> tag.',
);
loadMainDartJs();
}
}, 4000);
});
} else {
// Service workers not supported. Just drop the <script> tag.
loadMainDartJs();
}
</script>
</body>
</html>
23 changes: 23 additions & 0 deletions web/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "ttdemo",
"short_name": "ttdemo",
"start_url": ".",
"display": "standalone",
"background_color": "#0175C2",
"theme_color": "#0175C2",
"description": "Tinh tế mobile apps built with Flutter for evaluation purposes.",
"orientation": "portrait-primary",
"prefer_related_applications": false,
"icons": [
{
"src": "icons/Icon-192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "icons/Icon-512.png",
"sizes": "512x512",
"type": "image/png"
}
]
}