feat: fully working search bar.

This commit is contained in:
Aunali321 2022-08-02 18:41:29 +05:30
parent 15a7f9f962
commit 0b58ce3dca
3 changed files with 18 additions and 10 deletions

View File

@ -12,7 +12,7 @@ class AppSelectorScreen extends StatefulWidget {
class _AppSelectorScreenState extends State<AppSelectorScreen> { class _AppSelectorScreenState extends State<AppSelectorScreen> {
List<Application> apps = []; List<Application> apps = [];
String query = 'yout'; String query = '';
void getApps() async { void getApps() async {
apps = await DeviceApps.getInstalledApplications(); apps = await DeviceApps.getInstalledApplications();
@ -30,10 +30,16 @@ class _AppSelectorScreenState extends State<AppSelectorScreen> {
return Scaffold( return Scaffold(
body: SafeArea( body: SafeArea(
child: Padding( child: Padding(
padding: const EdgeInsets.symmetric(vertical: 4.0, horizontal: 8.0), padding: const EdgeInsets.symmetric(vertical: 4.0, horizontal: 12.0),
child: Column( child: Column(
children: [ children: [
SearchBar(), SearchBar(
onQueryChanged: (searchQuery) {
setState(() {
query = searchQuery;
});
},
),
if (query.isEmpty || query.length < 2) if (query.isEmpty || query.length < 2)
apps.isEmpty apps.isEmpty
? const Center( ? const Center(
@ -43,6 +49,8 @@ class _AppSelectorScreenState extends State<AppSelectorScreen> {
child: ListView.builder( child: ListView.builder(
itemCount: apps.length, itemCount: apps.length,
itemBuilder: (context, index) { itemBuilder: (context, index) {
//sort alphabetically
apps.sort((a, b) => a.appName.compareTo(b.appName));
return InstalledAppItem( return InstalledAppItem(
name: apps[index].appName, name: apps[index].appName,
pkgName: apps[index].packageName, pkgName: apps[index].packageName,
@ -60,6 +68,7 @@ class _AppSelectorScreenState extends State<AppSelectorScreen> {
child: ListView.builder( child: ListView.builder(
itemCount: apps.length, itemCount: apps.length,
itemBuilder: (context, index) { itemBuilder: (context, index) {
apps.sort((a, b) => a.appName.compareTo(b.appName));
if (apps[index].appName.toLowerCase().contains( if (apps[index].appName.toLowerCase().contains(
query.toLowerCase(), query.toLowerCase(),
)) { )) {

View File

@ -37,7 +37,7 @@ class PatcherScreen extends StatelessWidget {
Navigator.push( Navigator.push(
context, context,
MaterialPageRoute( MaterialPageRoute(
builder: (context) => AppSelectorScreen())); builder: (context) => const AppSelectorScreen()));
}, },
), ),
const SizedBox(height: 16), const SizedBox(height: 16),

View File

@ -2,10 +2,13 @@ import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart'; import 'package:google_fonts/google_fonts.dart';
class SearchBar extends StatefulWidget { class SearchBar extends StatefulWidget {
const SearchBar({ SearchBar({
Key? key, Key? key,
required this.onQueryChanged,
}) : super(key: key); }) : super(key: key);
final Function(String) onQueryChanged;
@override @override
State<SearchBar> createState() => _SearchBarState(); State<SearchBar> createState() => _SearchBarState();
} }
@ -14,10 +17,6 @@ class _SearchBarState extends State<SearchBar> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Container( return Container(
padding: const EdgeInsets.symmetric(
vertical: 0.0,
horizontal: 0.0,
),
decoration: BoxDecoration( decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12), borderRadius: BorderRadius.circular(12),
color: const Color(0xff1B222B), color: const Color(0xff1B222B),
@ -30,7 +29,7 @@ class _SearchBarState extends State<SearchBar> {
children: [ children: [
Expanded( Expanded(
child: TextField( child: TextField(
onSubmitted: (value) {}, onChanged: widget.onQueryChanged,
decoration: InputDecoration( decoration: InputDecoration(
fillColor: Colors.blueGrey[700], fillColor: Colors.blueGrey[700],
filled: true, filled: true,