DEV Community

Cover image for How to Create Gradient Chat Bubbles in Flutter?
Kuldeep Tarapara
Kuldeep Tarapara

Posted on • Originally published at flutteragency.com

How to Create Gradient Chat Bubbles in Flutter?

In the present scenario, many developers focus on the best way to build a professional and beautiful chat bubble in an application. If you are a programmer and want an application with a perfect platform, you can switch to flutter. Creating a gradient chat bubble in Flutter is so easy.

Flutter development companies has complete information regarding Flutter platform. Some professionals write code from scratch. On the other hand, in-built stuff for the Flutter, like transform and custompainter widget is a good option for the chat bubble.

Traditionally, chat apps show a message in a chat bubble with a solid background.

Many users want modern chat apps that show chat bubbles with a gradient.

For this concern, expertise puts effort into fulfilling user requirements by adding gradient chat bubbles in the project.
Experts use gradients depending on the bubble position on the screen.

Which challenges are recognized?

Traditional chat bubbles utilize decoratedBox or other widgets to paint a rounded rectangle in each chat message. It is suitable for a solid color and gradient repeated in every bubble.

Moreover, gradient, modern, and full-screen bubble backgrounds need a different approach. Complete screen gradient that merges with bubble scroll up and down screen involves approach. It lets developers make an informed decision for painting that suits layout information.

  • Experienced programmers have the proper knowledge and skill to create gradient chat bubbles in an application.
  • Every bubble gradient needs a bubble location on the screen.
  • Painting behavior involves access to layout information.
  • Such painting behavior is never possible with the widget due to widgets such as decoratedBox and Container widget.
  • You can make the right decision about the background color before the layout happens.
  • You can use custom painting behavior in that scenario and never go for custom layout behavior.
  • CustomPainter is an impressive option for completing a job.
  • In certain cases, you can gain complete control over child layout and never control over painting or hit testing. You may consider a flow widget. Custom RenderBox is best when you require control over painting, hit testing, and layout.

How to change original background widget?

Change widget is responsible for drawing background with a new widget like BubbleBackground. You must add color property and represent a complete screen gradient related to the bubble.

BubbleBackground(
  colors: message.isMine
      ? const [Color(0xFF6C7689), Color(0xFF3A364B)]
      : const [Color(0xFF19B7FF), Color(0xFF491CCB)],
  child: DefaultTextStyle.merge(
    style: const TextStyle(
      fontSize: 18.0,
      color: Colors.white,
    ),
    child: Padding(
      padding: const EdgeInsets.all(12.0),
      child: Text(message.text),
    ),
  ),
);
Enter fullscreen mode Exit fullscreen mode

How to build custom painter?

Developers implement BubbleBackground as a stateless widget. It is vital to define the build () method to return CustomPaint with a CustomPainter like BubblePainter. BubblePainter is ideal for painting a bubble gradient.

@immutable
class BubbleBackground extends StatelessWidget {
  const BubbleBackground({
    super.key,
    required this.colors,
    this.child,
  });
  final List<Color> colors;
  final Widget? child;
  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      painter: BubblePainter(
        colors: colors,
      ),
      child: child,
    );
  }
}
class BubblePainter extends CustomPainter {
  BubblePainter({
    required List<Color> colors,
  }) : _colors = colors;
  final List<Color> _colors;
  @override
  void paint(Canvas canvas, Size size) {
    // TODO:
  }
  @override
  bool shouldRepaint(BubblePainter oldDelegate) {
    // TODO:
    return false;
  }
}
Enter fullscreen mode Exit fullscreen mode

How to bring access to scrolling details?

CustomPainter needs information mandatory to evaluate where the bubble is within bound. The viewport is a vital asset to list view. With the help of viewport, you can determine the location and require reference to scrollable state and Bubble Background buildContext. You can get access to scrolling information to CustomPainter.

