| 1 | import 'dart:io'; |
| 2 | |
| 3 | import 'package:cake_wallet/new-ui/widgets/modern_button.dart'; |
| 4 | import 'package:cake_wallet/store/settings_store.dart'; |
| 5 | import 'package:cake_wallet/themes/core/material_base_theme.dart'; |
| 6 | import 'package:cake_wallet/themes/core/theme_store.dart'; |
| 7 | import 'package:cake_wallet/utils/route_aware.dart'; |
| 8 | import 'package:flutter/cupertino.dart'; |
| 9 | import 'package:flutter/material.dart'; |
| 10 | import 'package:cake_wallet/di.dart'; |
| 11 | import 'package:cake_wallet/src/widgets/nav_bar.dart'; |
| 12 | import 'package:cake_wallet/generated/i18n.dart'; |
| 13 | import 'package:flutter_mobx/flutter_mobx.dart'; |
| 14 | |
| 15 | enum AppBarStyle { regular, withShadow, transparent, completelyTransparent } |
| 16 | |
| 17 | abstract class BasePage extends StatelessWidget { |
| 18 | BasePage() : _scaffoldKey = GlobalKey<ScaffoldState>(); |
| 19 | final GlobalKey<ScaffoldState> _scaffoldKey; |
| 20 | |
| 21 | final Image closeButtonImage = Image.asset('assets/images/close_button.png'); |
| 22 | final Image closeButtonImageDarkTheme = Image.asset('assets/images/close_button_dark_theme.png'); |
| 23 | |
| 24 | String? get title => null; |
| 25 | |
| 26 | Color? get backgroundLightColor => null; |
| 27 | |
| 28 | Color? get backgroundDarkColor => null; |
| 29 | |
| 30 | bool get gradientBackground => false; |
| 31 | |
| 32 | bool get gradientAll => false; |
| 33 | |
| 34 | bool get resizeToAvoidBottomInset => true; |
| 35 | |
| 36 | bool get extendBodyBehindAppBar => false; |
| 37 | |
| 38 | Widget? get endDrawer => null; |
| 39 | |
| 40 | Function(BuildContext context)? get pushToWidget => null; |
| 41 | |
| 42 | Function(BuildContext context)? get pushToNextWidget => null; |
| 43 | |
| 44 | Function(BuildContext context)? get popWidget => null; |
| 45 | |
| 46 | Function(BuildContext context)? get popNextWidget => null; |
| 47 | |
| 48 | AppBarStyle get appBarStyle => AppBarStyle.regular; |
| 49 | |
| 50 | bool get hideAppBar => false; |
| 51 | |
| 52 | Widget Function(BuildContext, Widget)? get rootWrapper => null; |
| 53 | |
| 54 | MaterialThemeBase get currentTheme => getIt.get<ThemeStore>().currentTheme; |
| 55 | |
| 56 | void onOpenEndDrawer() => _scaffoldKey.currentState!.openEndDrawer(); |
| 57 | |
| 58 | void onClose(BuildContext context) => Navigator.of(context).pop(); |
| 59 | |
| 60 | Color pageBackgroundColor(BuildContext context) => |
| 61 | (currentTheme.isDark ? backgroundDarkColor : backgroundLightColor) ?? |
| 62 | (gradientBackground ? Colors.transparent : Theme.of(context).colorScheme.surface); |
| 63 | |
| 64 | Color titleColor(BuildContext context) => Theme.of(context).colorScheme.onSurface; |
| 65 | |
| 66 | Color? pageIconColor(BuildContext context) => titleColor(context); |
| 67 | |
| 68 | Widget closeButton(BuildContext context) => Image.asset( |
| 69 | currentTheme.isDark |
| 70 | ? 'assets/images/close_button_dark_theme.png' |
| 71 | : 'assets/images/close_button.png', |
| 72 | color: pageIconColor(context), |
| 73 | height: 16, |
| 74 | ); |
| 75 | |
| 76 | Widget backButton(BuildContext context) => Icon( |
| 77 | Icons.arrow_back_ios, |
| 78 | color: Theme.of(context).colorScheme.primary, |
| 79 | size: 16, |
| 80 | ); |
| 81 | |
| 82 | Widget? leading(BuildContext context) { |
| 83 | if (ModalRoute.of(context)?.isFirst ?? true) { |
| 84 | return null; |
| 85 | } |
| 86 | |
| 87 | return Padding( |
| 88 | padding: const EdgeInsets.only(top: 4.0), |
| 89 | child: ButtonTheme( |
| 90 | minWidth: double.minPositive, |
| 91 | child: ModernButton( |
| 92 | size: 37, |
| 93 | icon: Icon(CupertinoIcons.back), |
| 94 | semanticLabel: S.of(context).seed_alert_back, |
| 95 | iconColor: Theme.of(context).colorScheme.onSurfaceVariant, |
| 96 | onPressed: () => onClose(context), |
| 97 | ), |
| 98 | ), |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | Widget? middle(BuildContext context) { |
| 103 | return title == null |
| 104 | ? null |
| 105 | : Text( |
| 106 | title!, |
| 107 | style: Theme.of(context).textTheme.titleLarge?.copyWith( |
| 108 | fontSize: 18.0, |
| 109 | fontWeight: FontWeight.w600, |
| 110 | color: titleColor(context), |
| 111 | ), |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | Widget? trailing(BuildContext context) => null; |
| 116 | |
| 117 | Widget? floatingActionButton(BuildContext context) => null; |
| 118 | |
| 119 | PreferredSizeWidget appBar(BuildContext context) { |
| 120 | final appBarColor = pageBackgroundColor(context); |
| 121 | |
| 122 | switch (appBarStyle) { |
| 123 | case AppBarStyle.regular: |
| 124 | // FIX-ME: NavBar no context |
| 125 | return NavBar( |
| 126 | // context: context, |
| 127 | leading: leading(context), |
| 128 | middle: middle(context), |
| 129 | trailing: trailing(context), |
| 130 | backgroundColor: appBarColor); |
| 131 | |
| 132 | case AppBarStyle.withShadow: |
| 133 | // FIX-ME: NavBar no context |
| 134 | return NavBar.withShadow( |
| 135 | // context: context, |
| 136 | leading: leading(context), |
| 137 | middle: middle(context), |
| 138 | trailing: trailing(context), |
| 139 | backgroundColor: appBarColor); |
| 140 | |
| 141 | case AppBarStyle.transparent: |
| 142 | return CupertinoNavigationBar( |
| 143 | leading: leading(context), |
| 144 | middle: middle(context), |
| 145 | trailing: trailing(context), |
| 146 | backgroundColor: Colors.transparent, |
| 147 | border: null, |
| 148 | ); |
| 149 | |
| 150 | case AppBarStyle.completelyTransparent: |
| 151 | return AppBar( |
| 152 | leading: leading(context), |
| 153 | title: middle(context), |
| 154 | actions: <Widget>[if (trailing(context) != null) trailing(context)!], |
| 155 | backgroundColor: Colors.transparent, |
| 156 | elevation: 0, |
| 157 | centerTitle: true, |
| 158 | ); |
| 159 | |
| 160 | default: |
| 161 | // FIX-ME: NavBar no context |
| 162 | return NavBar( |
| 163 | // context: context, |
| 164 | leading: leading(context), |
| 165 | middle: middle(context), |
| 166 | trailing: trailing(context), |
| 167 | backgroundColor: appBarColor); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | Widget body(BuildContext context); |
| 172 | |
| 173 | @override |
| 174 | Widget build(BuildContext context) { |
| 175 | final root = RouteAwareWidget( |
| 176 | child: Observer( |
| 177 | builder: (context) { |
| 178 | final backgroundImage = getIt.get<SettingsStore>().backgroundImage; |
| 179 | return Container( |
| 180 | width: double.infinity, |
| 181 | height: double.infinity, |
| 182 | decoration: BoxDecoration( |
| 183 | color: Theme.of(context).colorScheme.surface, |
| 184 | image: backgroundImage.isNotEmpty |
| 185 | ? DecorationImage( |
| 186 | image: FileImage(File(backgroundImage)), |
| 187 | fit: BoxFit.cover, |
| 188 | ) |
| 189 | : null, |
| 190 | // color: Colors.grey[200], |
| 191 | ), |
| 192 | child: Scaffold( |
| 193 | key: _scaffoldKey, |
| 194 | backgroundColor: pageBackgroundColor(context), |
| 195 | resizeToAvoidBottomInset: resizeToAvoidBottomInset, |
| 196 | extendBodyBehindAppBar: extendBodyBehindAppBar, |
| 197 | endDrawer: endDrawer, |
| 198 | appBar: hideAppBar ? null : appBar(context), |
| 199 | body: SafeArea( |
| 200 | left: false, |
| 201 | right: false, |
| 202 | top: false, |
| 203 | bottom: Platform.isAndroid, |
| 204 | child: body(context), |
| 205 | ), |
| 206 | floatingActionButton: floatingActionButton(context), |
| 207 | ), |
| 208 | ); |
| 209 | }, |
| 210 | ), |
| 211 | pushToWidget: (context) => pushToWidget?.call(context), |
| 212 | pushToNextWidget: (context) => pushToNextWidget?.call(context), |
| 213 | popWidget: (context) => popWidget?.call(context), |
| 214 | popNextWidget: (context) => popNextWidget?.call(context), |
| 215 | ); |
| 216 | |
| 217 | return rootWrapper?.call(context, root) ?? root; |
| 218 | } |
| 219 | } |