Skip to content

Commit 62cf487

Browse files
committed
widgets/content: Add MessageInheritedWidget to provide message simply
A lot of different widgets, some pretty deeply nested, go into rendering a message's content. This way each of those widgets can get data from the relevant Message object as needed, without the need for passing it all the way down, from widget params to widget params.
1 parent 9b122d4 commit 62cf487

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

lib/widgets/content.dart

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,24 @@ class MessageContent extends StatelessWidget {
2222

2323
@override
2424
Widget build(BuildContext context) {
25-
return BlockContentList(nodes: content.nodes);
25+
return InheritedMessage(message: message, child: BlockContentList(nodes: content.nodes));
26+
}
27+
}
28+
29+
/// Provides access to [message].
30+
class InheritedMessage extends InheritedWidget {
31+
const InheritedMessage({super.key, required this.message, required super.child});
32+
33+
final Message message;
34+
35+
@override
36+
bool updateShouldNotify(covariant InheritedMessage oldWidget) =>
37+
!identical(oldWidget.message, message);
38+
39+
static InheritedMessage of(BuildContext context) {
40+
final widget = context.dependOnInheritedWidgetOfExactType<InheritedMessage>();
41+
assert(widget != null, 'No InheritedMessage ancestor');
42+
return widget!;
2643
}
2744
}
2845

0 commit comments

Comments
 (0)