veilidchat/lib/pages/main_pager/main_pager.dart

289 lines
9.8 KiB
Dart
Raw Normal View History

2023-08-02 21:09:28 -04:00
import 'dart:async';
import 'package:awesome_extensions/awesome_extensions.dart';
2023-09-23 12:56:54 -04:00
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
2023-07-28 20:36:05 -04:00
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
2023-08-02 21:09:28 -04:00
import 'package:flutter/services.dart';
2023-07-28 20:36:05 -04:00
import 'package:flutter_animate/flutter_animate.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_translate/flutter_translate.dart';
2023-09-24 15:30:54 -04:00
import 'package:preload_page_view/preload_page_view.dart';
2023-07-28 20:36:05 -04:00
import 'package:stylish_bottom_bar/model/bar_items.dart';
import 'package:stylish_bottom_bar/stylish_bottom_bar.dart';
2023-08-02 21:09:28 -04:00
import '../../components/bottom_sheet_action_button.dart';
2023-08-05 01:00:46 -04:00
import '../../components/paste_invite_dialog.dart';
2023-09-23 12:56:54 -04:00
import '../../components/scan_invite_dialog.dart';
2023-08-02 21:09:28 -04:00
import '../../components/send_invite_dialog.dart';
2023-09-23 12:56:54 -04:00
import '../../entities/local_account.dart';
2023-09-26 18:46:02 -04:00
import '../../proto/proto.dart' as proto;
2023-08-02 21:09:28 -04:00
import '../../tools/tools.dart';
2023-09-23 12:56:54 -04:00
import '../../veilid_support/veilid_support.dart';
2023-07-28 20:36:05 -04:00
import 'account_page.dart';
import 'chats_page.dart';
class MainPager extends ConsumerStatefulWidget {
2023-09-23 12:56:54 -04:00
const MainPager(
{required this.localAccounts,
required this.activeUserLogin,
required this.account,
super.key});
final IList<LocalAccount> localAccounts;
final TypedKey activeUserLogin;
final proto.Account account;
2023-07-28 20:36:05 -04:00
@override
MainPagerState createState() => MainPagerState();
2023-08-06 19:46:40 -04:00
static MainPagerState? of(BuildContext context) =>
context.findAncestorStateOfType<MainPagerState>();
2023-07-28 20:36:05 -04:00
}
class MainPagerState extends ConsumerState<MainPager>
with TickerProviderStateMixin {
//////////////////////////////////////////////////////////////////
final _unfocusNode = FocusNode();
var _currentPage = 0;
2023-09-24 15:30:54 -04:00
final pageController = PreloadPageController();
2023-07-28 20:36:05 -04:00
2023-07-29 10:55:35 -04:00
final _selectedIconList = <IconData>[Icons.person, Icons.chat];
2023-07-28 20:36:05 -04:00
// final _unselectedIconList = <IconData>[
// Icons.chat_outlined,
// Icons.person_outlined
// ];
final _fabIconList = <IconData>[
Icons.person_add_sharp,
2023-07-29 10:55:35 -04:00
Icons.add_comment_sharp,
2023-07-28 20:36:05 -04:00
];
2023-09-23 12:56:54 -04:00
final _bottomLabelList = <String>[
2023-07-28 20:36:05 -04:00
translate('pager.account'),
2023-07-29 10:55:35 -04:00
translate('pager.chats'),
2023-07-28 20:36:05 -04:00
];
//////////////////////////////////////////////////////////////////
@override
void initState() {
super.initState();
}
@override
void dispose() {
_unfocusNode.dispose();
2023-08-06 19:46:40 -04:00
pageController.dispose();
2023-07-28 20:36:05 -04:00
super.dispose();
}
bool onScrollNotification(ScrollNotification notification) {
if (notification is UserScrollNotification &&
notification.metrics.axis == Axis.vertical) {
switch (notification.direction) {
case ScrollDirection.forward:
// _hideBottomBarAnimationController.reverse();
// _fabAnimationController.forward(from: 0);
break;
case ScrollDirection.reverse:
// _hideBottomBarAnimationController.forward();
// _fabAnimationController.reverse(from: 1);
break;
case ScrollDirection.idle:
break;
}
}
return false;
}
BottomBarItem buildBottomBarItem(int index) {
final theme = Theme.of(context);
2023-09-23 12:56:54 -04:00
final scale = theme.extension<ScaleScheme>()!;
2023-07-28 20:36:05 -04:00
return BottomBarItem(
2023-09-23 12:56:54 -04:00
title: Text(_bottomLabelList[index]),
icon: Icon(_selectedIconList[index], color: scale.primaryScale.text),
selectedIcon:
Icon(_selectedIconList[index], color: scale.primaryScale.text),
backgroundColor: scale.primaryScale.text,
2023-07-28 20:36:05 -04:00
//unSelectedColor: theme.colorScheme.primaryContainer,
//selectedColor: theme.colorScheme.primary,
//badge: const Text('9+'),
//showBadge: true,
);
}
List<BottomBarItem> _buildBottomBarItems() {
final bottomBarItems = List<BottomBarItem>.empty(growable: true);
2023-09-23 12:56:54 -04:00
for (var index = 0; index < _bottomLabelList.length; index++) {
2023-07-28 20:36:05 -04:00
final item = buildBottomBarItem(index);
bottomBarItems.add(item);
}
return bottomBarItems;
}
2023-09-23 12:56:54 -04:00
Future<void> scanContactInvitationDialog(BuildContext context) async {
2023-08-05 01:00:46 -04:00
await showDialog<void>(
context: context,
// ignore: prefer_expression_function_bodies
builder: (context) {
return const AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20)),
),
contentPadding: EdgeInsets.only(
top: 10,
),
title: Text(
2023-09-23 12:56:54 -04:00
'Scan Contact Invite',
2023-08-05 01:00:46 -04:00
style: TextStyle(fontSize: 24),
),
2023-09-23 12:56:54 -04:00
content: ScanInviteDialog());
2023-08-05 01:00:46 -04:00
});
}
2023-08-02 21:09:28 -04:00
Widget _newContactInvitationBottomSheetBuilder(
// ignore: prefer_expression_function_bodies
BuildContext context) {
return KeyboardListener(
focusNode: FocusNode(),
onKeyEvent: (ke) {
if (ke.logicalKey == LogicalKeyboardKey.escape) {
Navigator.pop(context);
}
},
child: SizedBox(
height: 200,
child: Column(children: [
Text(translate('accounts_menu.invite_contact'),
style: Theme.of(context).textTheme.titleMedium)
.paddingAll(8),
Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [
Column(mainAxisAlignment: MainAxisAlignment.center, children: [
IconButton(
onPressed: () async {
Navigator.pop(context);
2023-09-23 12:56:54 -04:00
await SendInviteDialog.show(context);
2023-08-02 21:09:28 -04:00
},
iconSize: 64,
2023-08-05 01:00:46 -04:00
icon: const Icon(Icons.contact_page)),
Text(translate('accounts_menu.create_invite'))
2023-08-02 21:09:28 -04:00
]),
Column(mainAxisAlignment: MainAxisAlignment.center, children: [
IconButton(
2023-08-05 01:00:46 -04:00
onPressed: () async {
2023-08-02 21:09:28 -04:00
Navigator.pop(context);
2023-09-28 10:06:22 -04:00
await ScanInviteDialog.show(context);
2023-08-02 21:09:28 -04:00
},
iconSize: 64,
2023-08-05 01:00:46 -04:00
icon: const Icon(Icons.qr_code_scanner)),
Text(translate('accounts_menu.scan_invite'))
]),
Column(mainAxisAlignment: MainAxisAlignment.center, children: [
IconButton(
onPressed: () async {
Navigator.pop(context);
2023-09-23 12:56:54 -04:00
await PasteInviteDialog.show(context);
2023-08-05 01:00:46 -04:00
},
iconSize: 64,
icon: const Icon(Icons.paste)),
Text(translate('accounts_menu.paste_invite'))
2023-08-02 21:09:28 -04:00
])
]).expanded()
])));
}
// ignore: prefer_expression_function_bodies
Widget _onNewChatBottomSheetBuilder(BuildContext context) {
return const SizedBox(height: 200, child: Center(child: Text("test")));
2023-08-01 00:39:50 -04:00
}
2023-08-02 21:09:28 -04:00
Widget _bottomSheetBuilder(BuildContext context) {
2023-08-01 00:39:50 -04:00
if (_currentPage == 0) {
// New contact invitation
2023-08-02 21:09:28 -04:00
return _newContactInvitationBottomSheetBuilder(context);
2023-08-01 00:39:50 -04:00
} else if (_currentPage == 1) {
// New chat
2023-08-02 21:09:28 -04:00
return _onNewChatBottomSheetBuilder(context);
} else {
// Unknown error
return waitingPage(context);
2023-08-01 00:39:50 -04:00
}
}
2023-07-28 20:36:05 -04:00
@override
// ignore: prefer_expression_function_bodies
Widget build(BuildContext context) {
final theme = Theme.of(context);
2023-08-05 01:00:46 -04:00
final scale = theme.extension<ScaleScheme>()!;
2023-07-28 20:36:05 -04:00
return Scaffold(
2023-09-23 12:56:54 -04:00
//extendBody: true,
backgroundColor: Colors.transparent,
2023-07-28 20:36:05 -04:00
body: NotificationListener<ScrollNotification>(
onNotification: onScrollNotification,
2023-09-24 15:30:54 -04:00
child: PreloadPageView(
2023-09-23 12:56:54 -04:00
controller: pageController,
2023-09-24 15:30:54 -04:00
preloadPagesCount: 2,
2023-09-23 12:56:54 -04:00
onPageChanged: (index) {
setState(() {
_currentPage = index;
});
},
children: [
AccountPage(
localAccounts: widget.localAccounts,
activeUserLogin: widget.activeUserLogin,
account: widget.account),
2023-09-26 18:46:02 -04:00
const ChatsPage(),
2023-09-23 12:56:54 -04:00
])),
2023-07-28 20:36:05 -04:00
// appBar: AppBar(
// toolbarHeight: 24,
// title: Text(
// 'C',
// style: Theme.of(context).textTheme.headlineSmall,
// ),
// ),
bottomNavigationBar: StylishBottomBar(
2023-09-24 15:30:54 -04:00
backgroundColor: scale.primaryScale.hoverBorder,
2023-07-28 20:36:05 -04:00
// gradient: LinearGradient(
// begin: Alignment.topCenter,
// end: Alignment.bottomCenter,
// colors: <Color>[
// theme.colorScheme.primary,
// theme.colorScheme.primaryContainer,
// ]),
2023-08-02 21:09:28 -04:00
//borderRadius: BorderRadius.all(Radius.circular(16)),
2023-07-28 20:36:05 -04:00
option: AnimatedBarOptions(
// iconSize: 32,
//barAnimation: BarAnimation.fade,
iconStyle: IconStyle.animated,
inkEffect: true,
2023-09-23 12:56:54 -04:00
inkColor: scale.primaryScale.hoverBackground,
2023-07-28 20:36:05 -04:00
//opacity: 0.3,
),
items: _buildBottomBarItems(),
hasNotch: true,
fabLocation: StylishBarFabLocation.end,
currentIndex: _currentPage,
onTap: (index) async {
2023-08-06 19:46:40 -04:00
await pageController.animateToPage(index,
2023-07-29 10:55:35 -04:00
duration: 250.ms, curve: Curves.easeInOut);
2023-07-28 20:36:05 -04:00
},
),
2023-08-02 21:09:28 -04:00
floatingActionButton: BottomSheetActionButton(
2023-08-01 00:39:50 -04:00
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(14))),
2023-09-23 12:56:54 -04:00
//foregroundColor: scale.secondaryScale.text,
2023-09-24 15:30:54 -04:00
backgroundColor: scale.secondaryScale.hoverBorder,
2023-08-02 21:09:28 -04:00
builder: (context) => Icon(
_fabIconList[_currentPage],
2023-09-23 12:56:54 -04:00
color: scale.secondaryScale.text,
2023-08-02 21:09:28 -04:00
),
bottomSheetBuilder: _bottomSheetBuilder),
2023-07-28 20:36:05 -04:00
floatingActionButtonLocation: FloatingActionButtonLocation.endDocked,
);
}
}