flutter_app/lib/views/contactsview.dart

121 lines
4.5 KiB
Dart
Raw Permalink Normal View History

2021-06-03 18:32:25 +00:00
import 'package:cwtch/cwtch_icons_icons.dart';
import 'package:flutter/material.dart';
2021-05-19 21:39:52 +00:00
import 'package:cwtch/views/torstatusview.dart';
import 'package:cwtch/widgets/contactrow.dart';
import 'package:cwtch/widgets/profileimage.dart';
import 'package:cwtch/widgets/textfield.dart';
import 'package:cwtch/widgets/tor_icon.dart';
import 'package:provider/provider.dart';
2021-05-19 02:47:25 +00:00
import '../main.dart';
2021-05-05 20:43:53 +00:00
import '../settings.dart';
2021-01-21 20:37:35 +00:00
import 'addcontactview.dart';
import '../model.dart';
2021-02-25 23:33:15 +00:00
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
class ContactsView extends StatefulWidget {
2021-05-25 00:11:39 +00:00
const ContactsView({Key? key}) : super(key: key);
@override
_ContactsViewState createState() => _ContactsViewState();
}
class _ContactsViewState extends State<ContactsView> {
2021-05-25 00:11:39 +00:00
late TextEditingController ctrlrFilter;
2021-05-19 01:17:50 +00:00
bool showSearchBar = false;
@override
void initState() {
super.initState();
ctrlrFilter = new TextEditingController(text: Provider.of<ContactListState>(context, listen: false).filter);
}
@override
Widget build(BuildContext context) {
return Scaffold(
2021-05-25 20:43:13 +00:00
endDrawerEnableOpenDragGesture: false,
drawerEnableOpenDragGesture: false,
appBar: AppBar(
title: RepaintBoundary(
child: Row(children: [
ProfileImage(
imagePath: Provider.of<ProfileInfoState>(context).imagePath,
diameter: 42,
2021-05-26 21:02:17 +00:00
border: Provider.of<Settings>(context).current().portraitOnlineBorderColor(),
2021-05-25 20:43:13 +00:00
badgeTextColor: Colors.red,
badgeColor: Colors.red,
),
SizedBox(
width: 10,
),
Expanded(
child: Text("%1 » %2".replaceAll("%1", Provider.of<ProfileInfoState>(context).nickname).replaceAll("%2", AppLocalizations.of(context)!.titleManageContacts),
2021-05-26 21:02:17 +00:00
overflow: TextOverflow.ellipsis, style: TextStyle(color: Provider.of<Settings>(context).current().mainTextColor()))), //todo
2021-05-25 20:43:13 +00:00
])),
actions: [
IconButton(icon: TorIcon(), onPressed: _pushTorStatus),
IconButton(
// need both conditions for displaying initial empty textfield and also allowing filters to be cleared if this widget gets lost/reset
icon: Icon(showSearchBar || Provider.of<ContactListState>(context).isFiltered ? Icons.search_off : Icons.search),
onPressed: () {
Provider.of<ContactListState>(context, listen: false).filter = "";
setState(() {
showSearchBar = !showSearchBar;
});
})
],
),
floatingActionButton: FloatingActionButton(
onPressed: _pushAddContact,
tooltip: AppLocalizations.of(context)!.tooltipAddContact,
2021-06-03 18:32:25 +00:00
child: const Icon(CwtchIcons.person_add_alt_1_24px),
2021-05-25 20:43:13 +00:00
),
body: showSearchBar || Provider.of<ContactListState>(context).isFiltered ? _buildFilterable() : _buildContactList());
}
2021-05-19 01:17:50 +00:00
Widget _buildFilterable() {
2021-05-19 01:23:07 +00:00
Widget txtfield = CwtchTextField(
2021-05-25 00:11:39 +00:00
controller: ctrlrFilter,
labelText: AppLocalizations.of(context)!.search,
onChanged: (newVal) {
Provider.of<ContactListState>(context, listen: false).filter = newVal;
},
);
2021-05-19 02:47:25 +00:00
return Column(children: [Padding(padding: EdgeInsets.all(8), child: txtfield), Expanded(child: _buildContactList())]);
2021-05-19 01:17:50 +00:00
}
Widget _buildContactList() {
2021-05-26 00:02:34 +00:00
final tiles = Provider.of<ContactListState>(context).filteredList().map((ContactInfoState contact) {
2021-05-25 20:43:13 +00:00
return ChangeNotifierProvider<ContactInfoState>.value(key: ValueKey(contact.profileOnion + "" + contact.onion), value: contact, builder: (_, __) => RepaintBoundary(child: ContactRow()));
2021-03-12 12:39:19 +00:00
});
2021-03-16 23:33:03 +00:00
final divided = ListTile.divideTiles(
context: context,
tiles: tiles,
).toList();
2021-05-25 20:43:13 +00:00
return RepaintBoundary(child: ListView(children: divided));
}
2021-01-21 20:37:35 +00:00
void _pushAddContact() {
2021-03-10 17:40:14 +00:00
Navigator.of(context).push(MaterialPageRoute<void>(
2021-03-24 23:35:24 +00:00
builder: (BuildContext bcontext) {
return MultiProvider(
providers: [
ChangeNotifierProvider.value(value: Provider.of<ProfileInfoState>(context)),
],
2021-03-10 17:40:14 +00:00
child: AddContactView(),
);
},
));
2021-01-21 20:37:35 +00:00
}
2021-05-19 02:47:25 +00:00
void _pushTorStatus() {
Navigator.of(context).push(MaterialPageRoute<void>(
builder: (BuildContext context) {
return MultiProvider(
providers: [Provider.value(value: Provider.of<FlwtchState>(context))],
child: TorStatusView(),
);
},
));
}
}