1

OutlinedInputBorder cannot accept a linear gradient for its color. I would like the border of the textFormField to be a gradient when the user clicks into it.

Here is my attempt. The border does not change.

                      GestureDetector(
                        onTap: () {
                          setState(() {
                            borderFocused = true;
                          });
                        },
                        child: Container(
                          decoration: borderFocused
                              ? const BoxDecoration(
                                  border: GradientBoxBorder(
                                      gradient: LinearGradient(
                                  colors: [
                                    Color(0xff45a7f5),
                                    Color(0xff76c479)
                                  ],
                                  begin: Alignment.topLeft,
                                  end: Alignment.bottomRight,
                                )))
                              : BoxDecoration(
                                  border: Border(
                                      bottom: BorderSide(
                                          color: Color.fromARGB(
                                              30, 192, 192, 192)))),
                          child: TextFormField(
                            cursorColor: Color.fromARGB(255, 192, 192, 192),
                            style: const TextStyle(
                                color: Color.fromARGB(255, 192, 192, 192)),
                            decoration: textInputDecoration.copyWith(
                                labelStyle: const TextStyle(
                                    color: Color.fromARGB(255, 192, 192, 192)),
                                hintText: "Email",
                                prefixIcon: Icon(
                                  Icons.email,
                                  color: Theme.of(context).primaryColor,
                                )),

1 Answers1

1

You can use ShaderMask to get you most of the way there:

Scaffold(                                                                            
  body: Center(                                                                      
    child: ShaderMask(                                                               
      shaderCallback: (bounds) {                                                     
        return LinearGradient(colors: [Colors.red,Colors.blue]).createShader(bounds);
      },                                                                             
      child: Padding(                                                                
        padding: const EdgeInsets.all(8.0),                                          
        child: TextFormField(                                                        
          cursorColor: Color.fromARGB(255, 192, 192, 192),                           
          style: const TextStyle(color: Color.fromARGB(255, 192, 192, 192)),         
          decoration: InputDecoration(                                               
            border: OutlineInputBorder(borderSide: BorderSide(                       
            )),                                                                       
              hintText: "Email",                                                     
              prefixIcon: Icon(                                                      
                Icons.email,                                                         
                color: Theme.of(context).primaryColor,                               
              )),                                                                     
        ),                                                                            
      ),                                                                              
    ),                                                                                
  ),                                                                                  
);                                                                                    

enter image description here

Code on the Rocks
  • 11,488
  • 3
  • 53
  • 61