Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize the TextFormField label animation?

How one can customize the TextFormField label animation? The InputDecoration doesn’t provide a way to set a fixed font size or any kind of different animation.

I would like to animate only from the hint location to the label without scaling down the font. Also, if I override the font color property it won’t change when the field is on focus, although this can be easily handled with a FocusNode listener.

Anyway, does anyone know an easy way to provide my own custom animation or at least, tweak it a bit, without having to recreate a custom form field all from scratch?

Thank you.

like image 347
Miguel Ruivo Avatar asked Jul 18 '19 19:07

Miguel Ruivo


People also ask

How do you change the label style in flutter?

Step 1: Locate the file where you have placed the TextField widget. Step 2: Inside the TextField widget, add the decoration parameter and assign the InputDecoration widget. Step 3: Inside the InputDecoration widget, add the labelStyle parameter and assign the TextStyle widget.

How do you add labels in flutter?

In Flutter Slider, you can add labels on the top by following the below steps. Step 1: Add Syncfusion Flutter Sliders package to your dependencies in pubspec. yaml file. Step 2: Initialize the SfSlider widget as a child of any widget.

How do you remove floating labels in flutter?

Just remove the hintText and labelText in InputDecoration . Show activity on this post. The following code removes the LabelText when you write anything in the TextField and shows LabelText when you do not write anything in the TextField. Show activity on this post.


1 Answers

Actually, you can use FocusNode state to customize floating label font size as well as floating label text color. Depending on the focus state, floating label font size can be forced to look like it's maintaining the same size as hint. Here is a quick example demonstrating this,

class MyApp extends StatefulWidget {
  @override
  MyAppState createState() => MyAppState();
}

class MyAppState extends State<MyApp> {
  FocusNode myFocusNode = new FocusNode();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            appBar: AppBar(
              title: Text('Test'),
            ),
            body: Container(
                padding: const EdgeInsets.all(20.0),
                child: TextFormField(
                  focusNode: myFocusNode,
                  decoration: new InputDecoration(
                      alignLabelWithHint: true,
                      labelStyle: TextStyle(
                          fontSize: myFocusNode.hasFocus ? 24 : 18.0,//I believe the size difference here is 6.0 to account padding
                          color:
                              myFocusNode.hasFocus ? Colors.blue : Colors.grey),
                      labelText: 'Hint text',
                      filled: true,
                      fillColor: Colors.white,
                      enabledBorder: new OutlineInputBorder(
                        borderRadius: new BorderRadius.circular(25.0),
                        borderSide: new BorderSide(
                          color: Colors.grey,
                        ),
                      ),
                      focusedBorder: new OutlineInputBorder(
                          borderRadius: new BorderRadius.circular(25.0),
                          borderSide: new BorderSide(
                            color: Colors.blue,
                          ))),
                  style: new TextStyle(color: Colors.black),
                ))));
  }
}

demo

Hope this helps.

like image 167
Pro Avatar answered Sep 18 '22 21:09

Pro