import 'package:flutter/material.dart';

class StationFilter extends StatefulWidget {
  const StationFilter({super.key, required this.onChanged});
  final void Function(String filterText) onChanged;

  @override
  State<StationFilter> createState() => StationFilterState();
}

class StationFilterState extends State<StationFilter> {
  final TextEditingController _filterController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return TextField(
      controller: _filterController,
      decoration: InputDecoration(
        prefixIcon: Icon(Icons.search),
        suffixIcon: Opacity(
          opacity: _filterController.text.isEmpty ? 0 : 1,
          child: IconButton(
            onPressed: () {
              _filterController.clear();

              widget.onChanged(_filterController.text);
            },
            icon: Icon(Icons.delete),
          ),
        ),
        label: Text('Filter'),
      ),
      onChanged: (_) => widget.onChanged(_filterController.text),
    );
  }
}
