revanced-manager/lib/ui/widgets/shared/search_bar.dart

83 lines
2.7 KiB
Dart
Raw Normal View History

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;
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({
Key? key,
2022-08-07 21:15:52 +02:00
required this.hintText,
this.showSelectIcon = false,
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-02 12:26:37 +02:00
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(16),
color: Theme.of(context).colorScheme.secondaryContainer,
2022-08-02 12:26:37 +02:00
),
child: Row(
children: <Widget>[
2022-08-02 12:26:37 +02:00
Expanded(
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,
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,
prefixIcon: Icon(
2022-08-02 12:26:37 +02:00
Icons.search,
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('');
},
)
: 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(
borderRadius: BorderRadius.circular(12),
2022-08-02 12:26:37 +02:00
borderSide: BorderSide.none,
),
),
),
),
],
),
);
}
}