InputDecorator

sample.packages.flutter.lib.src.material.input_decorator.2389.

This sample shows how to style a TextField with a prefixIcon that changes color based on the MaterialState. The color defaults to gray, be blue while focused and red if in an error state.

  
import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: const Text(_title)),
        body: const MyStatelessWidget(),
      ),
    );
  }
}

class MyStatelessWidget extends StatelessWidget {
  const MyStatelessWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return TextFormField(
      initialValue: 'abc',
      decoration: InputDecoration(
        prefixIcon: const Icon(Icons.person),
        prefixIconColor: MaterialStateColor.resolveWith((Set states) {
          if (states.contains(MaterialState.focused)) {
            return Colors.green;
          } if (states.contains(MaterialState.error)) {
            return Colors.red;
          }
          return Colors.grey;
        }),
      ),
    );
  }
}
  

SHARE: