Add search mode

This commit is contained in:
Christian Pauly 2020-01-02 22:55:12 +01:00
parent de29800f29
commit 874374d266

View file

@ -35,7 +35,9 @@ class ChatList extends StatefulWidget {
} }
class _ChatListState extends State<ChatList> { class _ChatListState extends State<ChatList> {
bool searchMode = false;
StreamSubscription sub; StreamSubscription sub;
final TextEditingController searchController = TextEditingController();
Future<bool> waitForFirstSync(BuildContext context) async { Future<bool> waitForFirstSync(BuildContext context) async {
Client client = Matrix.of(context).client; Client client = Matrix.of(context).client;
@ -46,9 +48,20 @@ class _ChatListState extends State<ChatList> {
return true; return true;
} }
@override
void initState() {
searchController.addListener(
() => setState(() => null),
);
super.initState();
}
@override @override
void dispose() { void dispose() {
sub?.cancel(); sub?.cancel();
searchController.removeListener(
() => setState(() => null),
);
super.dispose(); super.dispose();
} }
@ -56,13 +69,31 @@ class _ChatListState extends State<ChatList> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: Text( title: searchMode
"Conversations", ? TextField(
autofocus: true,
autocorrect: false,
controller: searchController,
decoration: InputDecoration(
border: InputBorder.none,
hintText: "Search for a chat",
), ),
actions: <Widget>[ )
: Text(
"FluffyChat",
),
leading: searchMode
? IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () => setState(() => searchMode = false),
)
: null,
actions: searchMode
? null
: <Widget>[
IconButton( IconButton(
icon: Icon(Icons.search), icon: Icon(Icons.search),
onPressed: () {}, onPressed: () => setState(() => searchMode = true),
), ),
PopupMenuButton( PopupMenuButton(
onSelected: (String choice) { onSelected: (String choice) {
@ -76,7 +107,8 @@ class _ChatListState extends State<ChatList> {
); );
} }
}, },
itemBuilder: (BuildContext context) => <PopupMenuEntry<String>>[ itemBuilder: (BuildContext context) =>
<PopupMenuEntry<String>>[
const PopupMenuItem<String>( const PopupMenuItem<String>(
value: "settings", value: "settings",
child: Text('Settings'), child: Text('Settings'),
@ -100,7 +132,7 @@ class _ChatListState extends State<ChatList> {
), ),
), ),
SpeedDialChild( SpeedDialChild(
child: Icon(Icons.chat_bubble_outline), child: Icon(Icons.person_add),
backgroundColor: Colors.green, backgroundColor: Colors.green,
label: 'New private chat', label: 'New private chat',
labelStyle: TextStyle(fontSize: 18.0), labelStyle: TextStyle(fontSize: 18.0),
@ -114,7 +146,29 @@ class _ChatListState extends State<ChatList> {
future: waitForFirstSync(context), future: waitForFirstSync(context),
builder: (BuildContext context, snapshot) { builder: (BuildContext context, snapshot) {
if (snapshot.hasData) { if (snapshot.hasData) {
List<Room> rooms = Matrix.of(context).client.rooms; List<Room> rooms = List<Room>.from(Matrix.of(context).client.rooms);
rooms.removeWhere((Room room) =>
searchMode &&
!room.displayname
.toLowerCase()
.contains(searchController.text.toLowerCase() ?? ""));
if (rooms.isEmpty) {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Icon(
searchMode ? Icons.search : Icons.add,
size: 80,
color: Colors.grey,
),
Text(searchMode
? "No rooms found..."
: "Start your first chat :-)"),
],
),
);
}
return ListView.builder( return ListView.builder(
itemCount: rooms.length, itemCount: rooms.length,
itemBuilder: (BuildContext context, int i) => ChatListItem( itemBuilder: (BuildContext context, int i) => ChatListItem(