flutter_app/lib/widgets/DropdownContacts.dart

44 lines
1.3 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../model.dart';
2021-06-07 21:14:05 +00:00
bool noFilter(ContactInfoState peer) {
return true;
}
2021-05-11 22:56:16 +00:00
// Dropdown menu populated from Provider.of<ProfileInfoState>'s contact list
// Includes both peers and groups; begins empty/nothing selected
// Displays nicknames to UI but uses handles as values
// Pass an onChanged handler to access value
class DropdownContacts extends StatefulWidget {
2021-05-12 22:39:10 +00:00
DropdownContacts({
2021-05-25 00:11:39 +00:00
required this.onChanged,
2021-06-07 21:14:05 +00:00
this.filter = noFilter,
2021-05-12 22:39:10 +00:00
});
final Function(dynamic) onChanged;
2021-06-07 21:14:05 +00:00
final bool Function(ContactInfoState) filter;
@override
_DropdownContactsState createState() => _DropdownContactsState();
}
class _DropdownContactsState extends State<DropdownContacts> {
2021-05-25 00:38:07 +00:00
String? selected;
@override
Widget build(BuildContext context) {
2021-05-12 22:39:10 +00:00
return DropdownButton(
value: this.selected,
2021-06-07 21:14:05 +00:00
items: Provider.of<ProfileInfoState>(context, listen: false).contactList.contacts.where(widget.filter).map<DropdownMenuItem<String>>((ContactInfoState contact) {
2021-05-25 00:11:39 +00:00
return DropdownMenuItem<String>(value: contact.onion, child: Text(contact.nickname));
2021-05-12 22:39:10 +00:00
}).toList(),
2021-05-25 00:38:07 +00:00
onChanged: (String? newVal) {
2021-05-12 22:39:10 +00:00
setState(() {
2021-05-25 00:38:07 +00:00
this.selected = newVal;
2021-05-12 22:39:10 +00:00
});
2021-05-25 00:38:07 +00:00
widget.onChanged(newVal);
2021-05-12 22:39:10 +00:00
});
}
}