Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flutter notify from top of the screen

Tags:

flutter

I'm trying to figure out how to notify user with alert that comes from top of the screen like normal push notification does. How can I alert user from top of the screen.
AlertDialog is not customizable so I'm stuck with this. Is there any way to show something like alert or snack bar from top of the screen?

like image 911
Daibaku Avatar asked Nov 09 '18 03:11

Daibaku


People also ask

What are notifications in flutter?

Notify Your Users | by tomerpacific | Flutter Community | Medium Notifications are an excellent way to engage your users to go back to your application or to make them to pay attention to something while in the application. There are two types of notifications:

Why do we need to listen to events in flutter?

When working with scrollable widgets in Flutter (ListView, SliverList, CustomScrollView…), there is the need to listen to the event when we reach the bottom or the top of the widget. These events can be useful when we want to update UI or execute a logic (Ex: load more data).

How to check if listview reaches the top or bottom in flutter?

Flutter – How to check if the Listview reaches the top or the bottom 1 Using ScrollController#N#void setupScrollListener ( {required ScrollController scrollController, Function? onAtTop,... 2 Using NotificationListener More ...

How to detect tap when screen is full screen?

Use GestureDetector's behavior property and pass HitTestBehavior.opaque to it, which recognizes entire screen and detects the tap when you tap anywhere on the screen. body: GestureDetector ( behavior: HitTestBehavior.opaque, onTap: () => print ('Tapped'), child: QQBody (), ), Hope this answers your question.


1 Answers

Flutter gives you the possiblity to create notifications with the help of the class Overlay. To animate these entering the screen from the top you can use the SlideTransition in combination with an AnimationController. Here is an example application I created:

enter image description here

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: Home());
  }
}

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: RaisedButton.icon(
          icon: Icon(Icons.notifications_active),
          label: Text('Notify!'),
          onPressed: () {
            Navigator.of(context)
                .overlay
                .insert(OverlayEntry(builder: (BuildContext context) {
              return FunkyNotification();
            }));
          },
        ),
      ),
    );
  }
}

class FunkyNotification extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => FunkyNotificationState();
}

class FunkyNotificationState extends State<FunkyNotification>
    with SingleTickerProviderStateMixin {
  AnimationController controller;
  Animation<Offset> position;

  @override
  void initState() {
    super.initState();

    controller =
        AnimationController(vsync: this, duration: Duration(milliseconds: 750));
    position = Tween<Offset>(begin: Offset(0.0, -4.0), end: Offset.zero)
        .animate(
            CurvedAnimation(parent: controller, curve: Curves.bounceInOut));

    controller.forward();
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Material(
        color: Colors.transparent,
        child: Align(
          alignment: Alignment.topCenter,
          child: Padding(
            padding: EdgeInsets.only(top: 32.0),
            child: SlideTransition(
              position: position,
              child: Container(
                decoration: ShapeDecoration(
                    color: Colors.deepPurple,
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(16.0))),
                child: Padding(
                  padding: EdgeInsets.all(10.0),
                  child: Text(
                    'Notification!',
                    style: TextStyle(
                        color: Colors.white, fontWeight: FontWeight.bold),
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}
like image 127
NiklasPor Avatar answered Oct 21 '22 22:10

NiklasPor