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

85 lines
2.8 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
const SearchBar({
2023-11-11 13:07:32 +01:00
super.key,
2022-08-07 21:15:52 +02:00
required this.hintText,
this.showSelectIcon = false,
this.onSelectAll,
required this.onQueryChanged,
2023-11-11 13:07:32 +01:00
});
final String? hintText;
final bool showSelectIcon;
final Function(bool)? onSelectAll;
2022-08-02 12:26:37 +02:00
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();
bool _toggleSelectAll = false;
2022-08-02 12:26:37 +02:00
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(48),
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,
style: TextStyle(
color: Theme.of(context).colorScheme.secondary,
),
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).colorScheme.secondary,
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(100),
2022-08-02 12:26:37 +02:00
borderSide: BorderSide.none,
),
),
),
),
],
),
);
}
}