maheshj01 / searchfield

A highly customizable simple and easy to use flutter Widget to add a searchfield to your Flutter Application.This Widget allows you to search and select from list of suggestions.
MIT License
84 stars 63 forks source link

How to set the color of the selected font #169

Closed liuhll closed 1 month ago

maheshj01 commented 1 month ago

Hi @liuhll, Thanks for filing the issue and Apologies for the delayed response.

You can use the child parameter to customize the suggestions, Please see the below code sample

code sample ```dart // import 'package:example/pagination.dart'; import 'package:flutter/material.dart'; import 'package:searchfield/searchfield.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter App', theme: ThemeData( colorSchemeSeed: Colors.indigo, useMaterial3: true, brightness: Brightness.light, ), darkTheme: ThemeData( colorSchemeSeed: Colors.blue, useMaterial3: true, brightness: Brightness.dark, ), home: SearchFieldSample(), debugShowCheckedModeBanner: false, ); } } class SearchFieldSample extends StatefulWidget { const SearchFieldSample({Key? key}) : super(key: key); @override State createState() => _SearchFieldSampleState(); } class _SearchFieldSampleState extends State { @override void initState() { suggestions = [ 'United States', 'Germany', 'Canada', 'United Kingdom', 'France', 'Italy', 'Spain', 'Australia', 'India', 'China', 'Japan', 'Brazil', 'South Africa', 'Mexico', 'Argentina', 'Russia', 'Indonesia', 'Turkey', 'Saudi Arabia', 'Nigeria', 'Egypt', ]; selected = suggestions[0]; super.initState(); } final TextEditingController searchController = TextEditingController(); var suggestions = []; int counter = 0; var selected = ''; @override Widget build(BuildContext context) { Widget searchChild(x, isSelected) => Padding( padding: const EdgeInsets.symmetric(horizontal: 12), child: Text(x, style: TextStyle( fontSize: 18, color: isSelected ? Colors.green : Colors.black)), ); return Scaffold( appBar: AppBar(title: Text('Searchfield Demo')), body: Padding( padding: const EdgeInsets.all(8.0), child: SearchField( maxSuggestionsInViewPort: 10, suggestionAction: SuggestionAction.unfocus, onSuggestionTap: (SearchFieldListItem item) { setState(() { selected = item.searchKey; }); }, suggestions: suggestions .map( (e) => SearchFieldListItem(e, item: e, child: searchChild(e, e == selected)), ) .toList(), ), )); } } ```

Hope it answers your question.

https://github.com/user-attachments/assets/97704df9-1206-4ad8-90c2-ea89855ea92b

liuhll commented 1 month ago

Thank you! so much

maheshj01 commented 1 month ago

Closing as solved!