Initial commit: Penyerahan final Source code Tugas Akhir
This commit is contained in:
236
lib/screens/home/components/appBar/home_header.dart
Normal file
236
lib/screens/home/components/appBar/home_header.dart
Normal file
@ -0,0 +1,236 @@
|
||||
import 'package:firebase_messaging/firebase_messaging.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_svg/flutter_svg.dart';
|
||||
import 'package:initial_folder/providers/carts_provider.dart';
|
||||
import 'package:initial_folder/providers/notification_provider.dart';
|
||||
import 'package:initial_folder/providers/user_info_provider.dart';
|
||||
import 'package:initial_folder/screens/cart/cart_page.dart';
|
||||
import 'package:initial_folder/screens/login/login_with_email/login_email_screen.dart';
|
||||
import 'package:initial_folder/theme.dart';
|
||||
import 'package:initial_folder/widgets/custom_navigator.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../../../../size_config.dart';
|
||||
import '../notification.dart';
|
||||
import 'icon_btn_with_counter.dart';
|
||||
import 'package:initial_folder/helper/user_info.dart';
|
||||
import 'package:initial_folder/screens/splash/splash_screen_login.dart';
|
||||
|
||||
class HomeHeader extends StatefulWidget {
|
||||
const HomeHeader({
|
||||
Key? key,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<HomeHeader> createState() => _HomeHeaderState();
|
||||
}
|
||||
|
||||
class _HomeHeaderState extends State<HomeHeader> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
Future.delayed(Duration(seconds: 0), () async {
|
||||
await Provider.of<NotificationProvider>(context, listen: false)
|
||||
.getNotificationCount();
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
UserInfoProvider userInfoProvider = Provider.of<UserInfoProvider>(context);
|
||||
FirebaseMessaging.onMessage.listen((event) async {
|
||||
if (event.notification!.body!.isNotEmpty) {
|
||||
await Provider.of<NotificationProvider>(context, listen: false)
|
||||
.getNotificationCount();
|
||||
}
|
||||
});
|
||||
|
||||
Future _showDialogNotLogin(String teks) {
|
||||
return showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
elevation: 0.0,
|
||||
contentPadding: EdgeInsets.symmetric(
|
||||
horizontal: getProportionateScreenWidth(20),
|
||||
vertical: getProportionateScreenHeight(20),
|
||||
),
|
||||
content: Text(
|
||||
'Mohon login terlebih dahulu sebelum $teks',
|
||||
style: thirdTextStyle.copyWith(
|
||||
fontSize: getProportionateScreenWidth(11)),
|
||||
),
|
||||
actions: [
|
||||
GestureDetector(
|
||||
onTap: () {
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
child: Text('Batal',
|
||||
style: primaryTextStyle.copyWith(
|
||||
fontSize: getProportionateScreenWidth(12),
|
||||
letterSpacing: 1,
|
||||
color: primaryColor)),
|
||||
),
|
||||
SizedBox(width: getProportionateScreenWidth(5)),
|
||||
GestureDetector(
|
||||
onTap: () {
|
||||
Navigator.of(context).pushNamedAndRemoveUntil(
|
||||
LoginEmail.routeName, (Route<dynamic> route) => false);
|
||||
},
|
||||
child: Text(
|
||||
'Login',
|
||||
style: primaryTextStyle.copyWith(
|
||||
fontSize: getProportionateScreenWidth(12),
|
||||
letterSpacing: 1,
|
||||
color: primaryColor),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
handleNotLoginCart() async {
|
||||
var token = await UsersInfo().getToken();
|
||||
if (token != null || Condition.loginFirebase == true) {
|
||||
Navigator.of(context, rootNavigator: true).push(
|
||||
CustomNavigator(
|
||||
child: CartPage(),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
String teks = 'dapat mengakses keranjang';
|
||||
return _showDialogNotLogin(teks);
|
||||
}
|
||||
}
|
||||
|
||||
handleNotLoginNotif() async {
|
||||
var token = await UsersInfo().getToken();
|
||||
if (token != null || Condition.loginFirebase == true) {
|
||||
Navigator.of(context, rootNavigator: true).push(
|
||||
CustomNavigator(
|
||||
child: Notifikasi(),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
String teks = 'dapat mengakses notifikasi';
|
||||
return _showDialogNotLogin(teks);
|
||||
}
|
||||
}
|
||||
|
||||
return Container(
|
||||
height: getProportionateScreenHeight(60),
|
||||
child: Center(
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Padding(
|
||||
padding: EdgeInsets.only(left: getProportionateScreenWidth(23)),
|
||||
child: RichText(
|
||||
text: TextSpan(
|
||||
children: [
|
||||
TextSpan(
|
||||
text: 'Welcome ${userInfoProvider.fullName}\n',
|
||||
style: thirdTextStyle.copyWith(
|
||||
fontSize: getProportionateScreenWidth(12),
|
||||
color: Theme.of(context).brightness == Brightness.dark
|
||||
? Colors.white
|
||||
: Colors.black),
|
||||
),
|
||||
TextSpan(
|
||||
text: 'Let\'s\ Explore Course',
|
||||
style: thirdTextStyle.copyWith(
|
||||
fontSize: getProportionateScreenWidth(16),
|
||||
fontWeight: bold,
|
||||
color: Theme.of(context).brightness == Brightness.dark
|
||||
? Colors.white
|
||||
: Colors.black),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
Row(
|
||||
children: [
|
||||
Transform.scale(
|
||||
origin: Offset(-11, 0),
|
||||
scale: getProportionateScreenHeight(0.9),
|
||||
child: Container(
|
||||
padding:
|
||||
EdgeInsets.only(right: getProportionateScreenWidth(10)),
|
||||
child: Consumer<NotificationProvider>(
|
||||
builder: (context, value, child) {
|
||||
return IconBtnWithCounter(
|
||||
icon: Theme.of(context).brightness == Brightness.dark
|
||||
? SvgPicture.asset(
|
||||
"assets/icons/notification_dark.svg")
|
||||
: SvgPicture.asset(
|
||||
"assets/icons/notification.svg"),
|
||||
numOfitem:
|
||||
(Condition.loginEmail || Condition.loginFirebase)
|
||||
? value.notificationCount
|
||||
: 0,
|
||||
press: () {
|
||||
handleNotLoginNotif();
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
!Condition.loginEmail && !Condition.loginFirebase
|
||||
? Transform.scale(
|
||||
origin: Offset(0, 0),
|
||||
scale: getProportionateScreenHeight(1),
|
||||
child: Container(
|
||||
padding: EdgeInsets.fromLTRB(
|
||||
getProportionateScreenHeight(3),
|
||||
0,
|
||||
getProportionateScreenHeight(3),
|
||||
0),
|
||||
child: IconBtnWithCounter(
|
||||
numOfitem: 0,
|
||||
icon: Theme.of(context).brightness ==
|
||||
Brightness.dark
|
||||
? SvgPicture.asset("assets/icons/cart_dark.svg")
|
||||
: SvgPicture.asset("assets/icons/cart.svg"),
|
||||
press: () => handleNotLoginCart(),
|
||||
),
|
||||
),
|
||||
)
|
||||
: Transform.scale(
|
||||
origin: Offset(0, 0),
|
||||
scale: getProportionateScreenHeight(1),
|
||||
child: Container(
|
||||
padding: EdgeInsets.fromLTRB(
|
||||
getProportionateScreenHeight(3),
|
||||
0,
|
||||
getProportionateScreenHeight(3),
|
||||
0),
|
||||
child: Consumer<CartsProvider>(
|
||||
builder: (context, state, _) {
|
||||
return IconBtnWithCounter(
|
||||
numOfitem: state.result == null
|
||||
? 0
|
||||
: state.data.length,
|
||||
icon: Theme.of(context).brightness ==
|
||||
Brightness.dark
|
||||
? SvgPicture.asset(
|
||||
"assets/icons/cart_dark.svg")
|
||||
: SvgPicture.asset("assets/icons/cart.svg"),
|
||||
press: () => handleNotLoginCart(),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding:
|
||||
EdgeInsets.only(left: SizeConfig.blockHorizontal! * 4),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
// import 'package:flutter/material.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:initial_folder/theme.dart';
|
||||
|
||||
class IconBtnWithCounter extends StatelessWidget {
|
||||
const IconBtnWithCounter({
|
||||
Key? key,
|
||||
required this.icon,
|
||||
this.numOfitem = 0,
|
||||
required this.press,
|
||||
}) : super(key: key);
|
||||
|
||||
final Widget icon;
|
||||
final int numOfitem;
|
||||
final GestureTapCallback press;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return IconButton(
|
||||
highlightColor: Colors.transparent,
|
||||
hoverColor: Colors.transparent,
|
||||
onPressed: press,
|
||||
visualDensity: VisualDensity(horizontal: -4.0, vertical: -4.0),
|
||||
padding: EdgeInsets.zero,
|
||||
icon: Stack(
|
||||
clipBehavior: Clip.none,
|
||||
children: [
|
||||
Container(
|
||||
height: 26,
|
||||
width: 26,
|
||||
child: icon,
|
||||
),
|
||||
if (numOfitem != 0)
|
||||
Positioned(
|
||||
top: 0,
|
||||
right: -2,
|
||||
child: Container(
|
||||
height: 14,
|
||||
width: 16,
|
||||
decoration: BoxDecoration(
|
||||
color: Color(0xffCD2228),
|
||||
shape: BoxShape.circle,
|
||||
// border: Border.all(width: 1.5, color: Colors.red),
|
||||
),
|
||||
child: Center(
|
||||
child: Text(
|
||||
"$numOfitem",
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(
|
||||
fontSize: 8,
|
||||
height: 1.3,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user