Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dart 如何在Flatter中显示自定义toast对话框?_Dart_Flutter_Flutter Layout_Flutter Animation - Fatal编程技术网

Dart 如何在Flatter中显示自定义toast对话框?

Dart 如何在Flatter中显示自定义toast对话框?,dart,flutter,flutter-layout,flutter-animation,Dart,Flutter,Flutter Layout,Flutter Animation,我想显示一个自定义的toast(我自己的小部件布局)。我知道如何显示自定义的警报对话框,但这不是我想要的 因为,警报对话: 有黑色的背景 显示时防止触摸 必须手动解除 我不想使用flattertoast库,因为我不能用它来定制布局 我想在所有其他小部件的顶部显示我自己的布局,并使其在一段时间后消失。此外,当它显示时,它不应阻止任何输入。您可以通过添加来添加和自定义您自己的祝酒词 Widget widget = Center( child: ClipRRect( bor

我想显示一个自定义的
toast
(我自己的小部件布局)。我知道如何显示自定义的
警报对话框
,但这不是我想要的

因为,
警报对话

  • 有黑色的背景
  • 显示时防止触摸
  • 必须手动解除
  • 我不想使用
    flatter
    toast库,因为我不能用它来定制布局

    我想在所有其他小部件的顶部显示我自己的布局,并使其在一段时间后消失。此外,当它显示时,它不应阻止任何输入。

    您可以通过添加来添加和自定义您自己的祝酒词

    Widget widget = Center(
          child: ClipRRect(
            borderRadius: BorderRadius.circular(30.0),
            child: Container(
              width: 40.0,
              height: 40.0,
               color: Colors.grey.withOpacity(0.3),
              child: Icon(
                Icons.add,
                size: 30.0,
                color: Colors.green,
              ),
            ),
          ),
        );
    
        ToastFuture toastFuture = showToastWidget(
          widget,
          duration: Duration(seconds: 3),
          onDismiss: () {
            print("the toast dismiss"); // the method will be called on toast dismiss.
          },
        );
    

    定制吐司


    是的。这个图书馆正是我要找的。
        import 'package:flutter/material.dart';
    import 'package:flutter/widgets.dart';
    
    class Toast {
      static void show(
        String msg,
        BuildContext context) {
        Color textColor = Colors.white;
        Color backgroundColor = Colors.blueAccent;
        dismiss();
        Toast._createView(msg, context, backgroundColor, textColor);
      }
    
      static OverlayEntry _overlayEntry;
      static bool isVisible = false;
    
      static void _createView(
        String msg,
        BuildContext context,
        Color background,
        Color textColor,
      ) async {
        var overlayState = Overlay.of(context);
    
        final themeData = Theme.of(context);
    
        _overlayEntry = new OverlayEntry(
          builder: (BuildContext context) => _ToastAnimatedWidget(
            child: Container(
              width: MediaQuery.of(context).size.width,
              child: Container(
                alignment: Alignment.center,
                width: MediaQuery.of(context).size.width,
                child: Container(
                  decoration: BoxDecoration(
                    color: background,
                    borderRadius: BorderRadius.circular(20),
                  ),
                  margin: EdgeInsets.symmetric(horizontal: 20),
                  padding: EdgeInsets.fromLTRB(16, 10, 16, 10),
                  child: Text(
                    msg,
                    softWrap: true,
                    style: themeData.textTheme.body1.copyWith(
                      fontFamily: 'intel',
                      color: Colors.white,
                    ),
                  ),
                ),
              ),
            ),
          ),
        );
        isVisible = true;
        overlayState.insert(_overlayEntry);
      }
    
      static dismiss() async {
        if (!isVisible) {
          return;
        }
        isVisible = false;
        _overlayEntry?.remove();
      }
    }
    class _ToastAnimatedWidget extends StatefulWidget {
      _ToastAnimatedWidget({
        Key key,
        @required this.child,
      }) : super(key: key);
    
      final Widget child;
    
      @override
      _ToastWidgetState createState() => _ToastWidgetState();
    }
    
    class _ToastWidgetState extends State<_ToastAnimatedWidget>
        with SingleTickerProviderStateMixin {
    
      bool get _isVisible => true; //update this value later
    
      @override
      void initState() {
        super.initState();
      }
    
      @override
      Widget build(BuildContext context) {
        return Positioned(
            bottom: 50,
            child: AnimatedOpacity(
              duration: Duration(seconds: 2),
              opacity: _isVisible ? 1.0 : 0.0,
              child: widget.child,
            )
        );
      }
    }
    
    Toast.show(ApiContent.something_wrong, context);