flutter开发使用Overlay实现Toast功能

flutter使用Overlay实现Toast功能,代码如下:

void _showToast(BuildContext context) {
    OverlayState overlayState = Overlay.of(context);
    OverlayEntry overlayEntry = OverlayEntry(
      builder: (context) {
        return Positioned(
          bottom: MediaQuery.of(context).size.height * 0.2,
          child: Material(
            color: Colors.transparent,
            child: Container(
              color: Colors.transparent,
              width: MediaQuery.of(context).size.width,
              alignment: Alignment.center,
              child: Container(
                child: Text('message'),
                padding: EdgeInsets.symmetric(vertical: 8, horizontal: 10),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(20),
                  color: Colors.grey.withOpacity(0.6),
                ),
              ),
            ),
          ),
        );
      },
    );
    overlayState.insert(overlayEntry);
    Future.delayed(Duration(seconds: 2)).then((value) {
      overlayEntry.remove();
    });
  }

封装为工具类,并且项目里使用,代码如下:

  • toast_utils.dart工具类
import 'package:flutter/material.dart';

class ToastUtils {
  static BuildContext _context;
  static OverlayEntry _overlayEntry;
  static Timer _timer;

  ToastUtils._();

  static void init(BuildContext context) {
    _context = context;
  }

  static void showToast(String message, {BuildContext context}) {
    _overlayEntry?.remove();
    _timer?.cancel();
    OverlayState overlayState = Overlay.of(context ?? _context);
    _overlayEntry = OverlayEntry(
      builder: (ctx) {
        return Positioned(
          bottom: MediaQuery.of(ctx).size.height * 0.2,
          child: Material(
            color: Colors.transparent,
            child: Container(
              color: Colors.transparent,
              width: MediaQuery.of(ctx).size.width,
              alignment: Alignment.center,
              child: Container(
                child: Text(message),
                padding: EdgeInsets.symmetric(vertical: 8, horizontal: 10),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(20),
                  color: Colors.grey.withOpacity(0.6),
                ),
              ),
            ),
          ),
        );
      },
    );
    overlayState.insert(_overlayEntry);

    _timer = Timer.periodic(Duration(seconds: 2), (timer) {
      _overlayEntry?.remove();
      _overlayEntry = null;
      timer.cancel();
    });
  }
}

  • 项目里配置和使用
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: Builder(builder: (c) {
        ToastUtils.init(c);	//1. 配置全局context
        return MyHomePage(title: 'Flutter Demo Home Page');
      }),
    );
  }
}

ToastUtils.showToast('message');	//2. 配置了全局context使用时就不用传递context了。
  • 代码地址

https://github.com/yongfengnice/flutter_circle_progress

posted @   yongfengnice  阅读(838)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示