Files
SimpleCloudNotifier/flutter/lib/components/layout/nav_layout.dart
Mike Schwörer 521c1e94c0
All checks were successful
Build Docker and Deploy / Build Docker Container (push) Has been skipped
Build Docker and Deploy / Run Unit-Tests (push) Has been skipped
Build Docker and Deploy / Deploy to Server (push) Has been skipped
Allow accessing app-settings even if not logged-in [skip-ci]
2025-11-06 11:12:20 +01:00

122 lines
4.4 KiB
Dart

import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:provider/provider.dart';
import 'package:simplecloudnotifier/components/hidable_fab/hidable_fab.dart';
import 'package:simplecloudnotifier/components/layout/app_bar.dart';
import 'package:simplecloudnotifier/pages/channel_list/channel_list.dart';
import 'package:simplecloudnotifier/pages/send/send.dart';
import 'package:simplecloudnotifier/components/bottom_fab/fab_bottom_app_bar.dart';
import 'package:simplecloudnotifier/pages/account/account.dart';
import 'package:simplecloudnotifier/pages/message_list/message_list.dart';
import 'package:simplecloudnotifier/pages/settings/settings_view.dart';
import 'package:simplecloudnotifier/state/app_auth.dart';
import 'package:simplecloudnotifier/utils/toaster.dart';
class SCNNavLayout extends StatefulWidget {
const SCNNavLayout({super.key});
@override
State<SCNNavLayout> createState() => _SCNNavLayoutState();
}
class _SCNNavLayoutState extends State<SCNNavLayout> {
static const INDEX_MESSAGES = 0;
static const INDEX_CHANNELS = 1;
static const INDEX_ACCOUNT = 2;
static const INDEX_CONFIG = 3;
static const INDEX_SEND = 4; // FAB
int _selectedIndex = INDEX_MESSAGES;
@override
initState() {
final userAcc = Provider.of<AppAuth>(context, listen: false);
if (!userAcc.isAuth()) _selectedIndex = INDEX_ACCOUNT;
super.initState();
}
void _onItemTapped(int index) {
final userAcc = Provider.of<AppAuth>(context, listen: false);
if (!userAcc.isAuth() && [INDEX_MESSAGES, INDEX_CHANNELS, INDEX_SEND].contains(index)) {
Toaster.info("Not logged in", "Please login or create a new account first");
setState(() {
_selectedIndex = INDEX_ACCOUNT;
});
return;
}
setState(() {
_selectedIndex = index;
});
}
void _onFABTapped() {
final userAcc = Provider.of<AppAuth>(context, listen: false);
if (!userAcc.isAuth()) {
Toaster.info("Not logged in", "Please login or create a new account first");
return;
}
setState(() {
_selectedIndex = INDEX_SEND;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: SCNAppBar(
title: null,
showSearch: _selectedIndex == INDEX_MESSAGES,
showShare: false,
showThemeSwitch: true,
),
body: IndexedStack(
children: [
ExcludeFocus(excluding: _selectedIndex != INDEX_MESSAGES, child: MessageListPage(isVisiblePage: _selectedIndex == INDEX_MESSAGES)),
ExcludeFocus(excluding: _selectedIndex != INDEX_CHANNELS, child: ChannelRootPage(isVisiblePage: _selectedIndex == INDEX_CHANNELS)),
ExcludeFocus(excluding: _selectedIndex != INDEX_ACCOUNT, child: AccountRootPage(isVisiblePage: _selectedIndex == INDEX_ACCOUNT)),
ExcludeFocus(excluding: _selectedIndex != INDEX_CONFIG, child: SettingsRootPage(isVisiblePage: _selectedIndex == INDEX_CONFIG)),
ExcludeFocus(excluding: _selectedIndex != INDEX_SEND, child: SendRootPage(isVisiblePage: _selectedIndex == INDEX_SEND)),
],
index: _selectedIndex,
),
bottomNavigationBar: _buildNavBar(context),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
floatingActionButton: HidableFAB(
heroTag: 'fab_main',
onPressed: _onFABTapped,
icon: FontAwesomeIcons.solidPaperPlaneTop,
),
);
}
Widget _buildNavBar(BuildContext context) {
return FABBottomAppBar(
selectedIndex: _selectedIndex,
onTabSelected: _onItemTapped,
color: Theme.of(context).disabledColor,
selectedColor: Theme.of(context).primaryColorDark,
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'),
],
);
}
}