Design Converter
Education
Software Development Executive - I
Software Development Executive - II
Last updated on Sep 15, 2023
Last updated on Aug 22, 2023
Typing indicators aren't just fun animations in our chat applications; they serve as a communication tool within the messaging feature, enhancing our interaction with other users. The concept of a typing awareness indicator is relatively simple, and you can observe it practically in popular messaging apps such as Facebook Messenger.
Imagine a conversation you are having on Facebook Messenger. You see those tiny bubbles, right? Yes, those are the typing indicators. They aren't there just for aesthetics; they offer both a useful tool and a functional purpose.
Here's how a typing indicator works: When a user starts typing a message into the text field, an event, often referred to as a "typing event," is triggered. This typing event promptly activates the typing indicators, making them appear in your chat box.
1 // Assume this is a simplified portion of code within a 'ChatPage' Flutter code 2 if (_userIsTyping) { 3 showTypingIndicator(); 4 } 5
While simple, having this typing awareness indicator is vital at both ends of the conversation. For example, when we see someone actively typing, we anticipate their response and may decide to wait before typing our own text. It is both a useful tool for pacing the conversation and improving the user experience. That's why, in this guide, we will walk you through the process of implementing typing indicators in your Flutter chat applications.
To fully appreciate the process of designing a typing indicator in Flutter, it's vital to understand the core concepts and components: Widget, State, and Animation.
In the context of a Flutter app, everything you build is a Widget, including the typing indicator. Widgets are the building blocks of a Flutter application, and they "paint" the visuals seen by a user on a device. They essentially describe what their view should look like given their current configuration and state.
State is information that can be read synchronously when the widget gets built and might change during the lifetime of the widget. For instance, if an indicator was visible when we started typing in a text box and swapped to hidden when we stopped typing, the visibility of the indicator is the state in this case. It's this state we need to manage when implementing a typing indicator.
To create a typing indicator, we need an understanding of how animations work in Flutter. Animations add fluidity to applications and elevate user interfaces. A simple, static typing indicator could just be a piece of text that says 'Typing...'. However, with animations, we can make a lively and dynamic indicator that involves moving bubbles or dots, further enhancing the user experience.
1 // Here is a simple implementation of a TypingIndicator that utilizes Widget, State, and Animation in Flutter 2 3 class TypingIndicator extends StatefulWidget { 4 final bool isVisible; 5 6 TypingIndicator({ this.isVisible }); 7 8 @override 9 TypingIndicatorState createState() => TypingIndicatorState(); 10 } 11 12 class TypingIndicatorState extends State<TypingIndicator> with SingleTickerProviderStateMixin { 13 AnimationController controller; 14 15 @override 16 initState() { 17 super.initState(); 18 controller = AnimationController(vsync: this)... ; 19 } 20 21 @override 22 dispose() { 23 controller.dispose(); 24 super.dispose(); 25 } 26 27 @override 28 Widget build(context) { 29 return (widget.isVisible) ? AnimatedBuilder( 30 builder: ..., 31 animation: controller, 32 ) : Container(); 33 } 34 } 35
To provide an authentic real-time communication feel in our chat application, we need to create a customized typing indicator. Let's dive into the widget structure and see how we can embody the traditional mobile messaging look in our chat application.
As previously mentioned, everything in Flutter is a widget. In this step, we will define a new stateful widget named TypingIndicator which will be in charge of our typing awareness indicator. To give it the desired look and feel, we'll customize it to accept properties such as the color of the bubble, the flashing circles' light, and dark colors, and of course, a boolean to keep track of whether the indicator should show.
1 // This is the code for our TypingIndicator StatefulWidget 2 class TypingIndicator extends StatefulWidget { 3 final bool showIndicator; 4 final Color bubbleColor; 5 final Color flashingCircleDarkColor; 6 final Color flashingCircleBrightColor; 7 8 TypingIndicator({ 9 this.showIndicator = false, 10 this.bubbleColor = const Color(0xFF646b7f), 11 this.flashingCircleDarkColor = const Color(0xFF333333), 12 this.flashingCircleBrightColor = const Color(0xFFaec1dd), 13 }); 14 15 @override 16 State<TypingIndicator> createState() => _TypingIndicatorState(); 17 } 18
With the widget in place, we'll need to customize the visuals to mimic a traditional typing indicator's look and feel. Think of Apple's iPhone messaging feature, where three animated dots mimic someone typing at the other end. We will be taking a similar approach by customizing our indicators with flashing circles or 'bubbles'.
Consider using different colors for your bubbles and circles, paired with Flutter animation capabilities, and create engaging, expressive, and appealing interactions that will captivate users, relieving the stress of waiting for a response!
1 // Let's define the circle bubble as a StatelessWidget 2 class CircleBubble extends StatelessWidget { 3 final double size; 4 final Color bubbleColor; 5 6 CircleBubble({required this.size, required this.bubbleColor}); 7 8 @override 9 Widget build(BuildContext context) { 10 return Container( 11 width: size, 12 height: size, 13 decoration: BoxDecoration( 14 shape: BoxShape.circle, 15 color: bubbleColor, 16 ), 17 ); 18 } 19 } 20 21 // We can then use multiple CircleBubble to create a set of bubbles in our typing indicator. 22
When we think about space usage within a chat application for typing indicators, two things come to mind. Firstly, indicators must be noticeable and should not transfer visual discomfort to the user. Secondly, we need to consider how, when a user stops typing, the indicator disappears gracefully, reducing the space it took before.
An example of such implementation can be seen in Facebook Messenger. Deciding where to place the typing indicator and how to allocate space for it brings up intriguing dynamics in user interface discussion. The typing indicator doesn’t occupy any space when it isn’t displayed. Therefore, the indicator needs to grow in height when it appears and shrink in height when it disappears.
1 // A TextField Widget which expands as one types and shrinks as text is deleted in Flutter 2 TextField( 3 maxLines: null, 4 ) 5
Our goal is to ensure that the appearance and disappearance of the typing indicator are seamless and smooth. We can achieve this by animating the height of the typing indicator's space. The height of the typing indicator could be the natural height of the speech bubbles within the typing indicator.
1 // Implementation of an AnimatedBuilder which is displaying the indicator dynamically using SizedBox 2 3 Widget build(BuildContext context) { 4 return AnimatedBuilder( 5 animation: _indicatorSpaceAnimation, 6 builder: (context, child) { 7 return SizedBox(height: _indicatorSpaceAnimation.value,); 8 }, 9 ); 10 } 11
Having our structure ready for the typing bubble animation, let's dive into the intricate world of animation to bring our typing indicators to life. Animation is imperative to keep users engaged, and Flutter provides great tools to implement exciting animations for chat bubbles!
To make our typing indicators engaging and give them a human-like nuance of typing, we'll use staggered animations. A staggered animation includes sequential, overlapping animations. It begins when the previous animation segment concludes, creating a sequence or overlapping effect. The example of typing animations in Apple's iPhone messaging system is a perfect real-world example of staggered animations!
1 // This could represent a staggered animation in Flutter 2 final AnimationController controller = AnimationController( 3 duration: const Duration(seconds: 2), 4 vsync: this, 5 ); 6 7 // Drive a Tween, shifting the starting point to 15% and ending point to 60% of the controller's duration 8 final Animation<double> animation = Tween(begin: 0, end: 1).animate( 9 CurvedAnimation( 10 parent: controller, 11 curve: const Interval( 12 0.15, 13 0.60, 14 curve: Curves.ease, 15 ), 16 ), 17 ); 18
To implement staggered animations, we need to define separate animations for each bubble in our typing indicator. Using Flutter, we have the tools to easily animate the scale of the bubbles so that the bubbles are staggered whenever the showIndicator property changes.
In our typing indicator, the smallest bubble might start to scale up first, followed shortly after by the second bubble, and lastly, the large speech bubble.
1 // Displaying multiple CircleBubbles with different animations in the TypingIndicator 2 Stack( 3 children: [ 4 AnimatedBubble( 5 animation: _smallBubbleAnimation, 6 left: 8, 7 bottom: 8, 8 bubble: CircleBubble(size: 8, bubbleColor: widget.bubbleColor), 9 ), 10 AnimatedBubble( 11 animation: _mediumBubbleAnimation, 12 left: 10, 13 bottom: 10, 14 bubble: CircleBubble(size: 16, bubbleColor: widget.bubbleColor), 15 ), 16 AnimatedBubble( 17 animation: _largeBubbleAnimation, 18 left: 12, 19 bottom: 12, 20 bubble: StatusBubble(bubbleColor: widget.bubbleColor), 21 ), 22 ], 23 ); 24
We've animated our chat bubbles, but the bubbles alone aren't enough to denote someone actively typing. To make the typing indicator more lively and informative, we introduce an additional element - flashing circles. For instance, in Apple's iPhone messaging system, the typing indicator displays three small circles that flash repeatedly, which we'll replicate in our Flutter app.
The sequence of flashing circles not only adds an appealing visual element but also simulates the rhythm of someone actively typing. Each circle flashes at a slightly different time, giving the impression of motion and rhythm, like a wave or the bouncing ball in a song's lyrics video.
1 // A simplified representation of how to use AnimatedBuilder for flashing circles 2 AnimatedBuilder( 3 animation: animation, 4 builder: (context, child) { 5 return Transform.scale( 6 scale: animation.value, 7 alignment: Alignment.center, 8 child: child, 9 ); 10 }, 11 child: buildCircle(), 12 ); 13
To keep the circles flashing as long as the user is typing, we introduce a repeating AnimationController. This creates an indefinitely repeating animation that we can control to start when the user begins typing and to stop when the user finishes typing.
1 // Implementation of a repeating AnimationController in our TypingIndicator widget for flashing circles 2 _repeatingController = AnimationController( 3 vsync: this, 4 duration: const Duration(milliseconds: 1500), 5 ); 6 7 // Overriding the _showIndicator and _hideIndicator methods to include the repeatingController 8 void _showIndicator() { 9 _appearanceController 10 ..duration = const Duration(milliseconds: 750) 11 ..forward(); 12 _repeatingController.repeat(); 13 } 14 15 void _hideIndicator() { 16 _appearanceController 17 ..duration = const Duration(milliseconds: 150) 18 ..reverse(); 19 _repeatingController.stop(); 20 } 21
Each circle calculates its color using a sine function, so the color changes gradually at the minimum and maximum points. We animate each circle's color within a specified interval within the overall animation time.
Wrapping up, it's clear that adding a small feature like a typing indicator to your Flutter chat application can drastically boost user engagement and transform the overall user experience. Implementing this attractive and efficient feature requires a blend of Flutter's core concepts such as Widget, State, and Animation, and may also involve dealing with more advanced topics like Staggered Animations, Flashing Circles, and Stateful Widgets. Despite being a seemingly simple feature, the typing indicator itself is a powerful testament to the magic one can infuse into a Flutter application with a deep understanding and masterful use of these core features. Flutter provides us with all the tools to animate our creativity, let's keep exploring and enhancing our apps, making them more intuitive, dynamic, and delightful for users!
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.