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

107 lines
3.4 KiB
Dart
Raw Normal View History

2022-08-02 12:26:37 +02:00
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
class SearchBar extends StatefulWidget {
2022-08-08 01:24:45 +02:00
final String? hintText;
2022-08-11 22:17:10 +02:00
final Color? fillColor;
final bool showSelectIcon;
final Function(bool)? onSelectAll;
final Color? backgroundColor;
2022-08-08 01:24:45 +02:00
final Color? hintTextColor;
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,
2022-08-11 22:17:10 +02:00
required this.fillColor,
2022-08-02 15:11:29 +02:00
required this.onQueryChanged,
this.onSelectAll,
this.showSelectIcon = false,
this.backgroundColor = const Color(0xff1B222B),
this.hintTextColor = Colors.white,
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(12),
2022-08-07 21:15:52 +02:00
color: widget.backgroundColor,
2022-08-02 12:26:37 +02:00
border: Border.all(
2022-08-08 01:24:45 +02:00
color: widget.backgroundColor != null
? widget.backgroundColor!
: Colors.white,
2022-08-02 12:26:37 +02:00
width: 1,
),
),
child: Row(
children: [
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(
2022-08-11 22:17:10 +02:00
fillColor: widget.fillColor,
2022-08-02 12:26:37 +02:00
filled: true,
contentPadding: const EdgeInsets.all(12.0),
2022-08-07 21:15:52 +02:00
hintText: widget.hintText,
2022-08-02 12:26:37 +02:00
hintStyle: GoogleFonts.poppins(
2022-08-07 21:15:52 +02:00
color: widget.hintTextColor,
2022-08-02 12:26:37 +02:00
fontWeight: FontWeight.w400,
),
prefixIcon: const Icon(
Icons.search,
size: 24.0,
),
2022-08-12 00:45:51 +02:00
suffixIcon: _textController.text.isNotEmpty
? IconButton(
icon: const Icon(Icons.clear),
iconSize: 24.0,
onPressed: () {
_textController.clear();
widget.onQueryChanged('');
},
)
: widget.showSelectIcon
? IconButton(
icon: _toggleSelectAll
? const Icon(Icons.deselect)
: const Icon(Icons.select_all),
iconSize: 24.0,
onPressed: widget.onSelectAll != null
? () {
setState(() {
_toggleSelectAll = !_toggleSelectAll;
});
widget.onSelectAll!(_toggleSelectAll);
}
: () => {},
)
: null,
2022-08-02 12:26:37 +02:00
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
borderSide: BorderSide.none,
),
),
style: GoogleFonts.poppins(
color: Colors.white,
fontWeight: FontWeight.w400,
fontSize: 16,
),
),
),
],
),
);
}
}