BubblePainter(
  colors: colors,
  bubbleContext: context,
  scrollable: ScrollableState(),
)
class BubblePainter extends CustomPainter {
  BubblePainter({
    required ScrollableState scrollable,
    required BuildContext bubbleContext,
    required List<Color> colors,
  })  : _scrollable = scrollable,
        _bubbleContext = bubbleContext,
        _colors = colors;
  final ScrollableState _scrollable;
  final BuildContext _bubbleContext;
  final List<Color> _colors;
  @override
  bool shouldRepaint(BubblePainter oldDelegate) {
    return oldDelegate._scrollable != _scrollable ||
        oldDelegate._bubbleContext != _bubbleContext ||
        oldDelegate._colors != _colors;
  }
}
Enter fullscreen mode Exit fullscreen mode

How to cover full-screen bubble gradient?

CustomPainter is reliable for bringing desired gradient color and reference to containing ScrollableState and bubble BuildContext. With the necessary information, CustomPainter covers a full-screen bubble gradient.

Implementing the paint() method is the best way to calculate bubble position and configure the shader with the given color. Developers may also utilize matrix translation to offset shader depending on bubble position with scrollable widget.

class BubblePainter extends CustomPainter {
  BubblePainter({
    required ScrollableState scrollable,
    required BuildContext bubbleContext,
    required List<Color> colors,
  })  : _scrollable = scrollable,
        _bubbleContext = bubbleContext,
        _colors = colors;
  final ScrollableState _scrollable;
  final BuildContext _bubbleContext;
  final List<Color> _colors;
  @override
  bool shouldRepaint(BubblePainter oldDelegate) {
    return oldDelegate._scrollable != _scrollable ||
        oldDelegate._bubbleContext != _bubbleContext ||
        oldDelegate._colors != _colors;
  }
  @override
  void paint(Canvas canvas, Size size) {
    final scrollableBox = _scrollable.context.findRenderObject() as RenderBox;
    final scrollableRect = Offset.zero & scrollableBox.size;
    final bubbleBox = _bubbleContext.findRenderObject() as RenderBox;
    final origin =
        bubbleBox.localToGlobal(Offset.zero, ancestor: scrollableBox);
    final paint = Paint()
      ..shader = ui.Gradient.linear(
        scrollableRect.topCenter,
        scrollableRect.bottomCenter,
        _colors,
        [0.0, 1.0],
        TileMode.clamp,
        Matrix4.translationValues(-origin.dx, -origin.dy, 0.0).storage,
      );
    canvas.drawRect(Offset.zero & size, paint);
  }
}
Enter fullscreen mode Exit fullscreen mode

Once you complete the above code, you can get a modern chat bubble UI. Each bubble gradient changes the user scroll due to the bubble background widget invoking scrollable.of (context). The method can set up an inherent dependency on the predecessor scrollable state.

You can note every step carefully and start and finish tasks on time. Hire Flutter developer for perfect guidance to implement necessary measures, ensure a smooth workflow and build your own Flutter mobile app.

A chat feature is available in different types of the app today, from messaging to an ecommerce mobile app development and education apps to social networks. Gradient bubbles have impressive attributes.

Developers make apps with different background colors for incoming and outgoing messages that suit quick identification. Right painting decision is also crucial for layout information and position widget.

Output:

Image description

Conclusion:

Flutter is an exciting and fantastic platform for quickly creating applications. You can hire Flutter developers from an award-winning Flutter app development company like Flutter agency who are happy to work with you!

            ##Frequently Asked Questions (FAQs)
Enter fullscreen mode Exit fullscreen mode

1. How many kinds of gradients are in Flutter?

Flutter has three types of gradient: Linear Gradient, Radial Gradient, and Sweep Gradient.

2. What are the stops in gradient Flutter?

Stops list the values from 0.0 to 1.0, denoting fractions along with their gradient. Also,non-null has a list which has a similar length as colors. If the first value is not 0.0, then a stop with the position of 0.0 and the color is equivalent to the first color in the colors is implied.

3. What is the method to make a chat bubble on Flutter?
Create the new dart file, known as custom_shape.dart, inside a lib folder. After that, build the custom shape with a custom painter class. This class is used to draw a custom shape at the end of the chat bubble.

Top comments (0)