184 lines
5.8 KiB
Dart
184 lines
5.8 KiB
Dart
import 'package:didvan/services/auth/jwt_utils.dart';
|
|
import 'package:didvan/services/auth/token_storage.dart';
|
|
import 'package:didvan/views/home/media/podcast_tab_page.dart';
|
|
import 'package:didvan/views/home/media/videocast_tab_page.dart';
|
|
import 'package:didvan/views/home/media/widgets/media_banner_with_thumbnails.dart';
|
|
import 'package:didvan/views/home/media/widgets/banner_grid_view.dart';
|
|
import 'package:didvan/views/widgets/home_app_bar.dart';
|
|
import 'package:didvan/views/widgets/custom_media_tab_bar.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:didvan/views/podcasts/podcasts_state.dart';
|
|
import 'package:didvan/views/home/infography/infography_screen_state.dart';
|
|
|
|
class _MediaTabItem {
|
|
const _MediaTabItem({
|
|
required this.title,
|
|
required this.iconPath,
|
|
required this.child,
|
|
});
|
|
|
|
final String title;
|
|
final String iconPath;
|
|
final Widget child;
|
|
}
|
|
|
|
class MediaPage extends StatefulWidget {
|
|
const MediaPage({super.key});
|
|
|
|
@override
|
|
State<MediaPage> createState() => _MediaPageState();
|
|
}
|
|
|
|
class _MediaPageState extends State<MediaPage> {
|
|
int _currentIndex = 0;
|
|
final PageController _pageController = PageController();
|
|
|
|
bool _canView(String accessType) {
|
|
final token = TokenStorage.accessToken;
|
|
return token != null && JwtUtils.canView(token, accessType);
|
|
}
|
|
|
|
List<_MediaTabItem> _availableTabs() {
|
|
return [
|
|
if (_canView('podcast'))
|
|
_MediaTabItem(
|
|
title: 'پادکست',
|
|
iconPath: 'lib/assets/icons/microphone-2.svg',
|
|
child: ChangeNotifierProvider(
|
|
create: (_) => PodcastsState(),
|
|
child: const PodcastTabPage(key: ValueKey('PodcastTabPage')),
|
|
),
|
|
),
|
|
if (_canView('videocast'))
|
|
_MediaTabItem(
|
|
title: 'ویدیوکست',
|
|
iconPath: 'lib/assets/icons/video-play.svg',
|
|
child: ChangeNotifierProvider(
|
|
create: (_) => PodcastsState(),
|
|
child: const VideoCastTabPage(key: ValueKey('VideoCastTabPage')),
|
|
),
|
|
),
|
|
if (_canView('infography'))
|
|
const _MediaTabItem(
|
|
title: 'فولادینفو',
|
|
iconPath: 'lib/assets/icons/streamline-plump_factory-plant.svg',
|
|
child: SingleChildScrollView(
|
|
key: ValueKey('MainPageBanner'),
|
|
child: Padding(
|
|
padding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 16.0),
|
|
child: MediaBannerWithThumbnails(isFirst: true),
|
|
),
|
|
),
|
|
),
|
|
if (_canView('infography'))
|
|
_MediaTabItem(
|
|
title: 'اینفوگرافی',
|
|
iconPath: 'lib/assets/icons/hugeicons_chart-02.svg',
|
|
child: ChangeNotifierProvider(
|
|
create: (_) => InfographyScreenState(),
|
|
child: const SingleChildScrollView(
|
|
key: ValueKey('InfographyPage'),
|
|
child: Padding(
|
|
padding: EdgeInsets.symmetric(horizontal: 8.0, vertical: 16.0),
|
|
child: BannerGridView(),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
];
|
|
}
|
|
|
|
void _onTabTapped(int index, int tabCount) {
|
|
if (index < 0 || index >= tabCount) return;
|
|
|
|
setState(() {
|
|
_currentIndex = index;
|
|
});
|
|
_pageController.animateToPage(
|
|
index,
|
|
duration: const Duration(milliseconds: 300),
|
|
curve: Curves.easeInOut,
|
|
);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_pageController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final tabs = _availableTabs();
|
|
final safeCurrentIndex =
|
|
tabs.isEmpty || _currentIndex < tabs.length ? _currentIndex : 0;
|
|
|
|
if (safeCurrentIndex != _currentIndex) {
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_currentIndex = safeCurrentIndex;
|
|
});
|
|
if (_pageController.hasClients) {
|
|
_pageController.jumpToPage(safeCurrentIndex);
|
|
}
|
|
});
|
|
}
|
|
|
|
return Scaffold(
|
|
resizeToAvoidBottomInset: true,
|
|
// ignore: deprecated_member_use
|
|
backgroundColor: Theme.of(context).colorScheme.background,
|
|
body: SafeArea(
|
|
child: Column(
|
|
children: [
|
|
const HomeAppBar(
|
|
showBackButton: false,
|
|
showSearchField: true,
|
|
),
|
|
if (tabs.isNotEmpty) ...[
|
|
CustomMediaTabBar(
|
|
currentIndex: safeCurrentIndex,
|
|
onTap: (index) => _onTabTapped(index, tabs.length),
|
|
categories: tabs.map((tab) => tab.title).toList(),
|
|
iconPaths: tabs.map((tab) => tab.iconPath).toList(),
|
|
customIndicatorPath: 'assets/icons/tab_indicator.svg',
|
|
),
|
|
Container(
|
|
width: double.infinity,
|
|
padding: const EdgeInsets.symmetric(horizontal: 7),
|
|
child: Container(
|
|
height: 2,
|
|
color: const Color.fromRGBO(184, 184, 184, 1),
|
|
),
|
|
),
|
|
],
|
|
Expanded(
|
|
child: tabs.isEmpty
|
|
? const Center(
|
|
child: Padding(
|
|
padding: EdgeInsets.all(24),
|
|
child: Text(
|
|
'شما به بخش رسانه دسترسی ندارید.',
|
|
textAlign: TextAlign.center,
|
|
),
|
|
),
|
|
)
|
|
: PageView(
|
|
controller: _pageController,
|
|
onPageChanged: (index) {
|
|
setState(() {
|
|
_currentIndex = index;
|
|
});
|
|
},
|
|
children: tabs.map((tab) => tab.child).toList(),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|