|
| 1 | +import 'package:flutter/foundation.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | + |
| 4 | +const _defaultIconColor = Color(0xFF2196F3); |
| 5 | + |
| 6 | +/// Status bar color the template uses: each channel of [color] times 0.8. |
| 7 | +Color statusBarColor(Color color) => Color.fromARGB( |
| 8 | + 255, |
| 9 | + (color.r * 255 * 0.8).toInt(), |
| 10 | + (color.g * 255 * 0.8).toInt(), |
| 11 | + (color.b * 255 * 0.8).toInt(), |
| 12 | +); |
| 13 | + |
| 14 | +/// Title color the template uses: white on dark colors, black on light ones |
| 15 | +/// (Android's `Color.luminance(color) < 0.5`). |
| 16 | +Color titleColor(Color color) => |
| 17 | + color.computeLuminance() < 0.5 ? Colors.white : Colors.black; |
| 18 | + |
| 19 | +/// A sketch of the generated app: a phone showing the app screen, and the |
| 20 | +/// home-screen icon with its label below. Mirrors the template's |
| 21 | +/// MainActivity (status bar, title bar, fullscreen), not the website itself. |
| 22 | +class AppPreview extends StatelessWidget { |
| 23 | + const AppPreview({ |
| 24 | + super.key, |
| 25 | + required this.name, |
| 26 | + required this.url, |
| 27 | + required this.color, |
| 28 | + required this.fullscreen, |
| 29 | + this.icon, |
| 30 | + }); |
| 31 | + |
| 32 | + final String name; |
| 33 | + |
| 34 | + /// Normalized URL, or null while the URL field is empty or invalid. |
| 35 | + final String? url; |
| 36 | + final Color color; |
| 37 | + final bool fullscreen; |
| 38 | + final Uint8List? icon; |
| 39 | + |
| 40 | + @override |
| 41 | + Widget build(BuildContext context) { |
| 42 | + final title = name.trim().isEmpty ? 'App name' : name.trim(); |
| 43 | + final onBar = titleColor(statusBarColor(color)); |
| 44 | + final host = url == null ? null : Uri.tryParse(url!)?.host; |
| 45 | + return Column( |
| 46 | + children: [ |
| 47 | + Container( |
| 48 | + width: 200, |
| 49 | + height: 340, |
| 50 | + padding: const EdgeInsets.all(6), |
| 51 | + decoration: BoxDecoration( |
| 52 | + color: const Color(0xFF202124), |
| 53 | + border: Border.all( |
| 54 | + color: Theme.of(context).colorScheme.outlineVariant, |
| 55 | + ), |
| 56 | + borderRadius: BorderRadius.circular(24), |
| 57 | + ), |
| 58 | + child: ClipRRect( |
| 59 | + borderRadius: BorderRadius.circular(18), |
| 60 | + child: ColoredBox( |
| 61 | + color: Colors.white, |
| 62 | + child: Column( |
| 63 | + crossAxisAlignment: CrossAxisAlignment.stretch, |
| 64 | + children: [ |
| 65 | + Container( |
| 66 | + key: const Key('preview-status-bar'), |
| 67 | + height: 20, |
| 68 | + color: statusBarColor(color), |
| 69 | + padding: const EdgeInsets.symmetric(horizontal: 12), |
| 70 | + child: Row( |
| 71 | + children: [ |
| 72 | + Text( |
| 73 | + '12:00', |
| 74 | + style: TextStyle(color: onBar, fontSize: 10), |
| 75 | + ), |
| 76 | + const Spacer(), |
| 77 | + Icon(Icons.signal_cellular_alt, color: onBar, size: 11), |
| 78 | + const SizedBox(width: 3), |
| 79 | + Icon(Icons.battery_full, color: onBar, size: 11), |
| 80 | + ], |
| 81 | + ), |
| 82 | + ), |
| 83 | + if (!fullscreen) |
| 84 | + Container( |
| 85 | + key: const Key('preview-title-bar'), |
| 86 | + height: 40, |
| 87 | + color: color, |
| 88 | + padding: const EdgeInsets.symmetric(horizontal: 12), |
| 89 | + alignment: Alignment.centerLeft, |
| 90 | + child: Text( |
| 91 | + title, |
| 92 | + maxLines: 1, |
| 93 | + overflow: TextOverflow.ellipsis, |
| 94 | + style: TextStyle( |
| 95 | + color: titleColor(color), |
| 96 | + fontSize: 15, |
| 97 | + fontWeight: FontWeight.w500, |
| 98 | + ), |
| 99 | + ), |
| 100 | + ), |
| 101 | + Expanded( |
| 102 | + child: Center( |
| 103 | + child: Padding( |
| 104 | + padding: const EdgeInsets.all(12), |
| 105 | + child: Column( |
| 106 | + mainAxisSize: MainAxisSize.min, |
| 107 | + children: [ |
| 108 | + Icon( |
| 109 | + Icons.language, |
| 110 | + size: 40, |
| 111 | + color: Colors.grey.shade400, |
| 112 | + ), |
| 113 | + const SizedBox(height: 8), |
| 114 | + Text( |
| 115 | + host ?? 'Your website', |
| 116 | + textAlign: TextAlign.center, |
| 117 | + maxLines: 2, |
| 118 | + overflow: TextOverflow.ellipsis, |
| 119 | + style: TextStyle( |
| 120 | + color: Colors.grey.shade600, |
| 121 | + fontSize: 12, |
| 122 | + ), |
| 123 | + ), |
| 124 | + ], |
| 125 | + ), |
| 126 | + ), |
| 127 | + ), |
| 128 | + ), |
| 129 | + ], |
| 130 | + ), |
| 131 | + ), |
| 132 | + ), |
| 133 | + ), |
| 134 | + const SizedBox(height: 16), |
| 135 | + ClipRRect( |
| 136 | + borderRadius: BorderRadius.circular(12), |
| 137 | + child: icon == null |
| 138 | + ? Container( |
| 139 | + width: 48, |
| 140 | + height: 48, |
| 141 | + color: _defaultIconColor, |
| 142 | + child: const Icon(Icons.language, color: Colors.white), |
| 143 | + ) |
| 144 | + : Image.memory(icon!, width: 48, height: 48, fit: BoxFit.cover), |
| 145 | + ), |
| 146 | + const SizedBox(height: 4), |
| 147 | + SizedBox( |
| 148 | + width: 80, |
| 149 | + child: Text( |
| 150 | + title, |
| 151 | + key: const Key('preview-label'), |
| 152 | + textAlign: TextAlign.center, |
| 153 | + maxLines: 1, |
| 154 | + overflow: TextOverflow.ellipsis, |
| 155 | + style: Theme.of(context).textTheme.bodySmall, |
| 156 | + ), |
| 157 | + ), |
| 158 | + ], |
| 159 | + ); |
| 160 | + } |
| 161 | +} |
0 commit comments