top-snackbar-flutter

repository·main·Indexed 41 days ago

https://github.com/lanarsinc/top-snackbar-flutter

A Flutter package for displaying snackbar notifications at the top of the screen. It features a CustomSnackBar class with predefined .success(), .info(), and .error() constructors, support for persistent snackbars controlled via AnimationController, and a TapBounceContainer widget for tap feedback.

Tokens
2K
Snippets
7
Records
8
Agent score
50%

What's inside top-snackbar-flutter

  1. Customize iOS launch screen assets

    main

    To change the image displayed during the iOS app launch, you can either replace the image files directly in the example/ios/Runner/Assets.xcassets/LaunchImage.imageset/ directory or use Xcode for a more visual approach.

    Using Xcode:

    1. Open the iOS workspace by running open ios/Runner.xcworkspace from your terminal.
    2. In the Xcode Project Navigator, navigate to Runner/Assets.xcassets.
    3. Drag and drop your desired images into the asset catalog to replace the existing launch screen assets.
    open ios/Runner.xcworkspace
  2. How to implement a persistent snackbar

    main

    By default, snackbars are transient. To create a persistent snackbar that does not dismiss automatically, set the persistent parameter to true in the showTopSnackBar function.

    To manually control a persistent snackbar (e.g., to dismiss it via a button), use the onAnimationControllerInit callback to capture the AnimationController used by the snackbar. You can then call .reverse() on that controller to trigger the dismissal animation.

    AnimationController localAnimationController;
    
    // To show the persistent snackbar
    showTopSnackBar(
        Overlay.of(context),
        CustomSnackBar.info(
          message: "Persistent SnackBar",
        ),
        persistent: true,
        onAnimationControllerInit: (controller) =>
          localAnimationController = controller,
    );
    
    // To dismiss the persistent snackbar manually
    localAnimationController.reverse();
  3. Show a top snackbar using CustomSnackBar

    main

    To display a snackbar at the top of the screen, use the showTopSnackBar function. You must provide the Overlay from the current BuildContext and a snackbar widget. The package provides a CustomSnackBar class with factory constructors for different notification types: .success(), .info(), and .error().

    showTopSnackBar(
        Overlay.of(context),
        CustomSnackBar.success(
          message: "Good job, your release is successful. Have a nice day",
        ),
    );
  4. Show different types of CustomSnackBar

    main

    The CustomSnackBar class offers several predefined styles based on the message intent:

    • CustomSnackBar.success(message: "..."): For successful operations.
    • CustomSnackBar.info(message: "..."): For general information.
    • CustomSnackBar.error(message: "..."): For error messages or failures.
    // Success
    showTopSnackBar(Overlay.of(context), CustomSnackBar.success(message: "Success!"));
    
    // Info
    showTopSnackBar(Overlay.of(context), CustomSnackBar.info(message: "Info message"));
    
    // Error
    showTopSnackBar(Overlay.of(context), CustomSnackBar.error(message: "Error occurred"));
  5. Use TapBounceContainer for tap feedback

    main

    The TapBounceContainer is a widget that provides visual feedback when a user taps on its child. It achieves this by decreasing the scale of the child widget during a tap event.

    Properties

    • child: The widget to be wrapped and scaled.
    • onTap: An optional VoidCallback that is triggered after the tap animation completes (specifically after the onTapUp event and a delay equal to the animation duration).

    Behavior

    • On Tap Down: The widget scales down.
    • On Tap Up: The widget scales back up and then executes the onTap callback.
    • On Pan End: If a user drags away from the tap, the widget scales back up to its original size.
    TapBounceContainer(
      onTap: () {
        print('Widget tapped!');
      },
      child: ElevatedButton(
        onPressed: () {}, // Note: TapBounceContainer handles the tap detection via GestureDetector
        child: Text('Tap Me'),
      ),
    )
  6. Use CustomSnackBar for success, info, and error states

    main

    The CustomSnackBar widget provides three predefined named constructors to quickly display themed snackbar notifications: .success(), .info(), and .error(). Each constructor sets a default background color and icon appropriate for the state.

    Available Constructors

    • CustomSnackBar.success({ ... }): Uses a green background (0xff00E676) and a satisfied sentiment icon.
    • CustomSnackBar.info({ ... }): Uses a blue background (0xff2196F3) and a neutral sentiment icon.
    • CustomSnackBar.error({ ... }): Uses a red background (0xffff5252) and an error outline icon.

    Common Configuration Options

    All constructors accept the following parameters to customize the appearance:

    ParameterTypeDescription
    messageStringRequired. The text to display in the snackbar.
    backgroundColorColorThe background color of the snackbar container.
    iconWidgetThe icon widget to display (rotated by iconRotationAngle).
    textStyleTextStyleCustomizes the text appearance.
    messagePaddingEdgeInsetsGeometryPadding around the message text.
    maxLinesintMaximum number of lines for the message text.
    iconRotationAngleintRotation angle for the icon in degrees.
    iconPositionTopdoubleVertical offset for the icon position.
    iconPositionLeftdoubleHorizontal offset for the icon position.
    boxShadowList<BoxShadow>List of shadows applied to the container.
    borderRadiusBorderRadiusCorner radius of the container.
    textScaleFactordoubleScale factor for the text.
    textAlignTextAlignAlignment of the message text.
    // Example: Showing a success snackbar
    CustomSnackBar.success(
      message: 'Operation successful!',
      backgroundColor: Colors.green,
    );
    
    // Example: Showing an error snackbar with custom styling
    CustomSnackBar.error(
      message: 'Something went wrong',
      textStyle: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
      iconRotationAngle: 45,
    );
  7. Customize CustomSnackBar appearance

    main

    You can override the default styling of CustomSnackBar by passing specific values to its parameters.

    By default, the widget uses:

    • kDefaultBoxShadow: A black26 shadow with an offset of (0, 8), spread of 1, and blur of 30.
    • kDefaultBorderRadius: A circular radius of 12.

    To change the layout, you can adjust iconPositionTop, iconPositionLeft, and iconRotationAngle (which is converted from degrees to radians internally). The text is centered within the container using messagePadding and textAlign.

    const kDefaultBoxShadow = [
      BoxShadow(
        color: Colors.black26,
        offset: Offset(0, 8),
        spreadRadius: 1,
        blurRadius: 30,
      ),
    ];
    
    const kDefaultBorderRadius = BorderRadius.all(Radius.circular(12));