代码之家  ›  专栏  ›  技术社区  ›  Daibaku

从屏幕顶部发出颤振通知

  •  2
  • Daibaku  · 技术社区  · 7 年前

    我想弄清楚如何像普通的推送通知一样,用屏幕顶部发出的警报通知用户。 如何从屏幕顶部提醒用户。
    AlertDialog

    1 回复  |  直到 7 年前
        1
  •  11
  •   NiklasPor    7 年前

    Flutter提供了在类的帮助下创建通知的可能性 Overlay . 要设置这些从顶部进入屏幕的动画,可以使用 SlideTransition AnimationController . 下面是我创建的一个示例应用程序:

    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),
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ),
        );
      }
    }
    
        2
  •  0
  •   Vithani Ravi    5 年前

    在这里

    class Home extends StatefulWidget {
      @override
      _HomeState createState() => _HomeState();
    }
    
    class _HomeState extends State<Home> with TickerProviderStateMixin {
      bool _fromTop = true;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          floatingActionButton: FloatingActionButton(
            child: Icon(Icons.fireplace_outlined),
            onPressed: () {
              showGeneralDialog(
                barrierLabel: "Label",
                barrierDismissible: true,
                barrierColor: Colors.transparent,
                transitionDuration: Duration(milliseconds: 700),
                context: context,
                pageBuilder: (context, anim1, anim2) {
                  return GestureDetector(
                    onVerticalDragUpdate: (dragUpdateDetails) {
                      Navigator.of(context).pop();
                    },
                    child: Column(
                      children: [
                        SizedBox(height: 40),
                        Card(
                          margin:
                              EdgeInsets.symmetric(vertical: 20, horizontal: 10),
                          child: Container(
                            height: 100,
                            child: Image.asset('lib/model/promo.png',
                                fit: BoxFit.fill),
                            decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.circular(40),
                            ),
                          ),
                        ),
                      ],
                    ),
                  );
                },
                transitionBuilder: (context, anim1, anim2, child) {
                  return SlideTransition(
                    position: anim1.drive(Tween(
                            begin: Offset(0, _fromTop ? -1 : 1), end: Offset(0, 0))
                        .chain(CurveTween(curve: Sprung()))),
                    child: child,
                  );
                },
              );
            },
          ),
        );
      }
    }
    
    class Sprung extends Curve {
      factory Sprung([double damping = 20]) => Sprung.custom(damping: damping);
    
      Sprung.custom({
        double damping = 20,
        double stiffness = 180,
        double mass = 1.0,
        double velocity = 0.0,
      }) : this._sim = SpringSimulation(
              SpringDescription(
                damping: damping,
                mass: mass,
                stiffness: stiffness,
              ),
              0.0,
              1.0,
              velocity,
            );
    
      final SpringSimulation _sim;
    
      @override
      double transform(double t) => _sim.x(t) + t * (1 - _sim.x(1.0));
    }