Shake Text effect in Flutter UI, creatively manipulates text orientation for vivid user feedback. In various scenarios, developers want to shake the text widget, providing a visual cue that an error occurs. For instance, a wrong password input may elicit such an effect.
In this post, we will navigate through the process of integrating this effect in a Flutter app, bringing your favorite flutter text shake effect to life.
Before venturing into the shake text, let's summarize the basic text widgets. In any Flutter app, the final widget child (like Text or TextFormField) depicts what the user sees on the screen. These child Widgets can then be customized to suit your needs.
For instance:
1const Text( 2 'This is a basic Text Widget in Flutter', 3),
In another case, for a text input field, we use the TextFormField widget.
This effect creates a unique user interface experience by adding a shaking animation to the text widget when certain conditions are met. Developers often use this effect in Flutter where an error occurs, to enhance the visual cues provided to users.
For example, when users enter invalid input into a form, it helps to draw the users' attention to the error they made.
Shake TextField Flutter is a favorite widget of many that allows developers to enhance the functionality of the TextField widget by applying a shake animation effect on it. This animation gives a feedback mechanism when users make a mistake.
The shake effect can not only be applied to a text widget but also to any given child widget in a Flutter app.
Let's dive into how to integrate it in a Flutter application. We'll create a new shake effect for our Flutter Text Widget right from the ground up. Let's start with boilerplate code for a flutter widget, making sure to use helpful tools like a builder context child. Remember, always build one point at a time to avoid errors.
1class ShakeError extends StatefulWidget { 2 final Widget child; 3 4 const ShakeError({Key key, this.child}) : super(key: key); 5 6 _ShakeErrorState createState() => _ShakeErrorState(); 7} 8
The provided class shakeerror extends StatefulWidget, because the state should change upon an entrust error. The StatefulWidget calls createState() to create a separate state object.
Having set up our ShakeError widget, we now create the state for it. We do this by defining a class that extends the State object. Here, the buildcontext context is passed to the build method allowing the recreation of the widget tree.
1class _ShakeErrorState extends State<ShakeError> { 2 Animation<double> _doubleAnim; 3 AnimationController _animationController; 4 5 6 void initState() { 7 super.initState(); 8 _animationController = AnimationController( 9 duration: const Duration(milliseconds: 100), 10 vsync: this, 11 ); 12 _doubleAnim = Tween(begin: -5.0, end: 5.0).animate(_animationController) 13 ..addListener(() { 14 setState(() {}); 15 }); 16 } 17 18 19 Widget build(BuildContext context) { 20 return Transform.translate( 21 offset: Offset(_doubleAnim.value, 0), 22 child: widget.child, 23 ); 24 } 25 26 27 void dispose() { 28 _animationController.dispose(); 29 super.dispose(); 30 } 31} 32
This class extends the state of ShakeError implementing three primary methods: Build, Dispose, and the InitState. The void initstate initializes an AnimationController that uses a given duration and passes the context to the build method.
Great! Now, adding the shake animation is handled in our animation controller class within the _ShakeErrorState class. Here, we customize and define shake duration, shake distance, and much more.
With the initState() method, the AnimationController is initialized. The details of the animation, such as duration, are defined in this section. Also, the Tween animation parameters are set in this section with a begin and end value, which effectively means we want to shake our text widget side-to-side by 5 pixels.
1 2void initState() { 3 super.initState(); 4 _animationController = AnimationController( 5 duration: const Duration(milliseconds: 100), 6 vsync: this, 7 ); 8 _doubleAnim = Tween(begin: -5.0, end: 5.0).animate(_animationController) 9 ..addListener(() { 10 setState(() {}); 11 }); 12} 13
In the build method, return Transform.translate applies the shake effect to the child widget provided to our ShakeEffect widget, by translating it side-to-side.
1 2Widget build(BuildContext context) { 3 return Transform.translate( 4 offset: Offset(_doubleAnim.value, 0), 5 child: widget.child, 6 ); 7} 8
The shake effect's duration and intensity can be easily customized using the Duration and Tween values in the animation parameters of our initState() method. Adjust these values as per requirements to get the desired flutter text shake effect.
Let's make our exploration more interactive by triggering the shake animation effect when an error occurs. So instead of constantly shaking, we want our text widget only to shake when an error occurs.
1void shake() { 2 _animationController 3 .forward(from: 0) 4 .then((value) => _animationController.reverse()); 5} 6
The above void shake function starts the animation when called, shakes the widget (many times if you wish using a loop), and then reverses back to its original state. This completes our shake effect, giving us a flutter shake TextField widget ready to use in our flutter apps.
We can call the shake method in situations where a shake effect is necessary. For instance, we can use it when an incorrect or empty value is submitted in a form.
Applying the shaking text effect in a real-world context enriches user interface interactions. A practical application would be shaking the TextField widget when it's empty or an inappropriate value is passed.
Below, we have a complete TextField widget with the shake animation effect applied.
1class MyHomePage extends StatelessWidget { 2 final TextEditingController controller = TextEditingController(); 3 4 MyHomePage({Key key}) : super(key: key); 5 6 7 Widget build(BuildContext context) { 8 final _shakeKey = GlobalKey<_ShakeErrorState>(); 9 return Scaffold( 10 appBar: AppBar( 11 title: Text('Shake TextField Demo'), 12 ), 13 body: ShakeError( 14 key: _shakeKey, 15 child: Padding( 16 padding: const EdgeInsets.all(20.0), 17 child: TextField( 18 controller: controller, 19 decoration: const InputDecoration( 20 labelText: 'Enter some text', 21 ), 22 ), 23 ), 24 ), 25 floatingActionButton: FloatingActionButton( 26 onPressed: () { 27 if (controller.text.isEmpty) { 28 _shakeKey.currentState.shake(); 29 } 30 }, 31 child: Icon(Icons.text_fields), 32 ), 33 ); 34 } 35} 36
Here, class MyHomePage extends StatelessWidget enables the Stateless nature of our homepage. The build method is employed where the Scaffold widget returns the final interface. This includes an AppBar, a defined body, and a floating button that commands the shake effect to the TextField when no text is provided.
Thus, the Shaking Text Effect can be a useful tool when we want to notify users of actions needed seamlessly.
As developers, we often run into a few issues when implementing new features. Here are some common challenges while applying the shake effect and how to overcome them:
To ensure your shake effect runs smoothly, it's crucial to test the code at various stages of development. Flutter has debugging tools integrated into its framework, making this process easier.
If an error occurs, check the console logs and carefully trace each widget's state involved in the shake animation.
Here are a few tips to keep in mind while implementing a shaking text in Flutter:
In this blog, we've learned how to implement a shake text in Flutter, exploring step-by-step the transformation of a basic Text widget into a more responsive UI element. The shake effect is a fantastic way to provide visual feedback to users, making your Flutter apps more engaging and intuitive.
Now it's your turn! Pick up your developer tools and experiment with various animations in Flutter. Happy Flutterinnng!
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.