Commit 85bbe9eb by Karthik Ponnam

Docs Addded and minor updates

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