Advertisement
muhaiminurabir

app bar flutter custom

May 8th, 2025
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 5.07 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_svg/svg.dart';
  3. import 'package:investorv2/provider/notification_provider.dart';
  4. import 'package:investorv2/singleton/shared_pref.dart';
  5. import 'package:investorv2/utility/colors.dart';
  6. import 'package:investorv2/utility/routes.dart';
  7. import 'package:investorv2/utility/valueStrings.dart';
  8. import 'package:provider/provider.dart';
  9.  
  10. class CustomAppBar extends StatefulWidget {
  11.   final String title;
  12.   final Color color;
  13.   final Function() onTap;
  14.   final bool notificationIcon;
  15.   final bool menuIcon;
  16.  
  17.   const CustomAppBar({
  18.     super.key,
  19.     required this.title,
  20.     required this.color,
  21.     required this.onTap,
  22.     this.notificationIcon = false,
  23.     this.menuIcon = false,
  24.   });
  25.  
  26.   @override
  27.   _CustomAppBarState createState() => _CustomAppBarState();
  28. }
  29.  
  30. class _CustomAppBarState extends State<CustomAppBar> {
  31.   String logged = '';
  32.   @override
  33.   void initState() {
  34.     logged = SharedPref.getString(token);
  35.  
  36.     if (widget.notificationIcon) {
  37.       context.read<NotificationProvider>().unreadNotification = null;
  38.       context.read<NotificationProvider>().checkUnreadNotification();
  39.     }
  40.     super.initState();
  41.   }
  42.  
  43.   @override
  44.   Widget build(BuildContext context) {
  45.     return PreferredSize(
  46.       preferredSize:
  47.           const Size.fromHeight(kToolbarHeight), // Set the preferred height
  48.       child: AppBar(
  49.         title: Text(
  50.           widget.title,
  51.           style: widget.color == ProjectColors.primaryColor
  52.               ? Theme.of(context)
  53.                   .appBarTheme
  54.                   .titleTextStyle!
  55.                   .merge(TextStyle(fontWeight: FontWeight.bold, fontSize: 18))
  56.               : Theme.of(context).appBarTheme.titleTextStyle!.merge(TextStyle(
  57.                   color: ProjectColors.black,
  58.                   fontWeight: FontWeight.bold,
  59.                   fontSize: 18)),
  60.         ),
  61.         centerTitle: true,
  62.         backgroundColor: widget.color,
  63.         leading: widget.menuIcon
  64.             ? Padding(
  65.                 padding: const EdgeInsets.only(left: 8.0),
  66.                 child: IconButton(
  67.                     color: Colors.black,
  68.                     icon: SvgPicture.asset(
  69.                       "assets/images/hamburger.svg",
  70.                       colorFilter: const ColorFilter.mode(
  71.                           ProjectColors.black, BlendMode.srcIn),
  72.                       height: 36,
  73.                       width: 36,
  74.                     ),
  75.                     onPressed: widget.onTap),
  76.               )
  77.             : IconButton(
  78.                 icon: Icon(
  79.                   Icons.arrow_back,
  80.                   color: widget.color == ProjectColors.primaryColor
  81.                       ? Colors.white
  82.                       : ProjectColors.black,
  83.                   size: 28,
  84.                 ),
  85.                 iconSize: 40,
  86.                 onPressed: widget.onTap,
  87.               ),
  88.         actions: [
  89.           widget.notificationIcon && logged.isNotEmpty
  90.               ? Padding(
  91.                   padding: const EdgeInsets.only(right: 24.0),
  92.                   child: GestureDetector(
  93.                     onTap: () {
  94.                       Navigator.pushNamed(context, notificationPage,
  95.                           arguments: {});
  96.                     },
  97.                     child: Stack(alignment: Alignment.topRight, children: [
  98.                       Icon(Icons.notifications),
  99.                       showIcon(context)
  100.                     ]),
  101.                   ),
  102.                 )
  103.               : Container(),
  104.         ],
  105.       ),
  106.     );
  107.   }
  108.  
  109.   Widget showIcon(BuildContext context) {
  110.     final unreadNotification =
  111.         context.watch<NotificationProvider>().unreadNotification;
  112.  
  113.     if (unreadNotification == null ||
  114.         unreadNotification.notificationsUnread == null) {
  115.       return const SizedBox
  116.           .shrink(); // Return an empty widget if unreadNotification is null
  117.     }
  118.  
  119.     return Visibility(
  120.       visible: unreadNotification.notificationsUnread!,
  121.       child: Positioned(
  122.         child: Container(
  123.           padding: EdgeInsets.all(2),
  124.           decoration: BoxDecoration(
  125.             color: Colors.red,
  126.             shape: BoxShape.circle,
  127.           ),
  128.           constraints: BoxConstraints(
  129.             minWidth: 13,
  130.             minHeight: 13,
  131.           ),
  132.           child: Text(
  133.             '',
  134.             style: TextStyle(
  135.               color: Colors.white,
  136.               fontSize: 8,
  137.             ),
  138.             textAlign: TextAlign.center,
  139.           ),
  140.         ),
  141.       ),
  142.     );
  143.   }
  144. }
  145.  
  146.  
  147.  
  148. // use it by
  149.  
  150. Scaffold(
  151.       appBar: PreferredSize(
  152.         preferredSize: const Size.fromHeight(kToolbarHeight), // Set
  153.         child: CustomAppBar(
  154.           title: args["tittle"],
  155.           color: Colors.white,
  156.           onTap: () {
  157.             Navigator.of(context).pop();
  158.           },
  159.         ),
  160.       ),
  161.       body: YoutubePlayerBuilder(
  162.         player: YoutubePlayer(controller: _controller),
  163.         builder: (context, player) {
  164.           return Column(children: [player]);
  165.         },
  166.       ),
  167.     );
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement