Advertisement
harmonyV

_MySliverAppBar

Feb 14th, 2025
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.76 KB | None | 0 0
  1. class _MySliverAppBar extends ConsumerWidget {
  2.   const _MySliverAppBar({
  3.     this.goalData,
  4.     required this.titleEditingController,
  5.     required this.detailsEditingController,
  6.   });
  7.  
  8.   final GoalData? goalData;
  9.   final TextEditingController titleEditingController;
  10.   final TextEditingController detailsEditingController;
  11.  
  12.   @override
  13.   Widget build(BuildContext context, WidgetRef ref) {
  14.     final colorScheme = Theme.of(context).colorScheme;
  15.     final goalCategoryProvider = ref.watch(selectedGoalCategoryProvider);
  16.     final tagIds = ref.watch(selectedTagIdsProvider);
  17.  
  18.     final categoryColorScheme = getGoalColorScheme(
  19.       Theme.of(context).brightness,
  20.       goalCategoryProvider,
  21.     );
  22.  
  23.     final buttonStyle = ButtonStyle(
  24.       backgroundColor: WidgetStateProperty.all(
  25.         colorScheme.primaryContainer.withAlpha(200),
  26.       ),
  27.       foregroundColor: WidgetStateProperty.all(
  28.         colorScheme.onSecondaryContainer,
  29.       ),
  30.     );
  31.  
  32.     final items = GoalCategory.values.map((category) {
  33.       return DropdownMenuItem<GoalCategory>(
  34.         value: category,
  35.         child: Text(
  36.           category.title,
  37.           style: TextStyle(color: colorScheme.onSecondaryContainer),
  38.           softWrap: true,
  39.           maxLines: 2,
  40.         ),
  41.       );
  42.     }).toList();
  43.  
  44.     void saveGoal() {
  45.       final details = detailsEditingController.text.isEmpty
  46.           ? null
  47.           : detailsEditingController.text;
  48.  
  49.       if (goalData == null) {
  50.         GoalsRepository().createGoal(
  51.           category: goalCategoryProvider,
  52.           title: titleEditingController.text,
  53.           details: details,
  54.           tagIds: tagIds,
  55.         );
  56.         return;
  57.       }
  58.  
  59.       GoalsRepository().updateGoal(
  60.         goalToUpdate: goalData!,
  61.         category: goalCategoryProvider,
  62.         title: titleEditingController.text,
  63.         details: details,
  64.         tagIds: tagIds,
  65.       );
  66.     }
  67.  
  68.     return SliverAppBar(
  69.       expandedHeight: 200,
  70.       floating: true,
  71.       pinned: true,
  72.       automaticallyImplyLeading: false,
  73.       backgroundColor: categoryColorScheme.surface,
  74.       bottom: AppBar(
  75.         backgroundColor: Colors.transparent,
  76.         centerTitle: false,
  77.         leading: IconButton(
  78.           style: buttonStyle,
  79.           onPressed: () {
  80.             context.pop();
  81.           },
  82.           icon: const Icon(Icons.arrow_back_ios_new),
  83.         ),
  84.         title: Container(
  85.           padding: const EdgeInsets.symmetric(
  86.             horizontal: 8,
  87.             vertical: 8,
  88.           ),
  89.           decoration: BoxDecoration(
  90.             color: colorScheme.primaryContainer.withAlpha(200),
  91.             borderRadius: BorderRadius.circular(16),
  92.           ),
  93.           child: DropdownButton<GoalCategory>(
  94.             isDense: true,
  95.             isExpanded: true,
  96.             value: goalCategoryProvider,
  97.             dropdownColor: categoryColorScheme.surface,
  98.             style: TextStyle(
  99.               color: categoryColorScheme.onSecondaryContainer,
  100.             ),
  101.             items: items,
  102.             onChanged: (newValue) {
  103.               ref
  104.                   .read(selectedGoalCategoryProvider.notifier)
  105.                   .setGoalCategory(newValue!);
  106.             },
  107.           ),
  108.         ),
  109.         actions: [
  110.           TextButton(
  111.             style: buttonStyle,
  112.             onPressed: () {
  113.               if (_formKey.currentState!.validate()) {
  114.                 saveGoal();
  115.                 context.pop();
  116.               }
  117.             },
  118.             child: Text("Save"),
  119.           ),
  120.         ],
  121.       ),
  122.       flexibleSpace: FlexibleSpaceBar(
  123.         centerTitle: false,
  124.         background: Image.asset(
  125.           goalCategoryProvider.imagePath,
  126.           fit: BoxFit.cover,
  127.         ),
  128.       ),
  129.     );
  130.   }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement