Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change textfield background color on focus

I need to change textfield background color grey to white when user tap on textfiled. Focused textfield's background color should change grey to white and other un focused textfield background color stay remain grey.

i tried this

inputDecorationTheme: InputDecorationTheme(
        filled: true,
        fillColor: AppColors.textFieldBG,
        focusColor: AppColors.white,
        hintStyle: const TextStyle(color: AppColors.labelText, fontSize: 16),
        contentPadding:
            const EdgeInsets.symmetric(horizontal: 10, vertical: 20),

        focusedBorder: OutlineInputBorder(
            borderSide: const BorderSide(color: Colors.black26),
            borderRadius: BorderRadius.circular(10)),
        disabledBorder: OutlineInputBorder(
            borderSide: const BorderSide(color: Colors.transparent),
            borderRadius: BorderRadius.circular(10)),
        enabledBorder: UnderlineInputBorder(
          borderSide: const BorderSide(color: Colors.transparent),
          borderRadius: BorderRadius.circular(10),
        ),  

Here is text-filed code:

    TextField(
              focusNode: focusNode,
              onChanged: onChanged,
              controller: controller,
              maxLines: maxLines,
              onTap: onTap,
              enabled: isEnable,
              decoration: InputDecoration(
                  filled: true,
                  fillColor: AppColors.textFieldBG,
                  focusColor: AppColors.white,
                  hintText: hintText,
                  suffixIcon: isShowSuffixIcon
                      ? InkWell(
                          onTap: onTapIcon,
                          child: const Icon(
                            Icons.date_range_outlined,
                            color: AppColors.labelText,
                          ),
                        )
                      : null),
            ),
like image 899
Harsh Sureja Avatar asked Oct 14 '25 09:10

Harsh Sureja


1 Answers

You can do something like this:

FocusNode _focusNode = FocusNode();
Color _color = Colors.grey;
@override
void initState() {
  _focusNode.addListener(() {
    if(_focusNode.hasFocus){
      setState(() {
        _color = Colors.white;
      });
    }
    else{
      setState(() {
        _color = Colors.grey;
      });
    }
  });
}
TextField(
  decoration: InputDecoration(
    fillColor: _color,
    filled: true
  ),
  focusNode: _focusNode,
),
like image 145
Heck Codes Avatar answered Oct 17 '25 01:10

Heck Codes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!