简体   繁体   中英

Flutter: Change Icon background color in text form field

I have this TextFormField wrapped in container for shadow effect. And input decoration for field styling. The icon background color and field background color are different.

I am not able to change the icon background color of the field.

样式化的 TextFormField

I am trying to achieve

在此处输入图像描述

Please suggest a way to change the icon background color.

Here is the code

Container(
          margin: const EdgeInsets.all(24.0),
          decoration: const BoxDecoration(
            borderRadius: BorderRadius.all(
              Radius.circular(10.0),
            ),
            boxShadow: [
              BoxShadow(
                spreadRadius: 10.0,
                blurStyle: BlurStyle.outer,
                blurRadius: 4.0,
                color: Colors.black26,
              ),
            ],
          ),
          child: Material(
            child: TextFormField(
              maxLines: 1,
              decoration: InputDecoration(
                icon: Icon(
                  Icons.search,
                  size: 24.0,
                  color: Theme.of(context).primaryColor,
                ),
                isDense: true,
                hintText: 'Search',
                contentPadding: const EdgeInsets.symmetric(vertical: 8.0),
                fillColor: Theme.of(context).scaffoldBackgroundColor,
                filled: true,
                floatingLabelBehavior: FloatingLabelBehavior.never,
                focusedBorder: InputBorder.none,
                border: InputBorder.none,
              ),
              onFieldSubmitted: (text) {
                // Perform search
              },
            ),
          ),
        )

Wrap the Icon with a Container and give it a color.

Container(
  color: Colors.red,
  child: Icon(
     Icons.search,
     size: 24.0,
     color: Theme.of(context).primaryColor,
  ),
)

Add color attribute to Material widget to change icon background color.

Container(
      margin: const EdgeInsets.all(24.0),
      decoration: const BoxDecoration(
        borderRadius: BorderRadius.all(
          Radius.circular(10.0),
        ),
        boxShadow: [
          BoxShadow(
            spreadRadius: 10.0,
            blurStyle: BlurStyle.outer,
            blurRadius: 4.0,
            color: Colors.black26,
          ),
        ],
      ),
      child: Material(
      color: Colors.transparent                //Changed here 
        child: TextFormField(
          maxLines: 1,
          decoration: InputDecoration(
            icon: Icon(
              Icons.search,
              size: 24.0,
              color: Colors.black,
            ),
            isDense: true,
            hintText: 'Search',
            contentPadding: const EdgeInsets.symmetric(vertical: 8.0),
            fillColor: Theme.of(context).scaffoldBackgroundColor,
            filled: true,
            floatingLabelBehavior: FloatingLabelBehavior.never,
            focusedBorder: InputBorder.none,
            border: InputBorder.none,
          ),
          onFieldSubmitted: (text) {
            // Perform search
          },
        ),
      ),
    ),

I add comments in the code to report where i changed.

This is the result:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM