Commit 85bbe9eb by Karthik Ponnam

Docs Addded and minor updates

parent 5b759baa
import 'package:FlutterToast_example/toast_context.dart'; import 'package:fluttertoast_example/toast_context.dart';
import 'package:FlutterToast_example/toast_no_context.dart'; import 'package:fluttertoast_example/toast_no_context.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
GlobalKey globalKey = GlobalKey(); GlobalKey globalKey = GlobalKey();
...@@ -26,7 +26,7 @@ class _MyAppState extends State<MyApp> { ...@@ -26,7 +26,7 @@ class _MyAppState extends State<MyApp> {
body: Column( body: Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
RaisedButton( ElevatedButton(
onPressed: () { onPressed: () {
Navigator.of(context).push(MaterialPageRoute( Navigator.of(context).push(MaterialPageRoute(
builder: (context) => ToastNoContext(), builder: (context) => ToastNoContext(),
...@@ -37,7 +37,7 @@ class _MyAppState extends State<MyApp> { ...@@ -37,7 +37,7 @@ class _MyAppState extends State<MyApp> {
SizedBox( SizedBox(
height: 24.0, height: 24.0,
), ),
RaisedButton( ElevatedButton(
onPressed: () { onPressed: () {
Navigator.of(context).push(MaterialPageRoute( Navigator.of(context).push(MaterialPageRoute(
builder: (context) => ToastContext(), builder: (context) => ToastContext(),
......
import 'package:FlutterToast_example/main.dart'; import 'package:fluttertoast_example/main.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart'; import 'package:fluttertoast/fluttertoast.dart';
...@@ -142,13 +142,13 @@ class _ToastContextState extends State<ToastContext> { ...@@ -142,13 +142,13 @@ class _ToastContextState extends State<ToastContext> {
SizedBox( SizedBox(
height: 24.0, height: 24.0,
), ),
RaisedButton( ElevatedButton(
child: Text("Show Custom Toast"), child: Text("Show Custom Toast"),
onPressed: () { onPressed: () {
_showToast(); _showToast();
}, },
), ),
RaisedButton( ElevatedButton(
child: Text("Show Custom Toast via PositionedToastBuilder"), child: Text("Show Custom Toast via PositionedToastBuilder"),
onPressed: () { onPressed: () {
_showBuilderToast(); _showBuilderToast();
...@@ -157,7 +157,7 @@ class _ToastContextState extends State<ToastContext> { ...@@ -157,7 +157,7 @@ class _ToastContextState extends State<ToastContext> {
SizedBox( SizedBox(
height: 24.0, height: 24.0,
), ),
RaisedButton( ElevatedButton(
child: Text("Custom Toast With Close Button"), child: Text("Custom Toast With Close Button"),
onPressed: () { onPressed: () {
_showToastCancel(); _showToastCancel();
...@@ -166,7 +166,7 @@ class _ToastContextState extends State<ToastContext> { ...@@ -166,7 +166,7 @@ class _ToastContextState extends State<ToastContext> {
SizedBox( SizedBox(
height: 24.0, height: 24.0,
), ),
RaisedButton( ElevatedButton(
child: Text("Queue Toasts"), child: Text("Queue Toasts"),
onPressed: () { onPressed: () {
_queueToasts(); _queueToasts();
...@@ -175,7 +175,7 @@ class _ToastContextState extends State<ToastContext> { ...@@ -175,7 +175,7 @@ class _ToastContextState extends State<ToastContext> {
SizedBox( SizedBox(
height: 24.0, height: 24.0,
), ),
RaisedButton( ElevatedButton(
child: Text("Cancel Toast"), child: Text("Cancel Toast"),
onPressed: () { onPressed: () {
_removeToast(); _removeToast();
...@@ -184,7 +184,7 @@ class _ToastContextState extends State<ToastContext> { ...@@ -184,7 +184,7 @@ class _ToastContextState extends State<ToastContext> {
SizedBox( SizedBox(
height: 24.0, height: 24.0,
), ),
RaisedButton( ElevatedButton(
child: Text("Remove Queued Toasts"), child: Text("Remove Queued Toasts"),
onPressed: () { onPressed: () {
_removeAllQueuedToasts(); _removeAllQueuedToasts();
......
...@@ -67,43 +67,43 @@ class ToastNoContext extends StatelessWidget { ...@@ -67,43 +67,43 @@ class ToastNoContext extends StatelessWidget {
children: <Widget>[ children: <Widget>[
new Padding( new Padding(
padding: const EdgeInsets.all(10.0), padding: const EdgeInsets.all(10.0),
child: new RaisedButton( child: new ElevatedButton(
child: new Text('Show Long Toast'), child: new Text('Show Long Toast'),
onPressed: showLongToast), onPressed: showLongToast),
), ),
new Padding( new Padding(
padding: const EdgeInsets.all(10.0), padding: const EdgeInsets.all(10.0),
child: new RaisedButton( child: new ElevatedButton(
child: new Text('Show Short Toast'), child: new Text('Show Short Toast'),
onPressed: showShortToast), onPressed: showShortToast),
), ),
new Padding( new Padding(
padding: const EdgeInsets.all(10.0), padding: const EdgeInsets.all(10.0),
child: new RaisedButton( child: new ElevatedButton(
child: new Text('Show Center Short Toast'), child: new Text('Show Center Short Toast'),
onPressed: showCenterShortToast), onPressed: showCenterShortToast),
), ),
new Padding( new Padding(
padding: const EdgeInsets.all(10.0), padding: const EdgeInsets.all(10.0),
child: new RaisedButton( child: new ElevatedButton(
child: new Text('Show Top Short Toast'), child: new Text('Show Top Short Toast'),
onPressed: showTopShortToast), onPressed: showTopShortToast),
), ),
new Padding( new Padding(
padding: const EdgeInsets.all(10.0), padding: const EdgeInsets.all(10.0),
child: new RaisedButton( child: new ElevatedButton(
child: new Text('Show Colored Toast'), child: new Text('Show Colored Toast'),
onPressed: showColoredToast), onPressed: showColoredToast),
), ),
new Padding( new Padding(
padding: const EdgeInsets.all(10.0), padding: const EdgeInsets.all(10.0),
child: new RaisedButton( child: new ElevatedButton(
child: new Text('Show Web Colored Toast'), child: new Text('Show Web Colored Toast'),
onPressed: showWebColoredToast), onPressed: showWebColoredToast),
), ),
new Padding( new Padding(
padding: const EdgeInsets.all(10.0), padding: const EdgeInsets.all(10.0),
child: new RaisedButton( child: new ElevatedButton(
child: new Text('Cancel Toasts'), child: new Text('Cancel Toasts'),
onPressed: cancelToast, onPressed: cancelToast,
), ),
......
...@@ -78,7 +78,7 @@ packages: ...@@ -78,7 +78,7 @@ packages:
path: ".." path: ".."
relative: true relative: true
source: path source: path
version: "7.1.8" version: "8.0.2"
js: js:
dependency: transitive dependency: transitive
description: description:
......
name: FlutterToast_example name: fluttertoast_example
description: Demonstrates how to use the FlutterToast plugin. description: Demonstrates how to use the FlutterToast plugin.
# The following line prevents the package from being accidentally published to # The following line prevents the package from being accidentally published to
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:FlutterToast_example/main.dart'; import 'package:fluttertoast_example/main.dart';
void main() { void main() {
testWidgets('Verify Platform version', (WidgetTester tester) async { testWidgets('Verify Platform version', (WidgetTester tester) async {
......
...@@ -3,8 +3,18 @@ import 'package:flutter/material.dart'; ...@@ -3,8 +3,18 @@ import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
enum Toast { LENGTH_SHORT, LENGTH_LONG } /// Toast Length
/// Only for Android Platform
enum Toast {
/// Show Short toast for 1 sec
LENGTH_SHORT,
/// Show Long toast for 5 sec
LENGTH_LONG
}
/// ToastGravity
/// Used to define the position of the Toast on the screen
enum ToastGravity { enum ToastGravity {
TOP, TOP,
BOTTOM, BOTTOM,
...@@ -18,15 +28,27 @@ enum ToastGravity { ...@@ -18,15 +28,27 @@ enum ToastGravity {
SNACKBAR SNACKBAR
} }
/// Plugin to show a toast message on screen
/// Only for android, ios and Web platforms
class Fluttertoast { class Fluttertoast {
/// [MethodChannel] used to communicate with the platform side.
static const MethodChannel _channel = static const MethodChannel _channel =
const MethodChannel('PonnamKarthik/fluttertoast'); const MethodChannel('PonnamKarthik/fluttertoast');
/// Let say you have an active show
/// Use this method to hide the toast immediately
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;
} }
/// Summons the platform's showToast which will display the message
///
/// Wraps the platform's native Toast for android.
/// Wraps the Plugin https://github.com/scalessec/Toast for iOS
/// Wraps the https://github.com/apvarun/toastify-js for Web
///
/// Parameter [msg] is required and remning all are options
static Future<bool> showToast({ static Future<bool> showToast({
@required String msg, @required String msg,
Toast toastLength, Toast toastLength,
...@@ -38,9 +60,7 @@ class Fluttertoast { ...@@ -38,9 +60,7 @@ class Fluttertoast {
bool webShowClose = false, bool webShowClose = false,
webBgColor: "linear-gradient(to right, #00b09b, #96c93d)", webBgColor: "linear-gradient(to right, #00b09b, #96c93d)",
webPosition: "right", webPosition: "right",
// Function(bool) didTap,
}) async { }) async {
// this.didTap = didTap;
String toast = "short"; String toast = "short";
if (toastLength == Toast.LENGTH_LONG) { if (toastLength == Toast.LENGTH_LONG) {
toast = "long"; toast = "long";
...@@ -80,18 +100,26 @@ class Fluttertoast { ...@@ -80,18 +100,26 @@ class Fluttertoast {
} }
} }
/// Signature for a function to buildCustom Toast
typedef PositionedToastBuilder = Widget Function( typedef PositionedToastBuilder = Widget Function(
BuildContext context, Widget child); BuildContext context, Widget child);
/// Runs on dart side this has no interaction with the Native Side
/// Works with all platforms just in two lines of code
/// final fToast = FToast().init(context)
/// fToast.showToast(child)
///
class FToast { class FToast {
BuildContext context; BuildContext context;
static final FToast _instance = FToast._internal(); static final FToast _instance = FToast._internal();
/// Prmary Constructor for FToast
factory FToast() { factory FToast() {
return _instance; return _instance;
} }
/// Take users Context and saves to avariable
init(BuildContext context) { init(BuildContext context) {
_instance.context = context; _instance.context = context;
} }
...@@ -99,9 +127,12 @@ class FToast { ...@@ -99,9 +127,12 @@ class FToast {
FToast._internal(); FToast._internal();
OverlayEntry _entry; OverlayEntry _entry;
List<_ToastEntry> _overlayQueue = List(); List<_ToastEntry> _overlayQueue = [];
Timer _timer; Timer _timer;
/// Internal function which handles the adding
/// the overlay to the screen
///
_showOverlay() { _showOverlay() {
if (_overlayQueue.length == 0) { if (_overlayQueue.length == 0) {
_entry = null; _entry = null;
...@@ -120,6 +151,8 @@ class FToast { ...@@ -120,6 +151,8 @@ class FToast {
}); });
} }
/// If any active toast present
/// call removeCustomToast to hide the toast immediately
removeCustomToast() { removeCustomToast() {
_timer?.cancel(); _timer?.cancel();
_timer = null; _timer = null;
...@@ -127,6 +160,11 @@ class FToast { ...@@ -127,6 +160,11 @@ class FToast {
_showOverlay(); _showOverlay();
} }
/// FToast maintains a queue for every toast
/// if we called showToast for 3 times we all to queue
/// and show them one after another
///
/// call removeCustomToast to hide the toast immediately
removeQueuedCustomToasts() { removeQueuedCustomToasts() {
_timer?.cancel(); _timer?.cancel();
_timer = null; _timer = null;
...@@ -135,6 +173,11 @@ class FToast { ...@@ -135,6 +173,11 @@ class FToast {
_entry = null; _entry = null;
} }
/// showToast accepts all the required paramenters and prepares the child
/// calls _showOverlay to display toast
///
/// Paramenter [child] is requried
///
void showToast({ void showToast({
@required Widget child, @required Widget child,
PositionedToastBuilder positionedToastBuilder, PositionedToastBuilder positionedToastBuilder,
...@@ -155,6 +198,9 @@ class FToast { ...@@ -155,6 +198,9 @@ class FToast {
if (_timer == null) _showOverlay(); if (_timer == null) _showOverlay();
} }
/// _getPostionWidgetBasedOnGravity generates [Positioned] [Widget]
/// based on the gravity [ToastGravity] provided by the user in
/// [showToast]
_getPostionWidgetBasedOnGravity(Widget child, ToastGravity gravity) { _getPostionWidgetBasedOnGravity(Widget child, ToastGravity gravity) {
switch (gravity) { switch (gravity) {
case ToastGravity.TOP: case ToastGravity.TOP:
...@@ -196,6 +242,9 @@ class FToast { ...@@ -196,6 +242,9 @@ class FToast {
} }
} }
/// internal class [_ToastEntry] which maintains
/// each [OverlayEntry] and [Duration] for every toast user
/// triggered
class _ToastEntry { class _ToastEntry {
final OverlayEntry entry; final OverlayEntry entry;
final Duration duration; final Duration duration;
...@@ -203,6 +252,8 @@ class _ToastEntry { ...@@ -203,6 +252,8 @@ class _ToastEntry {
_ToastEntry({this.entry, this.duration}); _ToastEntry({this.entry, this.duration});
} }
/// internal [StatefulWidget] which handles the show and hide
/// animations for [FToast]
class _ToastStateFul extends StatefulWidget { class _ToastStateFul extends StatefulWidget {
_ToastStateFul(this.child, this.duration, {Key key}) : super(key: key); _ToastStateFul(this.child, this.duration, {Key key}) : super(key: key);
...@@ -213,17 +264,21 @@ class _ToastStateFul extends StatefulWidget { ...@@ -213,17 +264,21 @@ class _ToastStateFul extends StatefulWidget {
ToastStateFulState createState() => ToastStateFulState(); ToastStateFulState createState() => ToastStateFulState();
} }
/// State for [_ToastStateFul]
class ToastStateFulState extends State<_ToastStateFul> class ToastStateFulState extends State<_ToastStateFul>
with SingleTickerProviderStateMixin { with SingleTickerProviderStateMixin {
/// Start the showing animations for the toast
showIt() { showIt() {
_animationController.forward(); _animationController.forward();
} }
/// Start the hidding animations for the toast
hideIt() { hideIt() {
_animationController.reverse(); _animationController.reverse();
_timer?.cancel(); _timer?.cancel();
} }
/// Controller to start and hide the animation
AnimationController _animationController; AnimationController _animationController;
Animation _fadeAnimation; Animation _fadeAnimation;
......
...@@ -3,17 +3,23 @@ import 'dart:html' as html; ...@@ -3,17 +3,23 @@ import 'dart:html' as html;
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart'; import 'package:flutter_web_plugins/flutter_web_plugins.dart';
/// Plugin Class to show a toast message on screen for web
class FluttertoastWebPlugin { class FluttertoastWebPlugin {
/// Constructor class
/// which calls the metohd to inject JS and CSS in to dom
FluttertoastWebPlugin() { FluttertoastWebPlugin() {
injectCssAndJSLibraries(); injectCssAndJSLibraries();
} }
/// Registers [MethodChannel] used to communicate with the platform side.
static void registerWith(Registrar registrar) { static void registerWith(Registrar registrar) {
final MethodChannel channel = MethodChannel('PonnamKarthik/fluttertoast', const StandardMethodCodec(), registrar.messenger); final MethodChannel channel = MethodChannel(
'PonnamKarthik/fluttertoast', const StandardMethodCodec(), registrar);
final FluttertoastWebPlugin instance = FluttertoastWebPlugin(); final FluttertoastWebPlugin instance = FluttertoastWebPlugin();
channel.setMethodCallHandler(instance.handleMethodCall); channel.setMethodCallHandler(instance.handleMethodCall);
} }
/// Handle Method Callbacks from [MethodChannel].
Future<dynamic> handleMethodCall(MethodCall call) async { Future<dynamic> handleMethodCall(MethodCall call) async {
switch (call.method) { switch (call.method) {
case 'showToast': case 'showToast':
...@@ -27,6 +33,8 @@ class FluttertoastWebPlugin { ...@@ -27,6 +33,8 @@ class FluttertoastWebPlugin {
} }
} }
/// showToast which parses the required arguments and pass
/// it to [addHtmlToast]
showToast(args) { showToast(args) {
String msg = args['msg']; String msg = args['msg'];
String gravity = "top"; String gravity = "top";
...@@ -36,26 +44,35 @@ class FluttertoastWebPlugin { ...@@ -36,26 +44,35 @@ class FluttertoastWebPlugin {
String position = args['webPosition'] ?? 'right'; String position = args['webPosition'] ?? 'right';
String bgColor = args['webBgColor'] ?? "linear-gradient(to right, #00b09b, #96c93d)"; String bgColor =
args['webBgColor'] ?? "linear-gradient(to right, #00b09b, #96c93d)";
int textColor = args['textcolor']; int textColor = args['textcolor'];
int time = args['time'] == null ? 3000 : (int.parse(args['time'].toString()) * 1000); int time = args['time'] == null
? 3000
: (int.parse(args['time'].toString()) * 1000);
bool showClose = args['webShowClose'] ?? false; bool showClose = args['webShowClose'] ?? false;
addHtmlToast(msg: msg, gravity: gravity, position: position, bgcolor: bgColor, showClose: showClose, time: time, textColor: textColor); addHtmlToast(
msg: msg,
gravity: gravity,
position: position,
bgcolor: bgColor,
showClose: showClose,
time: time,
textColor: textColor);
} }
/// [injectCssAndJSLibraries] which add the JS and CSS files into DOM
Future<void> injectCssAndJSLibraries() async { Future<void> injectCssAndJSLibraries() async {
final List<Future<void>> loading = <Future<void>>[]; final List<Future<void>> loading = <Future<void>>[];
final List<html.HtmlElement> tags = <html.HtmlElement>[]; final List<html.HtmlElement> tags = <html.HtmlElement>[];
final html.LinkElement css = html.LinkElement() final html.LinkElement css = html.LinkElement()
..id = 'toast-css' ..id = 'toast-css'
..attributes = { ..attributes = {"rel": "stylesheet"}
"rel": "stylesheet"
}
..href = 'assets/packages/fluttertoast/assets/toastify.css'; ..href = 'assets/packages/fluttertoast/assets/toastify.css';
tags.add(css); tags.add(css);
...@@ -70,6 +87,8 @@ class FluttertoastWebPlugin { ...@@ -70,6 +87,8 @@ class FluttertoastWebPlugin {
await Future.wait(loading); await Future.wait(loading);
} }
/// injects Final [Toastify] code with all the parameters to
/// make toast visible on web
addHtmlToast( addHtmlToast(
{String msg = "", {String msg = "",
String gravity = "top", String gravity = "top",
......
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: 7.1.8 version: 8.0.2
homepage: https://github.com/PonnamKarthik/FlutterToast homepage: https://github.com/PonnamKarthik/FlutterToast
issue_tracker: https://github.com/ponnamkarthik/FlutterToast/issues issue_tracker: https://github.com/ponnamkarthik/FlutterToast/issues
......
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