[Flutter]封装了个Toast组件

Flutter官方插件市场上已经有了很多成熟的Toast组件,如 fluttertoast 等等。

使用了一年多的Flutter框架,一时兴起,自己封装了一个简单的Toast组件。

注:本人觉得,自动关闭的时候,不宜使用 "Navigator.pop(context);" 或者 "Navigator.of(context).pop();" ,可以改用 "Navigator.of(context).removeRoute(this);" 。

包含测试的完整代码如下:

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar:  AppBar(
        title: const Text('HomePage'),
      ),
      body: Container(
        color: const Color(0xfff3f3f3),
        alignment: Alignment.center,
        child: GestureDetector(
          onTap: () {
            MyToast(msg: 'Hello world!').show(context);
          },
          child: Container(
            padding: const EdgeInsets.all(10),
            decoration: BoxDecoration(
              border: Border.all(color: Colors.black38, width: 1.0),
              borderRadius: BorderRadius.circular(12.0),
            ),
            child: const Text(
              '显示Toast',
              style: TextStyle(color: Colors.black38, fontSize: 16, height: 1),
              textAlign: TextAlign.center,
            ),
          ),
        ),
      ),
    );
  }
}


class MyToast extends PopupRoute {
  MyToast({
    this.msg = 'This is a Toast',
    this.margin,
  }) : super(settings: const RouteSettings(name: 'MyToast'));

  void show(
    BuildContext context, {
    Duration duration = const Duration(seconds: 2),
  }) {
    Navigator.push(context, this);
    //Navigator.of(context).push(this);
    Future.delayed(duration, () {
      //Navigator.pop(context);
      //Navigator.of(context).pop();
      Navigator.of(context).removeRoute(this);
    });
  }

  final String msg;
  final EdgeInsets? margin;

  @override
  Color? get barrierColor => Colors.transparent;

  @override
  bool get barrierDismissible => false;

  @override
  String? get barrierLabel => runtimeType.toString();

  @override
  Duration get transitionDuration => const Duration(milliseconds: 300);

  @override
  Widget buildPage(BuildContext context, Animation<double> animation,
      Animation<double> secondaryAnimation) {
    return Center(
      child: DefaultTextStyle(
        style: Theme.of(context).textTheme.bodyMedium!,
        child: _buildUI(),
      ),
    );
  }

  Widget _buildUI() {
    return Container(
      width: 280,
      height: 60,
      margin: margin,
      alignment: Alignment.center,
      decoration: const ShapeDecoration(
        color: Colors.black,
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(6.0))),
      ),
      padding: const EdgeInsets.all(10),
      child: Text(
        msg,
        style: const TextStyle(fontSize: 14.0, color: Colors.white),
      ),
    );
  }
}

特别鸣谢下列链接的作者:

(0)  Flutter入门:如何只关闭自身页面  。

(0)  PopupRoute class - widgets library - Dart API  。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值