Skip to content

Instantly share code, notes, and snippets.

@romanejaquez
Created March 6, 2022 19:02
Show Gist options
  • Select an option

  • Save romanejaquez/ac1eb567076588e60944b15407a1ecc3 to your computer and use it in GitHub Desktop.

Select an option

Save romanejaquez/ac1eb567076588e60944b15407a1ecc3 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: EmpData(),
);
}
}
class EmpData extends StatefulWidget {
@override
_EmpDataState createState() => _EmpDataState();
}
class _EmpDataState extends State<EmpData> {
int currentPage = 1;
List empList = [];
List filteredList = [];
//ConfigFile confObj = new ConfigFile();
//todo: get the data in a list of Strings
Future<List> getEmpData() async {
// faking making an HTTP call (since I don't have access to your backend)
return Future.delayed(const Duration(seconds: 2), () {
return [
{'emp_code': '12212',
'first_name': 'Roman',
'last_name': 'Jaquez',
'photo': 'photo_url'
},
{'emp_code': '12213',
'first_name': 'Salim',
'last_name': 'Dziri',
'photo': 'photo_url'
},
{'emp_code': '12214',
'first_name': 'John',
'last_name': 'Doe',
'photo': 'photo_url'
},
{'emp_code': '12215',
'first_name': 'Jane',
'last_name': 'Smith',
'photo': 'photo_url'
}
];
});
}
final TextEditingController editingController = TextEditingController();
ValueNotifier<String> empName = ValueNotifier('');
void changed(String query) {
filteredList = empList.where((e) => e['first_name'].toString().toLowerCase() == query.toLowerCase()).toList();
empName.value = query;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Employees List"),
centerTitle: true,
),
body: Column(
children: <Widget>[
//todo: chearch field
SizedBox(height: 20),
TextField(
controller: editingController,
onChanged: changed,
decoration: InputDecoration(
labelText: "Search",
hintText: "Search",
prefixIcon: Icon(Icons.search),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(25.0)))),
),
FutureBuilder<List>(
future: getEmpData(),
builder: (ctx,ss){
if(ss.hasData){
empList = ss.data!;
return Expanded(
child: ValueListenableBuilder<String>(
valueListenable: empName,
builder: (context, value, child) {
var listViewList = value.isEmpty ? empList : filteredList;
return ListView.builder(
itemCount: listViewList.length,
itemBuilder: (ctx,i){
return ListTile(
onTap: (){
// do whatever you want here
},
subtitle: Text("id: #${listViewList[i]['emp_code']}"),
title: Text("${listViewList[i]['first_name']} ${listViewList[i]['last_name']}"),
);
});
}));
}else{
return Center(child: CircularProgressIndicator());
}
}
)
],
)
);
}
}
@SalimDziri
Copy link

SalimDziri commented Mar 7, 2022

hello, thank you for your help, i got a problem that whenever i type anything in the search bar i got an empty list and when i delete everything the previous list return normal, so i tried to print(listViewList) and it became empty after i search anything here's what i get without searching anything :

image

i also want to search with full name (first_name + last_name) as well as emp_code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment