Encrypted messaging app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
4.6 KiB

import 'package:flutter/material.dart';
import '/models/friends.dart';
import '/views/main/friend_list_item.dart';
class FriendList extends StatefulWidget {
final List<Friend> friends;
const FriendList({
Key? key,
required this.friends,
}) : super(key: key);
@override
State<FriendList> createState() => _FriendListState();
}
class _FriendListState extends State<FriendList> {
List<Friend> friends = [];
List<Friend> friendsDuplicate = [];
@override
void initState() {
super.initState();
friends.addAll(widget.friends);
setState(() {});
}
void filterSearchResults(String query) {
List<Friend> dummySearchList = [];
dummySearchList.addAll(widget.friends);
if(query.isNotEmpty) {
List<Friend> dummyListData = [];
dummySearchList.forEach((item) {
if(item.username.toLowerCase().contains(query)) {
dummyListData.add(item);
}
});
setState(() {
friends.clear();
friends.addAll(dummyListData);
});
return;
}
setState(() {
friends.clear();
friends.addAll(widget.friends);
});
}
Widget list() {
if (friends.isEmpty) {
return const Center(
child: Text('No Friends'),
);
}
return ListView.builder(
itemCount: friends.length,
shrinkWrap: true,
padding: const EdgeInsets.only(top: 16),
physics: const NeverScrollableScrollPhysics(),
itemBuilder: (context, i) {
return FriendListItem(
friend: friends[i],
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
physics: const BouncingScrollPhysics(),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SafeArea(
child: Padding(
padding: const EdgeInsets.only(left: 16,right: 16,top: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
const Text("Friends",style: TextStyle(fontSize: 32,fontWeight: FontWeight.bold),),
Container(
padding: const EdgeInsets.only(left: 8,right: 8,top: 2,bottom: 2),
height: 30,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Theme.of(context).colorScheme.tertiary
),
child: Row(
children: <Widget>[
Icon(
Icons.add,
color: Theme.of(context).primaryColor,
size: 20
),
const SizedBox(width: 2,),
const Text(
"Add",
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold
)
),
],
),
)
],
),
),
),
Padding(
padding: const EdgeInsets.only(top: 16,left: 16,right: 16),
child: TextField(
decoration: const InputDecoration(
hintText: "Search...",
prefixIcon: Icon(
Icons.search,
size: 20
),
),
onChanged: (value) => filterSearchResults(value.toLowerCase())
),
),
Padding(
padding: const EdgeInsets.only(top: 16,left: 16,right: 16),
child: list(),
),
],
),
),
);
}
}