Ejemplo n.º 1
0
        public override Widget build(BuildContext context)
        {
            MediaQueryData mediaQueryData = MediaQuery.of(context);

            D.assert(widget.animation != null);
            ThemeData         theme         = Theme.of(context);
            ColorScheme       colorScheme   = theme.colorScheme;
            SnackBarThemeData snackBarTheme = theme.snackBarTheme;
            bool isThemeDark = theme.brightness == Brightness.dark;

            Brightness brightness           = isThemeDark ? Brightness.light : Brightness.dark;
            Color      themeBackgroundColor = isThemeDark
                ? colorScheme.onSurface
                : Color.alphaBlend(colorScheme.onSurface.withOpacity(0.80f), colorScheme.surface);
            ThemeData inverseTheme = new ThemeData(
                brightness: brightness,
                backgroundColor: themeBackgroundColor,
                colorScheme: new ColorScheme(
                    primary: colorScheme.onPrimary,
                    primaryVariant: colorScheme.onPrimary,
                    secondary: isThemeDark ? colorScheme.primaryVariant : colorScheme.secondary,
                    secondaryVariant: colorScheme.onSecondary,
                    surface: colorScheme.onSurface,
                    background: themeBackgroundColor,
                    error: colorScheme.onError,
                    onPrimary: colorScheme.primary,
                    onSecondary: colorScheme.secondary,
                    onSurface: colorScheme.surface,
                    onBackground: colorScheme.background,
                    onError: colorScheme.error,
                    brightness: brightness
                    ),
                snackBarTheme: snackBarTheme
                );

            TextStyle        contentTextStyle   = snackBarTheme.contentTextStyle ?? inverseTheme.textTheme.subtitle1;
            SnackBarBehavior snackBarBehavior   = widget.behavior ?? snackBarTheme.behavior ?? SnackBarBehavior.fix;
            bool             isFloatingSnackBar = snackBarBehavior == SnackBarBehavior.floating;
            float            snackBarPadding    = isFloatingSnackBar ? 16.0f : 24.0f;

            CurvedAnimation heightAnimation =
                new CurvedAnimation(parent: widget.animation, curve: SnackBarUtils._snackBarHeightCurve);
            CurvedAnimation fadeInAnimation =
                new CurvedAnimation(parent: widget.animation, curve: SnackBarUtils._snackBarFadeInCurve);
            CurvedAnimation fadeOutAnimation = new CurvedAnimation(
                parent: widget.animation,
                curve: SnackBarUtils._snackBarFadeOutCurve,
                reverseCurve: new Threshold(0.0f)
                );

            var childrenList = new List <Widget>()
            {
                new SizedBox(width: snackBarPadding),
                new Expanded(
                    child: new Container(
                        padding: EdgeInsets.symmetric(vertical: SnackBarUtils._singleLineVerticalPadding),
                        child: new DefaultTextStyle(
                            style: contentTextStyle,
                            child: widget.content
                            )
                        )
                    )
            };

            if (widget.action != null)
            {
                childrenList.Add(new ButtonTheme(
                                     textTheme: ButtonTextTheme.accent,
                                     minWidth: 64.0f,
                                     padding: EdgeInsets.symmetric(horizontal: snackBarPadding),
                                     child: widget.action
                                     ));
            }
            else
            {
                childrenList.Add(new SizedBox(width: snackBarPadding));
            }

            Widget snackBar = new SafeArea(
                top: false,
                bottom: !isFloatingSnackBar,
                child: new Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: childrenList
                    )
                );

            float elevation       = widget.elevation ?? snackBarTheme.elevation ?? 6.0f;
            Color backgroundColor =
                widget.backgroundColor ?? snackBarTheme.backgroundColor ?? inverseTheme.backgroundColor;
            ShapeBorder shape = widget.shape
                                ?? snackBarTheme.shape
                                ?? (isFloatingSnackBar
                                    ? new RoundedRectangleBorder(borderRadius: BorderRadius.circular(4.0f))
                                    : null);

            snackBar = new Material(
                shape: shape,
                elevation: elevation,
                color: backgroundColor,
                child: new Theme(
                    data: inverseTheme,
                    child: mediaQueryData.accessibleNavigation
                        ? snackBar
                        : new FadeTransition(
                        opacity: fadeOutAnimation,
                        child: snackBar
                        )
                    )
                );

            if (isFloatingSnackBar)
            {
                snackBar = new Padding(
                    padding: EdgeInsets.fromLTRB(15.0f, 5.0f, 15.0f, 10.0f),
                    child: snackBar
                    );
            }

            snackBar = new Dismissible(
                key: Key.key("dismissible"),
                direction: DismissDirection.down,
                resizeDuration: null,
                onDismissed: (DismissDirection? direction) => {
                Scaffold.of(context).removeCurrentSnackBar(reason: SnackBarClosedReason.swipe);
            },
                child: snackBar
                );

            Widget snackBarTransition = null;

            if (mediaQueryData.accessibleNavigation)
            {
                snackBarTransition = snackBar;
            }
            else if (isFloatingSnackBar)
            {
                snackBarTransition = new FadeTransition(
                    opacity: fadeInAnimation,
                    child: snackBar
                    );
            }
            else
            {
                snackBarTransition = new AnimatedBuilder(
                    animation: heightAnimation,
                    builder: (BuildContext subContext, Widget subChild) => {
                    return(new Align(
                               alignment: AlignmentDirectional.topStart,
                               heightFactor: heightAnimation.value,
                               child: subChild
                               ));
                },
                    child: snackBar
                    );
            }

            return(new ClipRect(child: snackBarTransition));
        }
