gpt4 book ai didi

dart - Flutter 区 block 模式 : Update BloC Streams Based Another BloC's Stream

转载 作者:IT王子 更新时间:2023-10-29 06:59:11 30 4
gpt4 key购买 nike

场景

我正在尝试创建一个具有两个屏幕的 Flutter 应用程序:ContactsScreenEditContactScreen .在 ContactsScreen , 用户将看到 DropdownButtonText . DropdownButton拥有 Contact 的列表通过 api 获取的对象。每当用户选择 Contact来自 DropdownButton , Text对象将显示有关该特定联系人的信息。此外,根据 Contact选择,一个RaisedButton将出现,单击后会将用户定向到 EditContactScreen编辑选定的 Contact .我正在使用 BloC 模式。我创建了两个 BloC,每个屏幕一个:ContactsScreenBlocEditContactScreenBloc . ContactsScreenBloc持有Stream<Contact>和一个 Sink<Contact>用于管理选定的 Contact .鉴于 EditContactScreenBlocContact 保留流和汇领域。最后,我有一个 GlobalBloc包含 Contact 的列表秒。 GlobalBloc是一个 InheritedWidget包裹了 MaterialApp .该应用程序过于简单,并且是更大应用程序的一部分,因此,我无法合并 ContactsScreenBlocEditContactScreenBloc , 并且应该有一个 GlobalBlocContact 的列表

问题

我实际上是 Flutter 的新手,所以我不确定我的方法是否合理。如果是,则当用户导航到 EditContactScreen 时并成功更新 Contact , 我怎样才能在选择的 Contact 中反射(reflect)出来呢?在ContactsScreen

代码片段

contact.dart

class Contact {
final String id;
final String firstName;
final String lastName;
final String phoneNumber;

Contact({this.id, this.firstName, this.lastName, this.phoneNumber});
Contact.fromJson(Map<String, dynamic> parsedJson)
: id = parsedJson['id'],
firstName = parsedJson['firstName'],
lastName = parsedJson['lastName'],
phoneNumber = parsedJson['phoneNumber'];

copyWith({String firstName, String lastName, String phoneNumber}) => Contact(
id: id,
firstName: firstName ?? this.firstName,
lastName: lastName ?? this.lastName,
phoneNumber: phoneNumber ?? this.phoneNumber
);

@override
bool operator ==(other) => other.id == this.id;

@override
int get hashCode => id.hashCode;
}

global.bloc.dart

class GlobalBloc {
final _repo = Repo();

final _contacts = BehaviorSubject<List<Contact>>(seedValue: []);

Stream<List<Contact>> get contacts => _contacts.stream;

Function(List<Contact>) get updateContacts => _contacts.sink.add;

Future refreshContacts() async{
final contacts = await _repo.getContacts();
updateContacts(contacts);
}
}

contacts_screen.bloc.dart

class ContactsScreenBloc {
final _selectedContact = BehaviorSubject<Contact>(seedValue: null);

Stream<Contact> get selectedContact => _selectedContact.stream;

Function(Contact) get changeSelectedContact => _selectedContact.sink.add;
}

edit_contacts_screen.bloc.dart

class ContactsScreenBloc {
final _selectedContact = BehaviorSubject<Contact>(seedValue: null);

Stream<Contact> get selectedContact => _selectedContact.stream;

Function(Contact) get changeSelectedContact => _selectedContact.sink.add;
}

global.provider.dart

class GlobalProvider extends InheritedWidget {
final bloc = GlobalBloc();
static GlobalBloc of(BuildContext context) => (context.inheritFromWidgetOfExactType(GlobalProvider) as GlobalProvider).bloc;
bool updateShouldNotify(_) => true;
}

contacts.screen.dart

class ContactsScreen extends StatelessWidget {
final bloc = ContactsScreenBloc();

@override
Widget build(BuildContext context) {
final globalBloc = GlobalProvider.of(context);

return Column(
children: <Widget>[
StreamBuilder<List<Contact>>(
stream: globalBloc.contacts,
builder: (context, listSnapshot) {
return StreamBuilder<Contact>(
stream: bloc.selectedContact,
builder: (context, itemSnapshot) {
return DropdownButton<Contact>(
items: listSnapshot.data
?.map(
(contact) => DropdownMenuItem<Contact>(
value: contact,
child: Text(contact.firstName + ' ' + contact.lastName),
),
)
?.toList(),
onChanged: bloc.changeSelectedContact,
hint: Text('Choose a contact.'),
value: itemSnapshot.hasData ? itemSnapshot.data : null,
);
},
);
},
), // end for DropdownButton StreamBuilder

StreamBuilder<Contact>(
stream: bloc.selectedContact,
builder: (context, snapshot) {
return snapshot.hasData
? Row(children: <Widget>[
Text(snapshot.data.firstName + ' ' + snapshot.data.lastName + ' ' + snapshot.data.phoneNumber),
FlatButton(
child: Text('Edit Contact'),
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) =>
EditContactScreen(contact: snapshot.data)));
},
),
],
)
: null;
}, // End for text description
)
],
);
}
}

edit_contact.screen.dart

class EditContactScreen extends StatelessWidget {
final bloc = EditContactScreenBloc();
final Contact contact;

EditContactScreen({this.contact});

@override
Widget build(BuildContext context) {
final globalBloc = GlobalProvider.of(context);

return Column(
children: <Widget>[
TextField(onChanged: (firstName) => bloc.updateContact(contact.copyWith(firstName: firstName))),
TextField(onChanged: (lastName) => bloc.updateContact(contact.copyWith(firstName: lastName))),
TextField(onChanged: (phoneNumber) => bloc.updateContact(contact.copyWith(firstName: phoneNumber))),
RaisedButton(child: Text('Update'), onPressed: () async {
await bloc.update();
await globalBloc.refreshContacts();
Navigator.of(context).pop();
},)
],
);
}
}

最佳答案

好的,我能够解决我的问题:

contacts_screen.bloc.dart ,我添加了以下方法:

void updateContactInfo(List<Contact> contacts) {
final contact = _selectedContact.value;
if (contact == null) return;
final updatedContact = contacts.firstWhere((a) => a.id == contact.id);
if (updatedContact == null) return;
changeSelectedContact(updatedContact);
}

并更新了StreamBuilder<List<Contact>>用于构建 DropdownButton变成这样:

StreamBuilder<List<Contact>>(
stream: globalBloc.contacts,
builder: (context, listSnapshot) {
bloc.updateContactInfo(listSnapshot.data);
return StreamBuilder<Contact>(
stream: bloc.selectedContact,
builder: (context, itemSnapshot) {
return DropdownButton<Contact>(
items: listSnapshot.data
?.map(
(contact) => DropdownMenuItem<Contact>(
value: contact,
child: Text(
contact.firstName + ' ' + contact.lastName),
),
)
?.toList(),
onChanged: bloc.changeSelectedContact,
hint: Text('Choose a contact.'),
value: itemSnapshot.hasData ? itemSnapshot.data : null,
);
},
);
},
)

关于dart - Flutter 区 block 模式 : Update BloC Streams Based Another BloC's Stream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53918475/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com