Initial commit
This commit is contained in:
41
lib/features/main/presentation/bloc/main_bloc.dart
Normal file
41
lib/features/main/presentation/bloc/main_bloc.dart
Normal file
@@ -0,0 +1,41 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
part 'main_event.dart';
|
||||
|
||||
part 'main_state.dart';
|
||||
|
||||
class MainBloc extends Bloc<MainEvent, MainState> {
|
||||
MainBloc() : super(const MainState(tab: MainTab.home)) {
|
||||
on<ChangeTabEvent>(_changeTab);
|
||||
}
|
||||
|
||||
void _changeTab(ChangeTabEvent event, Emitter<MainState> emit) {
|
||||
// if (state.tab != event.tab) {
|
||||
// changeTap(event.tab);
|
||||
// }
|
||||
emit(state.copyWith(tab: event.tab));
|
||||
}
|
||||
|
||||
// void changeTap(MainTab tab) {
|
||||
// switch (tab) {
|
||||
// case MainTab.home:
|
||||
// Navigator.of(
|
||||
// shellRootNavigatorKey.currentContext!,
|
||||
// ).pushNamedAndRemoveUntil(Routes.home, (route) => false);
|
||||
// break;
|
||||
// case MainTab.calculation:
|
||||
// Navigator.of(
|
||||
// shellRootNavigatorKey.currentContext!,
|
||||
// ).pushNamedAndRemoveUntil(Routes.calculation, (route) => false);
|
||||
// break;
|
||||
// case MainTab.profile:
|
||||
// Navigator.of(
|
||||
// shellRootNavigatorKey.currentContext!,
|
||||
// ).pushNamedAndRemoveUntil(Routes.profile, (route) => false);
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
enum MainTab { home, calculation, profile }
|
||||
14
lib/features/main/presentation/bloc/main_event.dart
Normal file
14
lib/features/main/presentation/bloc/main_event.dart
Normal file
@@ -0,0 +1,14 @@
|
||||
part of 'main_bloc.dart';
|
||||
|
||||
sealed class MainEvent extends Equatable {
|
||||
const MainEvent();
|
||||
}
|
||||
|
||||
final class ChangeTabEvent extends MainEvent {
|
||||
final MainTab tab;
|
||||
|
||||
const ChangeTabEvent({required this.tab});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [tab];
|
||||
}
|
||||
18
lib/features/main/presentation/bloc/main_state.dart
Normal file
18
lib/features/main/presentation/bloc/main_state.dart
Normal file
@@ -0,0 +1,18 @@
|
||||
part of 'main_bloc.dart';
|
||||
|
||||
class MainState extends Equatable {
|
||||
final MainTab tab;
|
||||
|
||||
const MainState({
|
||||
required this.tab,
|
||||
});
|
||||
|
||||
MainState copyWith({
|
||||
MainTab? tab,
|
||||
}) {
|
||||
return MainState(tab: tab ?? this.tab);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [tab];
|
||||
}
|
||||
123
lib/features/main/presentation/pages/main_page.dart
Normal file
123
lib/features/main/presentation/pages/main_page.dart
Normal file
@@ -0,0 +1,123 @@
|
||||
import 'package:cargocalculaterapp/core/extension/build_context_extension.dart';
|
||||
import 'package:cargocalculaterapp/features/calculator/presentation/bloc/calculator_bloc.dart';
|
||||
import 'package:cargocalculaterapp/features/calculator/presentation/pages/calculator_page.dart';
|
||||
import 'package:cargocalculaterapp/features/home/presentation/bloc/home_bloc.dart';
|
||||
import 'package:cargocalculaterapp/features/profile/presentation/bloc/profile_bloc.dart';
|
||||
import 'package:cargocalculaterapp/generated/l10n.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_svg/flutter_svg.dart';
|
||||
import '../../../../injector_container.dart';
|
||||
import '../../../home/presentation/pages/home_page.dart';
|
||||
import '../../../profile/presentation/pages/profile_page.dart';
|
||||
import '../bloc/main_bloc.dart';
|
||||
|
||||
class MainPage extends StatefulWidget {
|
||||
const MainPage({super.key});
|
||||
|
||||
@override
|
||||
State<MainPage> createState() => _MainPageState();
|
||||
}
|
||||
|
||||
class _MainPageState extends State<MainPage> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MultiBlocProvider(
|
||||
providers: [
|
||||
BlocProvider(create: (_) => sl<MainBloc>()),
|
||||
BlocProvider(create: (_) => sl<ProfileBloc>()),
|
||||
BlocProvider(create: (_) => sl<HomeBloc>()),
|
||||
BlocProvider(create: (_) => sl<CalculatorBloc>()),
|
||||
],
|
||||
child: BlocBuilder<MainBloc, MainState>(
|
||||
builder: (context, state) {
|
||||
return PopScope(
|
||||
canPop: false,
|
||||
onPopInvokedWithResult: (value, result) {
|
||||
// if (Navigator.canPop(shellRootNavigatorKey.currentContext!)) {
|
||||
// Navigator.pop(shellRootNavigatorKey.currentContext!);
|
||||
// } else
|
||||
if (state.tab.index != 0) {
|
||||
context.read<MainBloc>().add(
|
||||
ChangeTabEvent(tab: MainTab.values[0]),
|
||||
);
|
||||
} else {
|
||||
SystemNavigator.pop();
|
||||
}
|
||||
},
|
||||
child: Scaffold(
|
||||
body: IndexedStack(
|
||||
index: state.tab.index,
|
||||
children: const [HomePage(), CalculatorPage(), ProfilePage()],
|
||||
),
|
||||
bottomNavigationBar: BottomNavigationBar(
|
||||
currentIndex: state.tab.index,
|
||||
onTap: (index) {
|
||||
context.read<MainBloc>().add(
|
||||
ChangeTabEvent(tab: MainTab.values.elementAt(index)),
|
||||
);
|
||||
if (index == 0) {
|
||||
context.read<HomeBloc>().add(const RefreshEvent());
|
||||
}
|
||||
},
|
||||
elevation: 1,
|
||||
iconSize: 24,
|
||||
selectedLabelStyle: TextStyle(
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: context.color.primaryColor,
|
||||
),
|
||||
unselectedLabelStyle: TextStyle(
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: context.color.lightSecondary,
|
||||
),
|
||||
showSelectedLabels: true,
|
||||
showUnselectedLabels: true,
|
||||
items: [
|
||||
BottomNavigationBarItem(
|
||||
icon: SvgPicture.asset(
|
||||
"assets/svg/ic_box.svg",
|
||||
colorFilter: ColorFilter.mode(
|
||||
state.tab.index == 0
|
||||
? context.color.primaryColor
|
||||
: context.color.lightSecondary,
|
||||
BlendMode.srcIn,
|
||||
),
|
||||
),
|
||||
label: AppLocalization.current.orders,
|
||||
),
|
||||
BottomNavigationBarItem(
|
||||
icon: SvgPicture.asset(
|
||||
"assets/svg/ic_calculator.svg",
|
||||
colorFilter: ColorFilter.mode(
|
||||
state.tab.index == 1
|
||||
? context.color.primaryColor
|
||||
: context.color.lightSecondary,
|
||||
BlendMode.srcIn,
|
||||
),
|
||||
),
|
||||
label: AppLocalization.current.calculator,
|
||||
),
|
||||
BottomNavigationBarItem(
|
||||
icon: SvgPicture.asset(
|
||||
"assets/svg/ic_profile.svg",
|
||||
colorFilter: ColorFilter.mode(
|
||||
state.tab.index == 2
|
||||
? context.color.primaryColor
|
||||
: context.color.lightSecondary,
|
||||
BlendMode.srcIn,
|
||||
),
|
||||
),
|
||||
label: AppLocalization.current.profile,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user