2021-04-06 12:00:00 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:math';
|
|
|
|
|
2021-02-02 14:33:23 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_translate/flutter_translate.dart';
|
2021-04-06 12:00:00 +00:00
|
|
|
import 'package:logger/logger.dart';
|
2021-02-02 14:33:23 +00:00
|
|
|
|
2021-04-06 12:00:00 +00:00
|
|
|
import '../../core/enums/swipe_event.dart';
|
|
|
|
import '../../core/services/swipe_service.dart';
|
|
|
|
import '../../core/util/logger.dart';
|
|
|
|
import '../../locator.dart';
|
2021-02-02 14:33:23 +00:00
|
|
|
import '../shared/app_colors.dart';
|
|
|
|
import 'history_view.dart';
|
|
|
|
import 'profile_view.dart';
|
|
|
|
import 'upload_view.dart';
|
|
|
|
|
|
|
|
class AuthenticatedTabBarView extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
AuthenticatedTabBarState createState() => AuthenticatedTabBarState();
|
|
|
|
}
|
|
|
|
|
2021-11-29 23:44:22 +00:00
|
|
|
class AuthenticatedTabBarState extends State<AuthenticatedTabBarView>
|
|
|
|
with SingleTickerProviderStateMixin {
|
2021-04-06 12:00:00 +00:00
|
|
|
final Logger _logger = getLogger();
|
|
|
|
final SwipeService _swipeService = locator<SwipeService>();
|
|
|
|
|
2021-11-29 23:44:22 +00:00
|
|
|
late StreamSubscription _swipeEventSubscription;
|
|
|
|
TabController? _tabController;
|
2021-02-02 14:33:23 +00:00
|
|
|
int _currentTabIndex = 0;
|
|
|
|
|
|
|
|
List<Widget> _realPages = [UploadView(), HistoryView(), ProfileView()];
|
|
|
|
List<Widget> _tabPages = [
|
|
|
|
UploadView(),
|
|
|
|
Container(),
|
|
|
|
Container(),
|
|
|
|
];
|
|
|
|
List<bool> _hasInit = [true, false, false];
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
_tabController = TabController(length: _realPages.length, vsync: this)
|
|
|
|
..addListener(() {
|
2021-11-29 23:44:22 +00:00
|
|
|
int selectedIndex = _tabController!.index;
|
2021-02-02 14:33:23 +00:00
|
|
|
if (_currentTabIndex != selectedIndex) {
|
|
|
|
if (!_hasInit[selectedIndex]) {
|
|
|
|
_tabPages[selectedIndex] = _realPages[selectedIndex];
|
|
|
|
_hasInit[selectedIndex] = true;
|
|
|
|
}
|
|
|
|
setState(() => _currentTabIndex = selectedIndex);
|
|
|
|
}
|
|
|
|
});
|
2021-04-06 12:00:00 +00:00
|
|
|
|
2021-11-29 23:44:22 +00:00
|
|
|
_swipeEventSubscription =
|
|
|
|
_swipeService.swipeEventController.stream.listen((SwipeEvent event) {
|
2021-04-19 22:50:46 +00:00
|
|
|
_logger.d('Received a swipe event for the authenticated tab bar: $event');
|
2021-04-06 12:00:00 +00:00
|
|
|
|
|
|
|
int targetIndex = _currentTabIndex;
|
|
|
|
if (SwipeEvent.Left == event) {
|
|
|
|
targetIndex = min(_currentTabIndex + 1, _realPages.length - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SwipeEvent.Right == event) {
|
|
|
|
targetIndex = max(_currentTabIndex - 1, 0);
|
|
|
|
}
|
|
|
|
|
2021-04-19 22:50:46 +00:00
|
|
|
if (SwipeEvent.Start == event) {
|
|
|
|
targetIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SwipeEvent.End == event) {
|
|
|
|
targetIndex = _tabPages.length - 1;
|
|
|
|
}
|
|
|
|
|
2021-11-29 23:44:22 +00:00
|
|
|
_logger.d(
|
|
|
|
"Changing to tab '$targetIndex' because of a swipe event '$event'");
|
|
|
|
_tabController!.animateTo(targetIndex);
|
2021-04-06 12:00:00 +00:00
|
|
|
});
|
2021-02-02 14:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
2021-11-29 23:44:22 +00:00
|
|
|
_tabController!.dispose();
|
2021-04-06 12:00:00 +00:00
|
|
|
_swipeEventSubscription.cancel();
|
2021-02-02 14:33:23 +00:00
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
double width = MediaQuery.of(context).size.width;
|
|
|
|
double yourWidth = width / 3;
|
2021-04-03 12:30:55 +00:00
|
|
|
double yourHeight = 55;
|
2021-02-02 14:33:23 +00:00
|
|
|
|
2021-11-29 23:44:22 +00:00
|
|
|
Color colorTabItem0 =
|
|
|
|
_currentTabIndex == 0 ? blueColor : primaryAccentColor;
|
|
|
|
Color colorTabItem1 =
|
|
|
|
_currentTabIndex == 1 ? blueColor : primaryAccentColor;
|
|
|
|
Color colorTabItem2 =
|
|
|
|
_currentTabIndex == 2 ? blueColor : primaryAccentColor;
|
2021-04-05 20:06:54 +00:00
|
|
|
|
2021-02-02 14:33:23 +00:00
|
|
|
List<Widget> _tabsButton = [
|
|
|
|
Container(
|
2021-04-05 20:06:54 +00:00
|
|
|
width: yourWidth,
|
|
|
|
height: yourHeight,
|
|
|
|
alignment: Alignment.center,
|
|
|
|
child: Tab(
|
|
|
|
icon: Icon(
|
2021-11-29 23:44:22 +00:00
|
|
|
_currentTabIndex == 0
|
|
|
|
? Icons.upload_outlined
|
|
|
|
: Icons.upload_rounded,
|
2021-04-05 20:06:54 +00:00
|
|
|
color: colorTabItem0,
|
|
|
|
),
|
2021-11-29 23:44:22 +00:00
|
|
|
child: Text(translate('tabs.upload'),
|
|
|
|
style: TextStyle(color: colorTabItem0)),
|
2021-04-05 20:06:54 +00:00
|
|
|
),
|
|
|
|
),
|
2021-02-02 14:33:23 +00:00
|
|
|
Container(
|
2021-04-05 20:06:54 +00:00
|
|
|
width: yourWidth,
|
|
|
|
height: yourHeight,
|
|
|
|
alignment: Alignment.center,
|
|
|
|
child: Tab(
|
|
|
|
icon: Icon(
|
2021-11-29 23:44:22 +00:00
|
|
|
_currentTabIndex == 1
|
|
|
|
? Icons.history_outlined
|
|
|
|
: Icons.history_rounded,
|
2021-04-05 20:06:54 +00:00
|
|
|
color: colorTabItem1,
|
|
|
|
),
|
2021-11-29 23:44:22 +00:00
|
|
|
child: Text(translate('tabs.history'),
|
|
|
|
style: TextStyle(color: colorTabItem1)),
|
2021-04-05 20:06:54 +00:00
|
|
|
),
|
|
|
|
),
|
2021-02-02 14:33:23 +00:00
|
|
|
Container(
|
2021-04-05 20:06:54 +00:00
|
|
|
width: yourWidth,
|
|
|
|
height: yourHeight,
|
|
|
|
alignment: Alignment.center,
|
|
|
|
child: Tab(
|
|
|
|
icon: Icon(
|
2021-11-29 23:44:22 +00:00
|
|
|
_currentTabIndex == 2
|
|
|
|
? Icons.person_outlined
|
|
|
|
: Icons.person_rounded,
|
2021-04-05 20:06:54 +00:00
|
|
|
color: colorTabItem2,
|
|
|
|
),
|
2021-11-29 23:44:22 +00:00
|
|
|
child: Text(translate('tabs.profile'),
|
|
|
|
style: TextStyle(color: colorTabItem2)),
|
2021-04-05 20:06:54 +00:00
|
|
|
),
|
|
|
|
),
|
2021-02-02 14:33:23 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
return Scaffold(
|
|
|
|
body: IndexedStack(index: _currentTabIndex, children: _tabPages),
|
|
|
|
bottomNavigationBar: BottomAppBar(
|
|
|
|
child: TabBar(
|
|
|
|
indicatorSize: TabBarIndicatorSize.label,
|
|
|
|
labelColor: primaryAccentColor,
|
2021-04-05 20:06:54 +00:00
|
|
|
indicatorColor: blueColor,
|
2021-04-03 12:30:55 +00:00
|
|
|
indicatorWeight: 3.0,
|
2021-02-02 14:33:23 +00:00
|
|
|
labelPadding: EdgeInsets.all(0),
|
|
|
|
tabs: _tabsButton,
|
|
|
|
isScrollable: true,
|
|
|
|
controller: _tabController,
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|