Overlay Toast In Flutter

Overlay Toast In Flutter :

overlay toast In Flutter

error_overlay.dart

import 'package:flutter/material.dart';

showErrorOverlay(BuildContext context, String text) async {
  OverlayState overlayState = Overlay.of(context);
  OverlayEntry overlayEntry =
      OverlayEntry(builder: (context) => _OverlayWidget(text));
  overlayState.insert(overlayEntry);
  Future.delayed(Duration(milliseconds: 3500)).then((_) {
    overlayEntry.remove();
  });
}

class _OverlayWidget extends StatefulWidget {
  final String text;
  const _OverlayWidget(this.text);
  @override
  _OverlayWidgetState createState() => _OverlayWidgetState();
}

class _OverlayWidgetState extends State<_OverlayWidget>
    with TickerProviderStateMixin {
  AnimationController controller;
  Animation<double> animation;

  initState() {
    super.initState();
    controller = AnimationController(
        duration: const Duration(milliseconds: 250), vsync: this);
    animation = CurvedAnimation(parent: controller, curve: Curves.easeIn);
    animation.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        Future.delayed(Duration(seconds: 4), () {
          if (mounted && controller.isCompleted) {
            controller?.reverse();
          }
        });
      }
    });
    controller.forward();
  }

  @override
  Widget build(BuildContext context) {
    return Positioned(
        top: 10 + MediaQuery.of(context).padding.top,
        left: 16,
        right: 16,
        child: FadeTransition(
            opacity: animation,
            child: GestureDetector(
                onTap: () {
                  controller.reverse();
                },
                child: Material(
                    color: Colors.transparent,
                    child: Container(
                        padding:
                            EdgeInsets.symmetric(horizontal: 32, vertical: 16),
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(4),
                            color: Colors.red),
                        child: Row(children: <Widget>[
                          Expanded(
                              child: Text(widget.text,
                                  style: TextStyle(color: Colors.white)))
                        ]))))));
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }
}

main.dart

import 'package:flutter/material.dart';

import 'error_overlay.dart';

void main() => runApp(MyApp());

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

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text(widget.title)),
        body: Center(
            child: FlatButton(
                onPressed: () {
                  showErrorOverlay(context, "This is a toast message");
                },
                child: Text('Click Here'))));
  }
}

The flutter tutorial  is a website that bring you the latest and amazing resources of code. All the languages codes are included in this website. The languages like flutter, android, java,kotlin etc.with the help of this languages any user can develop the beautiful application

For more information about Flutter. visit www.fluttertutorial.in