Skip to content

Commit 55e14f4

Browse files
chrisbobbegnprice
andcommitted
msglist [nfc]: Make a widget-like class for the app bar
Conceptually this should be a widget, but can't quite be, for the reasons explained in the comment. Still we can organize the code in nearly the same way as if it were a widget. Co-authored-by: Greg Price <[email protected]>
1 parent dc5acec commit 55e14f4

File tree

1 file changed

+72
-56
lines changed

1 file changed

+72
-56
lines changed

lib/widgets/message_list.dart

Lines changed: 72 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -283,53 +283,6 @@ class _MessageListPageState extends State<MessageListPage> implements MessageLis
283283

284284
@override
285285
Widget build(BuildContext context) {
286-
final store = PerAccountStoreWidget.of(context);
287-
final messageListTheme = MessageListTheme.of(context);
288-
final zulipLocalizations = ZulipLocalizations.of(context);
289-
290-
final Color? appBarBackgroundColor;
291-
bool removeAppBarBottomBorder = false;
292-
switch(narrow) {
293-
case CombinedFeedNarrow():
294-
case MentionsNarrow():
295-
case StarredMessagesNarrow():
296-
appBarBackgroundColor = null; // i.e., inherit
297-
298-
case ChannelNarrow(:final streamId):
299-
case TopicNarrow(:final streamId):
300-
final subscription = store.subscriptions[streamId];
301-
appBarBackgroundColor =
302-
colorSwatchFor(context, subscription).barBackground;
303-
// All recipient headers will match this color; remove distracting line
304-
// (but are recipient headers even needed for topic narrows?)
305-
removeAppBarBottomBorder = true;
306-
307-
case DmNarrow():
308-
appBarBackgroundColor = messageListTheme.dmRecipientHeaderBg;
309-
// All recipient headers will match this color; remove distracting line
310-
// (but are recipient headers even needed?)
311-
removeAppBarBottomBorder = true;
312-
}
313-
314-
List<Widget> actions = [];
315-
switch (narrow) {
316-
case CombinedFeedNarrow():
317-
case MentionsNarrow():
318-
case StarredMessagesNarrow():
319-
case DmNarrow():
320-
break;
321-
case ChannelNarrow(:final streamId):
322-
actions.add(_TopicListButton(streamId: streamId));
323-
case TopicNarrow(:final streamId):
324-
actions.add(IconButton(
325-
icon: const Icon(ZulipIcons.message_feed),
326-
tooltip: zulipLocalizations.channelFeedButtonTooltip,
327-
onPressed: () => Navigator.push(context,
328-
MessageListPage.buildRoute(context: context,
329-
narrow: ChannelNarrow(streamId)))));
330-
actions.add(_TopicListButton(streamId: streamId));
331-
}
332-
333286
final Anchor initAnchor;
334287
if (widget.initAnchorMessageId != null) {
335288
initAnchor = NumericAnchor(widget.initAnchorMessageId!);
@@ -340,15 +293,7 @@ class _MessageListPageState extends State<MessageListPage> implements MessageLis
340293
}
341294

342295
Widget result = Scaffold(
343-
appBar: ZulipAppBar(
344-
buildTitle: (willCenterTitle) =>
345-
MessageListAppBarTitle(narrow: narrow, willCenterTitle: willCenterTitle),
346-
actions: actions,
347-
backgroundColor: appBarBackgroundColor,
348-
shape: removeAppBarBottomBorder
349-
? const Border()
350-
: null, // i.e., inherit
351-
),
296+
appBar: _MessageListAppBar.build(context, narrow: narrow),
352297
// TODO question for Vlad: for a stream view, should we set the Scaffold's
353298
// [backgroundColor] based on stream color, as in this frame:
354299
// https://www.figma.com/file/1JTNtYo9memgW7vV6d0ygq/Zulip-Mobile?node-id=132%3A9684&mode=dev
@@ -397,6 +342,77 @@ class _MessageListPageState extends State<MessageListPage> implements MessageLis
397342
}
398343
}
399344

345+
// Conceptually this should be a widget class. But it needs to be a
346+
// PreferredSizeWidget, with the `preferredSize` that the underlying AppBar
347+
// will have... and there's currently no good way to get that value short of
348+
// constructing the whole AppBar widget with all its properties.
349+
// So this has to be built eagerly by its parent's build method,
350+
// making it a build function rather than a widget. Discussion:
351+
// https://github.com/zulip/zulip-flutter/pull/1662#discussion_r2183471883
352+
// Still we can organize it on a class, with the name the widget would have.
353+
// TODO(upstream): AppBar should expose a bit more API so that it's possible
354+
// to customize by composition in a reasonable way.
355+
abstract class _MessageListAppBar {
356+
static AppBar build(BuildContext context, {required Narrow narrow}) {
357+
final store = PerAccountStoreWidget.of(context);
358+
final messageListTheme = MessageListTheme.of(context);
359+
final zulipLocalizations = ZulipLocalizations.of(context);
360+
361+
final Color? appBarBackgroundColor;
362+
bool removeAppBarBottomBorder = false;
363+
switch(narrow) {
364+
case CombinedFeedNarrow():
365+
case MentionsNarrow():
366+
case StarredMessagesNarrow():
367+
appBarBackgroundColor = null; // i.e., inherit
368+
369+
case ChannelNarrow(:final streamId):
370+
case TopicNarrow(:final streamId):
371+
final subscription = store.subscriptions[streamId];
372+
appBarBackgroundColor =
373+
colorSwatchFor(context, subscription).barBackground;
374+
// All recipient headers will match this color; remove distracting line
375+
// (but are recipient headers even needed for topic narrows?)
376+
removeAppBarBottomBorder = true;
377+
378+
case DmNarrow():
379+
appBarBackgroundColor = messageListTheme.dmRecipientHeaderBg;
380+
// All recipient headers will match this color; remove distracting line
381+
// (but are recipient headers even needed?)
382+
removeAppBarBottomBorder = true;
383+
}
384+
385+
List<Widget> actions = [];
386+
switch (narrow) {
387+
case CombinedFeedNarrow():
388+
case MentionsNarrow():
389+
case StarredMessagesNarrow():
390+
case DmNarrow():
391+
break;
392+
case ChannelNarrow(:final streamId):
393+
actions.add(_TopicListButton(streamId: streamId));
394+
case TopicNarrow(:final streamId):
395+
actions.add(IconButton(
396+
icon: const Icon(ZulipIcons.message_feed),
397+
tooltip: zulipLocalizations.channelFeedButtonTooltip,
398+
onPressed: () => Navigator.push(context,
399+
MessageListPage.buildRoute(context: context,
400+
narrow: ChannelNarrow(streamId)))));
401+
actions.add(_TopicListButton(streamId: streamId));
402+
}
403+
404+
return ZulipAppBar(
405+
buildTitle: (willCenterTitle) =>
406+
MessageListAppBarTitle(narrow: narrow, willCenterTitle: willCenterTitle),
407+
actions: actions,
408+
backgroundColor: appBarBackgroundColor,
409+
shape: removeAppBarBottomBorder
410+
? const Border()
411+
: null, // i.e., inherit
412+
);
413+
}
414+
}
415+
400416
class RevealedMutedMessagesState extends ChangeNotifier {
401417
final Set<int> _revealedMessages = {};
402418

0 commit comments

Comments
 (0)