2022-08-02 12:26:37 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
class SearchBar extends StatefulWidget {
|
2022-08-08 01:24:45 +02:00
|
|
|
final String? hintText;
|
2022-08-21 03:03:36 +02:00
|
|
|
final bool showSelectIcon;
|
|
|
|
final Function(bool)? onSelectAll;
|
2022-08-07 21:15:52 +02:00
|
|
|
|
2022-08-08 01:24:45 +02:00
|
|
|
const SearchBar({
|
2022-08-21 03:03:36 +02:00
|
|
|
Key? key,
|
2022-08-07 21:15:52 +02:00
|
|
|
required this.hintText,
|
2022-08-21 03:03:36 +02:00
|
|
|
this.showSelectIcon = false,
|
2022-09-05 04:32:36 +02:00
|
|
|
this.onSelectAll,
|
|
|
|
required this.onQueryChanged,
|
2022-08-02 12:26:37 +02:00
|
|
|
}) : super(key: key);
|
|
|
|
|
2022-08-02 15:11:29 +02:00
|
|
|
final Function(String) onQueryChanged;
|
|
|
|
|
2022-08-02 12:26:37 +02:00
|
|
|
@override
|
|
|
|
State<SearchBar> createState() => _SearchBarState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _SearchBarState extends State<SearchBar> {
|
2022-08-12 00:45:51 +02:00
|
|
|
final TextEditingController _textController = TextEditingController();
|
2022-08-25 11:28:40 +02:00
|
|
|
bool _toggleSelectAll = true;
|
2022-08-21 03:03:36 +02:00
|
|
|
|
2022-08-02 12:26:37 +02:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Container(
|
|
|
|
decoration: BoxDecoration(
|
2022-09-05 04:32:36 +02:00
|
|
|
borderRadius: BorderRadius.circular(16),
|
|
|
|
color: Theme.of(context).colorScheme.secondaryContainer,
|
2022-08-02 12:26:37 +02:00
|
|
|
),
|
|
|
|
child: Row(
|
2022-09-02 15:35:25 +02:00
|
|
|
children: <Widget>[
|
2022-08-02 12:26:37 +02:00
|
|
|
Expanded(
|
2022-08-21 03:03:36 +02:00
|
|
|
child: TextFormField(
|
2022-08-02 15:11:29 +02:00
|
|
|
onChanged: widget.onQueryChanged,
|
2022-08-12 00:45:51 +02:00
|
|
|
controller: _textController,
|
2022-08-02 12:26:37 +02:00
|
|
|
decoration: InputDecoration(
|
|
|
|
filled: true,
|
2022-09-05 04:32:36 +02:00
|
|
|
fillColor: Theme.of(context).colorScheme.secondaryContainer,
|
2022-08-02 12:26:37 +02:00
|
|
|
contentPadding: const EdgeInsets.all(12.0),
|
2022-08-07 21:15:52 +02:00
|
|
|
hintText: widget.hintText,
|
2022-09-01 02:25:19 +02:00
|
|
|
prefixIcon: Icon(
|
2022-08-02 12:26:37 +02:00
|
|
|
Icons.search,
|
2022-09-01 02:25:19 +02:00
|
|
|
color: Theme.of(context).iconTheme.color,
|
2022-08-02 12:26:37 +02:00
|
|
|
),
|
2022-08-12 00:45:51 +02:00
|
|
|
suffixIcon: _textController.text.isNotEmpty
|
|
|
|
? IconButton(
|
|
|
|
icon: const Icon(Icons.clear),
|
|
|
|
onPressed: () {
|
|
|
|
_textController.clear();
|
|
|
|
widget.onQueryChanged('');
|
|
|
|
},
|
|
|
|
)
|
2022-08-21 03:03:36 +02:00
|
|
|
: widget.showSelectIcon
|
|
|
|
? IconButton(
|
|
|
|
icon: _toggleSelectAll
|
|
|
|
? const Icon(Icons.deselect)
|
|
|
|
: const Icon(Icons.select_all),
|
|
|
|
onPressed: widget.onSelectAll != null
|
|
|
|
? () {
|
|
|
|
setState(() {
|
|
|
|
_toggleSelectAll = !_toggleSelectAll;
|
|
|
|
});
|
|
|
|
widget.onSelectAll!(_toggleSelectAll);
|
|
|
|
}
|
|
|
|
: () => {},
|
|
|
|
)
|
|
|
|
: null,
|
2022-08-02 12:26:37 +02:00
|
|
|
border: OutlineInputBorder(
|
2022-09-01 02:25:19 +02:00
|
|
|
borderRadius: BorderRadius.circular(12),
|
2022-08-02 12:26:37 +02:00
|
|
|
borderSide: BorderSide.none,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|