SimpleCloudNotifier/flutter/lib/nav_layout.dart

112 lines
3.9 KiB
Dart
Raw Normal View History

2024-02-10 19:57:17 +01:00
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
2024-05-27 17:21:29 +02:00
import 'package:provider/provider.dart';
2024-06-02 17:09:57 +02:00
import 'package:simplecloudnotifier/components/hidable_fab/hidable_fab.dart';
2024-05-21 23:20:34 +02:00
import 'package:simplecloudnotifier/components/layout/app_bar.dart';
2024-05-26 00:20:25 +02:00
import 'package:simplecloudnotifier/pages/channel_list/channel_list.dart';
2024-06-02 17:09:57 +02:00
import 'package:simplecloudnotifier/pages/send/send.dart';
2024-05-25 22:06:43 +02:00
import 'package:simplecloudnotifier/components/bottom_fab/fab_bottom_app_bar.dart';
2024-05-27 17:21:29 +02:00
import 'package:simplecloudnotifier/pages/account/account.dart';
2024-05-25 22:06:43 +02:00
import 'package:simplecloudnotifier/pages/message_list/message_list.dart';
import 'package:simplecloudnotifier/pages/settings/root.dart';
2024-06-02 17:09:57 +02:00
import 'package:simplecloudnotifier/state/app_auth.dart';
2024-05-31 23:21:24 +02:00
import 'package:simplecloudnotifier/utils/toaster.dart';
2024-02-10 19:57:17 +01:00
class SCNNavLayout extends StatefulWidget {
const SCNNavLayout({super.key});
@override
State<SCNNavLayout> createState() => _SCNNavLayoutState();
}
class _SCNNavLayoutState extends State<SCNNavLayout> {
2024-02-18 16:23:10 +01:00
int _selectedIndex = 0; // 4 == FAB
2024-02-10 19:57:17 +01:00
2024-05-27 17:21:29 +02:00
@override
initState() {
2024-06-02 17:09:57 +02:00
final userAcc = Provider.of<AppAuth>(context, listen: false);
if (!userAcc.isAuth()) _selectedIndex = 2;
2024-05-27 17:21:29 +02:00
super.initState();
}
2024-02-10 19:57:17 +01:00
void _onItemTapped(int index) {
2024-06-02 17:09:57 +02:00
final userAcc = Provider.of<AppAuth>(context, listen: false);
if (!userAcc.isAuth()) {
2024-05-31 23:21:24 +02:00
Toaster.info("Not logged in", "Please login or create a new account first");
2024-05-27 17:21:29 +02:00
return;
}
2024-02-10 19:57:17 +01:00
setState(() {
_selectedIndex = index;
});
}
2024-02-18 16:23:10 +01:00
void _onFABTapped() {
2024-06-02 17:09:57 +02:00
final userAcc = Provider.of<AppAuth>(context, listen: false);
if (!userAcc.isAuth()) {
2024-05-31 23:21:24 +02:00
Toaster.info("Not logged in", "Please login or create a new account first");
2024-05-27 17:21:29 +02:00
return;
}
2024-02-10 19:57:17 +01:00
setState(() {
2024-02-18 16:23:10 +01:00
_selectedIndex = 4;
2024-02-10 19:57:17 +01:00
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
2024-05-23 17:41:51 +02:00
appBar: SCNAppBar(
title: null,
2024-06-25 12:00:34 +02:00
showSearch: _selectedIndex == 0,
2024-06-08 20:01:23 +02:00
showShare: false,
2024-05-23 17:41:51 +02:00
showThemeSwitch: true,
),
2024-06-08 12:55:58 +02:00
body: IndexedStack(
2024-05-26 18:34:42 +02:00
children: [
ExcludeFocus(excluding: _selectedIndex != 0, child: MessageListPage(isVisiblePage: _selectedIndex == 0)),
ExcludeFocus(excluding: _selectedIndex != 1, child: ChannelRootPage(isVisiblePage: _selectedIndex == 1)),
ExcludeFocus(excluding: _selectedIndex != 2, child: AccountRootPage(isVisiblePage: _selectedIndex == 2)),
ExcludeFocus(excluding: _selectedIndex != 3, child: SettingsRootPage(isVisiblePage: _selectedIndex == 3)),
ExcludeFocus(excluding: _selectedIndex != 4, child: SendRootPage(isVisiblePage: _selectedIndex == 4)),
2024-05-26 18:34:42 +02:00
],
index: _selectedIndex,
),
2024-02-10 19:57:17 +01:00
bottomNavigationBar: _buildNavBar(context),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
2024-06-02 17:09:57 +02:00
floatingActionButton: HidableFAB(
2024-07-13 00:11:13 +02:00
heroTag: 'fab_main',
2024-06-02 17:09:57 +02:00
onPressed: _onFABTapped,
icon: FontAwesomeIcons.solidPaperPlaneTop,
),
2024-02-10 19:57:17 +01:00
);
}
Widget _buildNavBar(BuildContext context) {
return FABBottomAppBar(
2024-02-18 16:23:10 +01:00
selectedIndex: _selectedIndex,
2024-02-10 19:57:17 +01:00
onTabSelected: _onItemTapped,
2024-02-11 01:08:51 +01:00
color: Theme.of(context).disabledColor,
selectedColor: Theme.of(context).primaryColorDark,
2024-02-10 19:57:17 +01:00
notchedShape: const AutomaticNotchedShape(
RoundedRectangleBorder(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(15),
topRight: Radius.circular(15),
),
),
RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(17)),
),
),
items: [
FABBottomAppBarItem(iconData: FontAwesomeIcons.solidEnvelope, text: 'Messages'),
FABBottomAppBarItem(iconData: FontAwesomeIcons.solidSnake, text: 'Channels'),
FABBottomAppBarItem(iconData: FontAwesomeIcons.solidFileUser, text: 'Account'),
FABBottomAppBarItem(iconData: FontAwesomeIcons.solidGear, text: 'Settings'),
],
);
}
}