Commit a8cd67a2 by Karthik Ponnam

added web support

parent 09b85e2a
## [4.0.0]
- Added Web Support
## [3.1.3] ## [3.1.3]
- Toast optimized for Android - Toast optimized for Android
......
...@@ -6,6 +6,7 @@ Android and iOS Toast Library for Flutter ...@@ -6,6 +6,7 @@ Android and iOS Toast Library for Flutter
> >
> - Android > - Android
> - IOS > - IOS
> - Web (Uses [Toastify-JS](https://github.com/apvarun/toastify-js))
If you dont want to use androidx then use `fluttertoast` version `2.2.11` If you dont want to use androidx then use `fluttertoast` version `2.2.11`
...@@ -36,11 +37,14 @@ Fluttertoast.showToast( ...@@ -36,11 +37,14 @@ Fluttertoast.showToast(
| --------------- | ------------------------------------------------------------------ | | --------------- | ------------------------------------------------------------------ |
| msg | String (Not Null)(required) | | msg | String (Not Null)(required) |
| toastLength | Toast.LENGTH_SHORT or Toast.LENGTH_LONG (optional) | | toastLength | Toast.LENGTH_SHORT or Toast.LENGTH_LONG (optional) |
| gravity | ToastGravity.TOP (or) ToastGravity.CENTER (or) ToastGravity.BOTTOM | | gravity | ToastGravity.TOP (or) ToastGravity.CENTER (or) ToastGravity.BOTTOM (Web Only supports top, bottom) |
| timeInSecForIos | int (only for ios) | | timeInSecForIosWeb | int (only for ios) |
| bgcolor | Colors.red | | bgcolor | Colors.red |
| textcolor | Colors.white | | textcolor | Colors.white |
| fontSize | 16.0 (float) | | fontSize | 16.0 (float) |
| webShowClose | false (bool) |
| webBgColor | String (hex Color) Default(linear-gradient(to right, #00b09b, #96c93d)) |
| webPosition | String (`left`, `center` or `right`) |
### To cancel all the toasts call ### To cancel all the toasts call
......
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"fluttertoast","path":"C:\\\\Users\\\\ponna\\\\Desktop\\\\code\\\\My\\\\FlutterToast\\\\","dependencies":[]}],"android":[{"name":"fluttertoast","path":"C:\\\\Users\\\\ponna\\\\Desktop\\\\code\\\\My\\\\FlutterToast\\\\","dependencies":[]}],"macos":[],"linux":[],"windows":[],"web":[{"name":"fluttertoast","path":"C:\\\\Users\\\\ponna\\\\Desktop\\\\code\\\\My\\\\FlutterToast\\\\","dependencies":[]}]},"dependencyGraph":[{"name":"fluttertoast","dependencies":[]}],"date_created":"2020-03-21 20:38:10.570161","version":"1.15.17"}
\ No newline at end of file
gradle-wrapper.jar
/.gradle
/captures/
/gradlew
/gradlew.bat
/local.properties
GeneratedPluginRegistrant.java
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.github.ponnamkarthik.toast.example">
<!-- Flutter needs it to communicate with the running application
to allow setting breakpoints, to provide hot reload, etc.
-->
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
package io.github.ponnamkarthik.toast.example
import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() {
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.github.ponnamkarthik.toast.example">
<!-- Flutter needs it to communicate with the running application
to allow setting breakpoints, to provide hot reload, etc.
-->
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
*.mode1v3
*.mode2v3
*.moved-aside
*.pbxuser
*.perspectivev3
**/*sync/
.sconsign.dblite
.tags*
**/.vagrant/
**/DerivedData/
Icon?
**/Pods/
**/.symlinks/
profile
xcuserdata
**/.generated/
Flutter/App.framework
Flutter/Flutter.framework
Flutter/Flutter.podspec
Flutter/Generated.xcconfig
Flutter/app.flx
Flutter/app.zip
Flutter/flutter_assets/
Flutter/flutter_export_environment.sh
ServiceDefinitions.json
Runner/GeneratedPluginRegistrant.*
# Exceptions to above rules.
!default.mode1v3
!default.mode2v3
!default.pbxuser
!default.perspectivev3
#!/bin/sh #!/bin/sh
# This is a generated file; do not edit or check into version control. # This is a generated file; do not edit or check into version control.
export "FLUTTER_ROOT=/mnt/linux/SDKs/flutter" export "FLUTTER_ROOT=C:\Users\ponna\Desktop\sdks\flutter"
export "FLUTTER_APPLICATION_PATH=/mnt/linux/Programming/Flutter/FlutterToast/example" export "FLUTTER_APPLICATION_PATH=C:\Users\ponna\Desktop\code\My\FlutterToast\example"
export "FLUTTER_TARGET=lib/main.dart" export "FLUTTER_TARGET=lib\main.dart"
export "FLUTTER_BUILD_DIR=build" export "FLUTTER_BUILD_DIR=build"
export "SYMROOT=${SOURCE_ROOT}/../build/ios" export "SYMROOT=${SOURCE_ROOT}/../build\ios"
export "FLUTTER_FRAMEWORK_DIR=/mnt/linux/SDKs/flutter/bin/cache/artifacts/engine/ios" export "OTHER_LDFLAGS=$(inherited) -framework Flutter"
export "FLUTTER_FRAMEWORK_DIR=C:\Users\ponna\Desktop\sdks\flutter\bin\cache\artifacts\engine\ios"
export "FLUTTER_BUILD_NAME=1.0.0" export "FLUTTER_BUILD_NAME=1.0.0"
export "FLUTTER_BUILD_NUMBER=1" export "FLUTTER_BUILD_NUMBER=1"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>PreviewsEnabled</key>
<false/>
</dict>
</plist>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
import UIKit
import Flutter
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
#import "GeneratedPluginRegistrant.h"
//
// Generated file. Do not edit.
//
// ignore: unused_import
import 'dart:ui';
import 'package:fluttertoast/fluttertoast_web.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
void registerPlugins(PluginRegistry registry) {
FluttertoastWebPlugin.registerWith(registry.registrarFor(FluttertoastWebPlugin));
registry.registerMessageHandler();
}
...@@ -21,6 +21,16 @@ class _MyAppState extends State<MyApp> { ...@@ -21,6 +21,16 @@ class _MyAppState extends State<MyApp> {
); );
} }
void showWebColoredToast() {
Fluttertoast.showToast(
msg: "This is Colored Toast with android duration of 5 Sec",
toastLength: Toast.LENGTH_SHORT,
webBgColor: "#e74c3c",
timeInSecForIosWeb: 5,
);
}
void showColoredToast() { void showColoredToast() {
Fluttertoast.showToast( Fluttertoast.showToast(
msg: "This is Colored Toast with android duration of 5 Sec", msg: "This is Colored Toast with android duration of 5 Sec",
...@@ -33,7 +43,7 @@ class _MyAppState extends State<MyApp> { ...@@ -33,7 +43,7 @@ class _MyAppState extends State<MyApp> {
Fluttertoast.showToast( Fluttertoast.showToast(
msg: "This is Short Toast", msg: "This is Short Toast",
toastLength: Toast.LENGTH_SHORT, toastLength: Toast.LENGTH_SHORT,
timeInSecForIos: 1); timeInSecForIosWeb: 1);
} }
void showTopShortToast() { void showTopShortToast() {
...@@ -41,7 +51,7 @@ class _MyAppState extends State<MyApp> { ...@@ -41,7 +51,7 @@ class _MyAppState extends State<MyApp> {
msg: "This is Top Short Toast", msg: "This is Top Short Toast",
toastLength: Toast.LENGTH_SHORT, toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.TOP, gravity: ToastGravity.TOP,
timeInSecForIos: 1); timeInSecForIosWeb: 1);
} }
void showCenterShortToast() { void showCenterShortToast() {
...@@ -49,7 +59,7 @@ class _MyAppState extends State<MyApp> { ...@@ -49,7 +59,7 @@ class _MyAppState extends State<MyApp> {
msg: "This is Center Short Toast", msg: "This is Center Short Toast",
toastLength: Toast.LENGTH_SHORT, toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER, gravity: ToastGravity.CENTER,
timeInSecForIos: 1); timeInSecForIosWeb: 1);
} }
void cancelToast() { void cancelToast() {
...@@ -99,6 +109,12 @@ class _MyAppState extends State<MyApp> { ...@@ -99,6 +109,12 @@ class _MyAppState extends State<MyApp> {
new Padding( new Padding(
padding: const EdgeInsets.all(10.0), padding: const EdgeInsets.all(10.0),
child: new RaisedButton( child: new RaisedButton(
child: new Text('Show Web Colored Toast'),
onPressed: showWebColoredToast),
),
new Padding(
padding: const EdgeInsets.all(10.0),
child: new RaisedButton(
child: new Text('Cancel Toasts'), child: new Text('Cancel Toasts'),
onPressed: cancelToast, onPressed: cancelToast,
), ),
......
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta content="IE=Edge" http-equiv="X-UA-Compatible">
<meta name="description" content="A new Flutter project.">
<!-- 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="example">
<link rel="apple-touch-icon" href="icons/Icon-192.png">
<!-- Favicon -->
<link rel="shortcut icon" type="image/png" href="favicon.png"/>
<title>example</title>
<link rel="manifest" href="manifest.json">
</head>
<body>
<!-- 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>
if ('serviceWorker' in navigator) {
window.addEventListener('load', function () {
navigator.serviceWorker.register('flutter_service_worker.js');
});
}
</script>
<script src="main.dart.js" type="application/javascript"></script>
</body>
</html>
{
"name": "example",
"short_name": "example",
"start_url": ".",
"display": "minimal-ui",
"background_color": "#0175C2",
"theme_color": "#0175C2",
"description": "A new Flutter project.",
"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"
}
]
}
...@@ -12,20 +12,6 @@ class Fluttertoast { ...@@ -12,20 +12,6 @@ class Fluttertoast {
static const MethodChannel _channel = static const MethodChannel _channel =
const MethodChannel('PonnamKarthik/fluttertoast'); const MethodChannel('PonnamKarthik/fluttertoast');
// for Version 4.x.x
// static Fluttertoast _instance;
// static Fluttertoast get instance {
// if (_instance == null) {
// _instance =Fluttertoast._create();
// }
// return _instance;
// }
// Fluttertoast._create(){
// }
static Future<bool> cancel() async { static Future<bool> cancel() async {
bool res = await _channel.invokeMethod("cancel"); bool res = await _channel.invokeMethod("cancel");
return res; return res;
...@@ -34,11 +20,14 @@ class Fluttertoast { ...@@ -34,11 +20,14 @@ class Fluttertoast {
static Future<bool> showToast({ static Future<bool> showToast({
@required String msg, @required String msg,
Toast toastLength, Toast toastLength,
int timeInSecForIos = 1, int timeInSecForIosWeb = 1,
double fontSize = 16.0, double fontSize = 16.0,
ToastGravity gravity, ToastGravity gravity,
Color backgroundColor, Color backgroundColor,
Color textColor, Color textColor,
bool webShowClose = false,
webBgColor: "linear-gradient(to right, #00b09b, #96c93d)",
webPosition: "right"
// Function(bool) didTap, // Function(bool) didTap,
}) async { }) async {
// this.didTap = didTap; // this.didTap = didTap;
...@@ -66,11 +55,14 @@ class Fluttertoast { ...@@ -66,11 +55,14 @@ class Fluttertoast {
final Map<String, dynamic> params = <String, dynamic>{ final Map<String, dynamic> params = <String, dynamic>{
'msg': msg, 'msg': msg,
'length': toast, 'length': toast,
'time': timeInSecForIos, 'time': timeInSecForIosWeb,
'gravity': gravityToast, 'gravity': gravityToast,
'bgcolor': backgroundColor != null ? backgroundColor.value : null, 'bgcolor': backgroundColor != null ? backgroundColor.value : null,
'textcolor': textColor != null ? textColor.value : null, 'textcolor': textColor != null ? textColor.value : null,
'fontSize': fontSize, 'fontSize': fontSize,
'webShowClose': webShowClose,
'webBgColor': webBgColor,
'webPosition': webPosition
}; };
bool res = await _channel.invokeMethod('showToast', params); bool res = await _channel.invokeMethod('showToast', params);
......
import 'dart:async';
import 'dart:html' as html;
import 'dart:ui' as ui;
import 'dart:js' as js;
import 'package:flutter/services.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
class FluttertoastWebPlugin {
FluttertoastWebPlugin() {
injectCssAndJSLibraries();
}
static void registerWith(Registrar registrar) {
final MethodChannel channel =
MethodChannel('PonnamKarthik/fluttertoast', const StandardMethodCodec(), registrar.messenger);
final FluttertoastWebPlugin instance = FluttertoastWebPlugin();
channel.setMethodCallHandler(instance.handleMethodCall);
}
Future<dynamic> handleMethodCall(MethodCall call) async {
switch (call.method) {
case 'showToast':
print("showToast");
print(call.arguments);
showToast(call.arguments);
return true;
default:
throw PlatformException(
code: 'Unimplemented',
details: "The fluttertoast plugin for web doesn't implement "
"the method '${call.method}'");
}
}
Future<void> showToast(args) {
String msg = args['msg'];
String gravity = "top";
if(args['gravity'] == "top" || args['gravity'] == "bottom") {
gravity = args["gravity"];
}
String position = args['webPosition'] ?? 'right';
String bgColor = args['webBgColor'] ?? "linear-gradient(to right, #00b09b, #96c93d)";
int time = args['time'] == null ? 3000 : (int.parse(args['time'].toString()) * 1000);
bool showClose = args['webShowClose'] ?? false;
addHtmlToast(msg: msg, gravity: gravity, position: position, bgcolor: bgColor, showClose: showClose, time: time);
}
Future<void> injectCssAndJSLibraries() async {
final List<Future<void>> loading = <Future<void>>[];
final List<html.HtmlElement> tags = <html.HtmlElement>[];
final html.StyleElement css = html.StyleElement()
..id = 'toast-css'
..appendText("@import url('https://cdn.jsdelivr.net/npm/toastify-js/src/toastify.min.css');");
tags.add(css);
final html.ScriptElement script = html.ScriptElement()
..async = true
..defer = true
..src = "https://cdn.jsdelivr.net/npm/toastify-js";
loading.add(script.onLoad.first);
tags.add(script);
html.querySelector('head').children.addAll(tags);
await Future.wait(loading);
// addHtmlToast();
// addHtmlToast();
}
addHtmlToast({String msg = "", String gravity = "top", String position = "right", String bgcolor = "linear-gradient(to right, #00b09b, #96c93d)", int time = 3000, bool showClose = false}) {
print(html.querySelector("#toast-content"));
html.Element ele = html.querySelector("#toast-content");
String content = """
var toastElement = Toastify({
text: '$msg',
gravity: '$gravity',
position: '$position',
duration: $time,
close: $showClose,
backgroundColor: "$bgcolor",
});
toastElement.showToast();
""";
if(html.querySelector("#toast-content") != null) {
ele.remove();
}
final html.ScriptElement scriptText = html.ScriptElement()
..id = "toast-content"
..innerHtml = content;
html.querySelector('head').children.add(scriptText);
}
}
name: fluttertoast name: fluttertoast
description: Toast Library for Flutter, Easily create toast messages in single line of code description: Toast Library for Flutter, Easily create toast messages in single line of code
version: 3.1.3 version: 4.0.0
author: Karthik Ponnam <ponnamkarthik3@gmail.com>
homepage: https://github.com/PonnamKarthik/FlutterToast homepage: https://github.com/PonnamKarthik/FlutterToast
environment: environment:
sdk: ">=2.0.0-dev.68.0 <3.0.0" sdk: ">=2.0.0-dev.68.0 <3.0.0"
flutter: ">=1.12.8 <2.0.0"
dependencies: dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
flutter_web_plugins:
sdk: flutter
# For information on the generic Dart part of this file, see the
# following page: https://www.dartlang.org/tools/pub/pubspec
# The following section is specific to Flutter.
flutter: flutter:
plugin: plugin:
androidPackage: io.github.ponnamkarthik.toast.fluttertoast platforms:
pluginClass: FluttertoastPlugin android:
# To add assets to your plugin package, add an assets section, like this: package: io.github.ponnamkarthik.toast.fluttertoast
# assets: pluginClass: FluttertoastPlugin
# - images/a_dot_burr.jpeg ios:
# - images/a_dot_ham.jpeg pluginClass: FluttertoastPlugin
# web:
# For details regarding assets in packages, see pluginClass: FluttertoastWebPlugin
# https://flutter.io/assets-and-images/#from-packages fileName: fluttertoast_web.dart
# \ No newline at end of file
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.io/assets-and-images/#resolution-aware.
# To add custom fonts to your plugin package, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts in packages, see
# https://flutter.io/custom-fonts/#from-packages
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment