|
1 | 1 | import 'package:flutter/material.dart';
|
2 | 2 |
|
3 |
| -import '../generated/l10n/zulip_localizations.dart'; |
4 |
| -import '../model/narrow.dart'; |
5 |
| -import 'app_bar.dart'; |
| 3 | +import 'icons.dart'; |
6 | 4 | import 'inbox.dart';
|
7 |
| -import 'message_list.dart'; |
8 | 5 | import 'page.dart';
|
9 | 6 | import 'recent_dm_conversations.dart';
|
10 |
| -import 'store.dart'; |
11 | 7 | import 'subscription_list.dart';
|
12 |
| -import 'text.dart'; |
| 8 | +import 'theme.dart'; |
13 | 9 |
|
14 |
| -class HomePage extends StatelessWidget { |
| 10 | +class HomePage extends StatefulWidget { |
15 | 11 | const HomePage({super.key});
|
16 | 12 |
|
17 | 13 | static Route<void> buildRoute({required int accountId}) {
|
18 | 14 | return MaterialAccountWidgetRoute(accountId: accountId,
|
19 |
| - page: const HomePage()); |
| 15 | + page: const HomePage()); |
20 | 16 | }
|
21 | 17 |
|
22 | 18 | @override
|
23 |
| - Widget build(BuildContext context) { |
24 |
| - final store = PerAccountStoreWidget.of(context); |
25 |
| - final zulipLocalizations = ZulipLocalizations.of(context); |
| 19 | + State<HomePage> createState() => _HomePageState(); |
| 20 | +} |
26 | 21 |
|
27 |
| - final colorScheme = ColorScheme.of(context); |
| 22 | +class _HomePageState extends State<HomePage> { |
| 23 | + int _pageIndex = 0; |
28 | 24 |
|
29 |
| - InlineSpan bold(String text) => TextSpan( |
30 |
| - style: const TextStyle().merge(weightVariableTextStyle(context, wght: 700)), |
31 |
| - text: text); |
| 25 | + @override |
| 26 | + Widget build(BuildContext context) { |
| 27 | + const pageBodies = [ |
| 28 | + InboxPageBody(), |
| 29 | + SubscriptionListPageBody(), |
| 30 | + // Users |
| 31 | + RecentDmConversationsPageBody(), |
| 32 | + // Menu |
| 33 | + ]; |
32 | 34 |
|
33 |
| - int? testStreamId; |
34 |
| - if (store.connection.realmUrl.origin == 'https://chat.zulip.org') { |
35 |
| - testStreamId = 7; // i.e. `#test here`; TODO cut this scaffolding hack |
| 35 | + Widget Function(int pageIndex) navigationButtonBuilder(IconData icon, String tooltip) { |
| 36 | + return (pageIndex) => NavigationButton( |
| 37 | + icon: icon, tooltip: tooltip, |
| 38 | + selected: _pageIndex == pageIndex, |
| 39 | + onPressed: () { |
| 40 | + setState(() { |
| 41 | + _pageIndex = pageIndex; |
| 42 | + }); |
| 43 | + }); |
36 | 44 | }
|
37 | 45 |
|
| 46 | + final navigationButtonBuilders = [ |
| 47 | + navigationButtonBuilder(ZulipIcons.inbox, 'Inbox'), |
| 48 | + navigationButtonBuilder(ZulipIcons.hash_italic, 'Channels'), |
| 49 | + // navigationButtonBuilder(ZulipIcons.contacts, 'Users'), |
| 50 | + navigationButtonBuilder(ZulipIcons.user, 'Direct messages'), |
| 51 | + // navigationButtonBuilder(ZulipIcons.menu, 'Menu'), |
| 52 | + ]; |
| 53 | + |
| 54 | + final designVariables = DesignVariables.of(context); |
| 55 | + |
38 | 56 | return Scaffold(
|
39 |
| - appBar: ZulipAppBar(title: const Text("Home")), |
40 |
| - body: ElevatedButtonTheme( |
41 |
| - data: ElevatedButtonThemeData(style: ButtonStyle( |
42 |
| - backgroundColor: WidgetStatePropertyAll(colorScheme.secondaryContainer), |
43 |
| - foregroundColor: WidgetStatePropertyAll(colorScheme.onSecondaryContainer))), |
44 |
| - child: Center( |
45 |
| - child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [ |
46 |
| - DefaultTextStyle.merge( |
47 |
| - textAlign: TextAlign.center, |
48 |
| - style: const TextStyle(fontSize: 18), |
49 |
| - child: Column(children: [ |
50 |
| - Text.rich(TextSpan( |
51 |
| - text: 'Connected to: ', |
52 |
| - children: [bold(store.realmUrl.toString())])), |
53 |
| - ])), |
54 |
| - const SizedBox(height: 16), |
55 |
| - ElevatedButton( |
56 |
| - onPressed: () => Navigator.push(context, |
57 |
| - MessageListPage.buildRoute(context: context, |
58 |
| - narrow: const CombinedFeedNarrow())), |
59 |
| - child: Text(zulipLocalizations.combinedFeedPageTitle)), |
60 |
| - const SizedBox(height: 16), |
61 |
| - ElevatedButton( |
62 |
| - onPressed: () => Navigator.push(context, |
63 |
| - MessageListPage.buildRoute(context: context, |
64 |
| - narrow: const MentionsNarrow())), |
65 |
| - child: Text(zulipLocalizations.mentionsPageTitle)), |
66 |
| - const SizedBox(height: 16), |
67 |
| - ElevatedButton( |
68 |
| - onPressed: () => Navigator.push(context, |
69 |
| - MessageListPage.buildRoute(context: context, |
70 |
| - narrow: const StarredMessagesNarrow())), |
71 |
| - child: Text(zulipLocalizations.starredMessagesPageTitle)), |
72 |
| - const SizedBox(height: 16), |
73 |
| - ElevatedButton( |
74 |
| - onPressed: () => Navigator.push(context, |
75 |
| - InboxPage.buildRoute(context: context)), |
76 |
| - child: const Text("Inbox")), // TODO(i18n) |
77 |
| - const SizedBox(height: 16), |
78 |
| - ElevatedButton( |
79 |
| - onPressed: () => Navigator.push(context, |
80 |
| - SubscriptionListPage.buildRoute(context: context)), |
81 |
| - child: const Text("Subscribed channels")), |
82 |
| - const SizedBox(height: 16), |
83 |
| - ElevatedButton( |
84 |
| - onPressed: () => Navigator.push(context, |
85 |
| - RecentDmConversationsPage.buildRoute(context: context)), |
86 |
| - child: Text(zulipLocalizations.recentDmConversationsPageTitle)), |
87 |
| - if (testStreamId != null) ...[ |
88 |
| - const SizedBox(height: 16), |
89 |
| - ElevatedButton( |
90 |
| - onPressed: () => Navigator.push(context, |
91 |
| - MessageListPage.buildRoute(context: context, |
92 |
| - narrow: ChannelNarrow(testStreamId!))), |
93 |
| - child: const Text("#test here")), // scaffolding hack, see above |
94 |
| - ], |
95 |
| - ])))); |
| 57 | + body: Stack( |
| 58 | + children: [ |
| 59 | + for (int index = 0; index < pageBodies.length; index++) |
| 60 | + Offstage(offstage: index != _pageIndex, child: pageBodies[index]) |
| 61 | + ]), |
| 62 | + bottomNavigationBar: SafeArea( |
| 63 | + child: DecoratedBox( |
| 64 | + decoration: BoxDecoration( |
| 65 | + border: Border(top: BorderSide(color: designVariables.borderBar, width: 0)), |
| 66 | + color: designVariables.bgBotBar), |
| 67 | + child: ConstrainedBox( |
| 68 | + // TODO(design): determine a suitable max width |
| 69 | + constraints: const BoxConstraints(maxWidth: 600).tighten(height: 48), |
| 70 | + child: Row( |
| 71 | + mainAxisAlignment: MainAxisAlignment.spaceAround, |
| 72 | + crossAxisAlignment: CrossAxisAlignment.stretch, |
| 73 | + children: [ |
| 74 | + for (final (pageIndex, buildButton) in navigationButtonBuilders.indexed) |
| 75 | + Expanded(child: buildButton(pageIndex)), |
| 76 | + ]))))); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +class Scaled extends StatefulWidget { |
| 81 | + const Scaled({ |
| 82 | + super.key, |
| 83 | + required this.scaleEnd, |
| 84 | + required this.duration, |
| 85 | + required this.child, |
| 86 | + }); |
| 87 | + |
| 88 | + final double scaleEnd; |
| 89 | + final Duration duration; |
| 90 | + final Widget child; |
| 91 | + |
| 92 | + @override |
| 93 | + State<Scaled> createState() => _ScaledState(); |
| 94 | +} |
| 95 | + |
| 96 | +class _ScaledState extends State<Scaled> { |
| 97 | + double _scale = 1; |
| 98 | + |
| 99 | + void _changeScale(double scale) { |
| 100 | + setState(() { |
| 101 | + _scale = scale; |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + @override |
| 106 | + Widget build(BuildContext context) { |
| 107 | + return GestureDetector( |
| 108 | + behavior: HitTestBehavior.translucent, |
| 109 | + onTapDown: (_) => _changeScale(widget.scaleEnd), |
| 110 | + onTapUp: (_) => _changeScale(1), |
| 111 | + onTapCancel: () => _changeScale(1), |
| 112 | + child: AnimatedScale( |
| 113 | + scale: _scale, |
| 114 | + duration: widget.duration, |
| 115 | + curve: Curves.easeOut, |
| 116 | + child: widget.child)); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +class NavigationButton extends StatelessWidget { |
| 121 | + const NavigationButton({ |
| 122 | + super.key, |
| 123 | + required this.icon, |
| 124 | + required this.tooltip, |
| 125 | + required this.selected, |
| 126 | + required this.onPressed, |
| 127 | + }); |
| 128 | + |
| 129 | + final IconData icon; |
| 130 | + final String tooltip; |
| 131 | + final bool selected; |
| 132 | + final void Function() onPressed; |
| 133 | + |
| 134 | + @override |
| 135 | + Widget build(BuildContext context) { |
| 136 | + final designVariables = DesignVariables.of(context); |
| 137 | + |
| 138 | + final iconColor = WidgetStateColor.fromMap({ |
| 139 | + WidgetState.pressed: designVariables.iconSelected, |
| 140 | + ~WidgetState.pressed: (selected) ? designVariables.iconSelected |
| 141 | + : designVariables.icon, |
| 142 | + }); |
| 143 | + |
| 144 | + return Scaled( |
| 145 | + scaleEnd: 0.875, |
| 146 | + duration: const Duration(milliseconds: 100), |
| 147 | + child: IconButton( |
| 148 | + icon: Icon(icon), |
| 149 | + tooltip: tooltip, |
| 150 | + onPressed: onPressed, |
| 151 | + style: IconButton.styleFrom( |
| 152 | + // TODO(#417): Disable splash effects for all buttons globally. |
| 153 | + splashFactory: NoSplash.splashFactory, |
| 154 | + highlightColor: designVariables.iconSelected.withValues(alpha: 0.05), |
| 155 | + shape: const RoundedRectangleBorder( |
| 156 | + borderRadius: BorderRadius.all(Radius.circular(4))), |
| 157 | + ).copyWith(foregroundColor: iconColor)), |
| 158 | + ); |
96 | 159 | }
|
97 | 160 | }
|
0 commit comments