flutter 弹框封装【组件】

提示框自定义组件,不懂的私信问我哦~

import 'package:flutter/material.dart';

class AutoDismissAlertDialog extends StatefulWidget {
  final String message;
  final Duration duration;

  const AutoDismissAlertDialog({
    required this.message,
    this.duration = const Duration(seconds: 2),
  });

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

class _AutoDismissAlertDialogState extends State<AutoDismissAlertDialog>
    with TickerProviderStateMixin {
  late AnimationController _animationController;
  late Animation<double> _animation;

  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 300),
    );
    _animation = CurvedAnimation(
      parent: _animationController,
      curve: Curves.easeInOut,
    );
    _showDialog();
  }

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

  void _showDialog() {
    _animationController.forward();
    Future.delayed(widget.duration, _dismissDialog);
  }

  void _dismissDialog() {
    _animationController.reverse().then((_) {
      Navigator.of(context).pop();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      child: Stack(
        children: [
          GestureDetector(
            onTap: null,
            child: Container(
              color: Colors.transparent,
            ),
          ),
          Center(
            child: ScaleTransition(
              scale: _animation,
              child: Container(
                width: 280,
                height: 46,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  color: Color(0xFF000000).withOpacity(0.7),
                  borderRadius: BorderRadius.circular(4.0),
                ),
                child: Container(
                  padding: EdgeInsets.symmetric(horizontal: 12),
                  alignment: Alignment.center,
                  child: Center(
                    child:
                        Text(widget.message, style: CustomTextStyle.text14_6),
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

用法:

 showDialog(
                                  context: context,
                                  builder: (context) {
                                    return AutoDismissAlertDialog(
                                      message: user.msginfo,
                                      duration: Duration(seconds: 1),
                                    );
                                  },
                                );

弹框,弹出秒数自定义,页面中间灰色弹出框

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

flutter大佬(接私活)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值