Skip to content

Commit 122f827

Browse files
Modified the sample
1 parent 756aa00 commit 122f827

File tree

3 files changed

+26
-29
lines changed

3 files changed

+26
-29
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# How to update the state of a Flutter chat using Flutter Bloc?
2-
This repository contains a sample to Update a state of Flutter chat using the Flutter Bloc [Syncfusion® Chat](https://help.syncfusion.com/flutter/chat/getting-started) widget.
1+
# How to update the state of a Flutter Chat widget using Flutter Bloc?
2+
This repository contains a sample to update the state of the [Syncfusion® Flutter Chat](https://help.syncfusion.com/flutter/chat/getting-started) widget using the Flutter Bloc.
33

44
Please refer the KB through this link.
55

lib/main.dart

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ class ChatApp extends StatelessWidget {
1111

1212
@override
1313
Widget build(BuildContext context) {
14-
return const MaterialApp(
15-
home: ChatScreen(),
16-
);
14+
return const MaterialApp(home: ChatScreen());
1715
}
1816
}
1917

@@ -34,10 +32,11 @@ class ChatState {
3432

3533
// Chat BLoC to manage state
3634
class ChatBloc extends Bloc<ChatEvent, ChatState> {
37-
ChatBloc() : super(ChatState(messages: [])) {
38-
on<SendMessage>((event, emit) {
39-
final updatedMessages = List<ChatMessage>.from(state.messages)
40-
..add(event.message);
35+
ChatBloc() : super(ChatState(messages: <ChatMessage>[])) {
36+
on<SendMessage>((SendMessage event, Emitter<ChatState> emit) {
37+
final List<ChatMessage> updatedMessages = List<ChatMessage>.from(
38+
state.messages,
39+
)..add(event.message);
4140
emit(ChatState(messages: updatedMessages));
4241
});
4342
}
@@ -50,37 +49,36 @@ class ChatScreen extends StatelessWidget {
5049
@override
5150
Widget build(BuildContext context) {
5251
return BlocProvider(
53-
create: (context) => ChatBloc(),
52+
create: (BuildContext context) => ChatBloc(),
5453
child: Scaffold(
55-
appBar: AppBar(
56-
title: const Text('Chat App'),
57-
),
54+
appBar: AppBar(title: const Text('Chat App')),
5855
body: Padding(
5956
padding: const EdgeInsets.symmetric(vertical: 15, horizontal: 20),
6057
child: Column(
6158
children: [
6259
Expanded(
6360
child: BlocBuilder<ChatBloc, ChatState>(
64-
buildWhen: (previous, current) =>
65-
previous.messages != current.messages,
66-
builder: (context, state) {
61+
buildWhen:
62+
(ChatState previous, ChatState current) =>
63+
previous.messages != current.messages,
64+
builder: (BuildContext context, ChatState state) {
6765
return SfChat(
6866
messages: state.messages,
6967
outgoingUser: '123-001',
7068
actionButton: ChatActionButton(
7169
onPressed: (String newMessage) {
7270
context.read<ChatBloc>().add(
73-
SendMessage(
74-
ChatMessage(
75-
text: newMessage,
76-
time: DateTime.now(),
77-
author: const ChatAuthor(
78-
id: '123-001',
79-
name: 'John Doe',
80-
),
81-
),
71+
SendMessage(
72+
ChatMessage(
73+
text: newMessage,
74+
time: DateTime.now(),
75+
author: const ChatAuthor(
76+
id: '123-001',
77+
name: 'John Doe',
8278
),
83-
);
79+
),
80+
),
81+
);
8482
},
8583
),
8684
);
@@ -94,4 +92,3 @@ class ChatScreen extends StatelessWidget {
9492
);
9593
}
9694
}
97-

pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
name: how_to_update_the_state_of_a_flutter_bloc_in_syncfusion_flutter_chat
2-
description: "A new Flutter project."
1+
name: state_maintenance_in_flutter_chat_widget_using_flutter_bloc
2+
description: "This project contains a sample for maintaining the state of the Syncfusion® Flutter Chat widget using Flutter Bloc."
33

44
version: 1.0.0+1
55

0 commit comments

Comments
 (0)