Skip to content
New issue

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

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

Already on GitHub? # to your account

Title: Implementing a Toast Visibility Indicator in Fluttertoast #513

Open
wants to merge 2 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
69 changes: 69 additions & 0 deletions ErrorSolvedTesting.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Toast Test',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}

class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
late Timer _timer;

@override
void initState() {
super.initState();
_timer = Timer.periodic(Duration(seconds: 1), (timer) {
print("isCurrentlyShowingToast: ${Fluttertoast.isCurrentlyShowingToast}");
});
}

@override
void dispose() {
_timer.cancel();
super.dispose();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter Toast Test'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
Fluttertoast.showToast(
msg: "This is Center Short Toast",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0,
);
},
child: Text('Show Toast'),
),
),
);
}
}
16 changes: 16 additions & 0 deletions lib/fluttertoast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@ class Fluttertoast {
static const MethodChannel _channel =
const MethodChannel('PonnamKarthik/fluttertoast');

/// Boolean to track if a toast is currently being shown
static bool isCurrentlyShowingToast = false;

/// 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");
isCurrentlyShowingToast = false; // Update variable
return res;
}

Expand Down Expand Up @@ -97,7 +101,15 @@ class Fluttertoast {
'webPosition': webPosition
};

isCurrentlyShowingToast = true; // Update variable

bool? res = await _channel.invokeMethod('showToast', params);

// Assuming the platform will invoke 'cancel' method after showing toast
Future.delayed(Duration(seconds: timeInSecForIosWeb), () {
isCurrentlyShowingToast = false;
});

return res;
}
}
Expand Down Expand Up @@ -140,6 +152,7 @@ class FToast {
_showOverlay() {
if (_overlayQueue.isEmpty) {
_entry = null;
Fluttertoast.isCurrentlyShowingToast = false; // Update variable
return;
}
if (context == null) {
Expand Down Expand Up @@ -184,6 +197,8 @@ class FToast {
removeCustomToast();
});
});

Fluttertoast.isCurrentlyShowingToast = true; // Update variable
}

/// If any active toast present
Expand Down Expand Up @@ -211,6 +226,7 @@ class FToast {
_overlayQueue.clear();
_entry?.remove();
_entry = null;
Fluttertoast.isCurrentlyShowingToast = false; // Update variable
}

/// showToast accepts all the required paramenters and prepares the child
Expand Down