Ejemplo n.º 2
0
        public override Widget build(BuildContext context)
        {
            MediaQueryData mediaQueryData = MediaQuery.of(context);

            D.assert(this.animation != null);

            ThemeData theme     = Theme.of(context);
            ThemeData darkTheme = new ThemeData(
                brightness: Brightness.dark,
                accentColor: theme.accentColor,
                accentColorBrightness: theme.accentColorBrightness
                );

            List <Widget> children = new List <Widget> {
                new SizedBox(width: SnackBarUtils._kSnackBarPadding),
                new Expanded(
                    child: new Container(
                        padding: EdgeInsets.symmetric(vertical: SnackBarUtils._kSingleLineVerticalPadding),
                        child: new DefaultTextStyle(
                            style: darkTheme.textTheme.subhead,
                            child: this.content)
                        )
                    )
            };

            if (this.action != null)
            {
                children.Add(ButtonTheme.bar(
                                 padding: EdgeInsets.symmetric(horizontal: SnackBarUtils._kSnackBarPadding),
                                 textTheme: ButtonTextTheme.accent,
                                 child: this.action
                                 ));
            }
            else
            {
                children.Add(new SizedBox(width: SnackBarUtils._kSnackBarPadding));
            }

            CurvedAnimation heightAnimation =
                new CurvedAnimation(parent: this.animation, curve: SnackBarUtils._snackBarHeightCurve);
            CurvedAnimation fadeAnimation = new CurvedAnimation(parent: this.animation,
                                                                curve: SnackBarUtils._snackBarFadeCurve, reverseCurve: new Threshold(0.0f));
            Widget snackbar = new SafeArea(
                top: false,
                child: new Row(
                    children: children,
                    crossAxisAlignment: CrossAxisAlignment.center
                    )
                );

            snackbar = new Dismissible(
                key: Key.key("dismissible"),
                direction: DismissDirection.down,
                resizeDuration: null,
                onDismissed: (DismissDirection? direction) => {
                Scaffold.of(context).removeCurrentSnackBar(reason: SnackBarClosedReason.swipe);
            },
                child: new Material(
                    elevation: 6.0f,
                    color: this.backgroundColor ?? SnackBarUtils._kSnackBackground,
                    child: new Theme(
                        data: darkTheme,
                        child: mediaQueryData.accessibleNavigation
                            ? snackbar
                            : new FadeTransition(
                            opacity: fadeAnimation,
                            child: snackbar
                            )
                        )
                    )
                );

            return(new ClipRect(
                       child: mediaQueryData.accessibleNavigation
                    ? snackbar
                    : new AnimatedBuilder(
                           animation: heightAnimation,
                           builder: (BuildContext subContext, Widget child) => {
                return new Align(
                    alignment: Alignment.topLeft,
                    heightFactor: heightAnimation.value,
                    child: child
                    );
            },
                           child: snackbar
                           )
                       ));
        }