Skip to content

Commit b73efe3

Browse files
devilAPIclaude
andcommitted
feat: live preview of the generated app screen and icon
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015N24yBvRNRonkZRTorXZTq
1 parent 2a3ffea commit b73efe3

5 files changed

Lines changed: 258 additions & 8 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Fill in the form and tap **Generate**:
2929
- **Color** - title-bar color
3030
- **Icon** - an image from your gallery/files (center-cropped to a square); without one the app gets the web2apk globe icon
3131
- **Fullscreen** - hides only the title bar; the Android status bar stays visible
32+
- **Preview** - a live sketch of the app screen and home-screen icon (beside the form on wide screens)
3233
- **Package** - shown as "Package: ... - tap to change". It is derived from the domain (`https://example.org` -> `org.example.web2apk`). Every app needs its own ID: two apps with the same ID replace each other, so change it if you build several apps for one domain.
3334

3435
When the APK is ready:

‎lib/src/ui/app_preview.dart‎

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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+
}

‎lib/src/ui/generator_page.dart‎

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import '../apk/signing_key.dart';
1111
import '../apk/template_contract.dart';
1212
import '../apk_actions.dart';
1313
import '../saved_apps.dart';
14+
import 'app_preview.dart';
1415
import 'key_page.dart';
1516
import 'my_apps_page.dart';
1617
import 'result_page.dart';
@@ -352,10 +353,17 @@ class _GeneratorPageState extends State<GeneratorPage> {
352353
),
353354
],
354355
),
355-
body: Center(
356-
child: ConstrainedBox(
357-
constraints: const BoxConstraints(maxWidth: 560),
358-
child: ListView(
356+
body: LayoutBuilder(
357+
builder: (context, constraints) {
358+
final wide = constraints.maxWidth >= 760;
359+
final preview = AppPreview(
360+
name: _name.text,
361+
url: _normalizedUrl,
362+
color: _color,
363+
fullscreen: _fullscreen,
364+
icon: _icon,
365+
);
366+
final form = ListView(
359367
padding: const EdgeInsets.all(16),
360368
children: [
361369
if (widget.showWebKeyNotice) ...[
@@ -427,7 +435,13 @@ class _GeneratorPageState extends State<GeneratorPage> {
427435
enabled: !busy,
428436
onTap: _editPackage,
429437
),
430-
const SizedBox(height: 16),
438+
if (!wide) ...[
439+
const SizedBox(height: 8),
440+
Text('Preview', style: Theme.of(context).textTheme.titleSmall),
441+
const SizedBox(height: 12),
442+
preview,
443+
],
444+
const SizedBox(height: 24),
431445
FilledButton(
432446
key: const Key('generate'),
433447
onPressed: _valid && !busy ? _generate : null,
@@ -447,8 +461,32 @@ class _GeneratorPageState extends State<GeneratorPage> {
447461
: const Text('Generate'),
448462
),
449463
],
450-
),
451-
),
464+
);
465+
if (!wide) {
466+
return Center(
467+
child: ConstrainedBox(
468+
constraints: const BoxConstraints(maxWidth: 560),
469+
child: form,
470+
),
471+
);
472+
}
473+
return Row(
474+
mainAxisAlignment: MainAxisAlignment.center,
475+
children: [
476+
Flexible(
477+
child: ConstrainedBox(
478+
constraints: const BoxConstraints(maxWidth: 560),
479+
child: form,
480+
),
481+
),
482+
const SizedBox(width: 8),
483+
SingleChildScrollView(
484+
padding: const EdgeInsets.all(16),
485+
child: preview,
486+
),
487+
],
488+
);
489+
},
452490
),
453491
);
454492
}

‎test/ui/app_preview_test.dart‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
import 'package:web2apk/src/ui/app_preview.dart';
4+
5+
void main() {
6+
Future<void> pump(
7+
WidgetTester tester, {
8+
String name = 'My Site',
9+
String? url = 'https://example.org/',
10+
Color color = const Color(0xFF2196F3),
11+
bool fullscreen = false,
12+
}) => tester.pumpWidget(
13+
MaterialApp(
14+
home: Scaffold(
15+
body: AppPreview(
16+
name: name,
17+
url: url,
18+
color: color,
19+
fullscreen: fullscreen,
20+
),
21+
),
22+
),
23+
);
24+
25+
testWidgets('shows title bar with the name, label and host', (tester) async {
26+
await pump(tester);
27+
expect(find.byKey(const Key('preview-title-bar')), findsOneWidget);
28+
expect(find.text('My Site'), findsNWidgets(2)); // title bar + label
29+
expect(find.text('example.org'), findsOneWidget);
30+
});
31+
32+
testWidgets('fullscreen hides only the title bar', (tester) async {
33+
await pump(tester, fullscreen: true);
34+
expect(find.byKey(const Key('preview-title-bar')), findsNothing);
35+
expect(find.byKey(const Key('preview-status-bar')), findsOneWidget);
36+
expect(find.text('My Site'), findsOneWidget); // label only
37+
});
38+
39+
testWidgets('placeholders while name and URL are empty', (tester) async {
40+
await pump(tester, name: ' ', url: null);
41+
expect(find.text('App name'), findsNWidgets(2));
42+
expect(find.text('Your website'), findsOneWidget);
43+
});
44+
45+
test('colors match the template', () {
46+
expect(titleColor(const Color(0xFF2196F3)), Colors.white);
47+
expect(titleColor(const Color(0xFFFFEB3B)), Colors.black);
48+
expect(statusBarColor(const Color(0xFF2196F3)), const Color(0xFF1A78C2));
49+
});
50+
}

‎test/ui/generator_page_test.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ void main() {
410410
await pumpWith(tester, pickIcon: () async => (bytes: png, name: 'ok.png'));
411411
await pickIconAndWait(tester);
412412
expect(find.text('ok.png'), findsOneWidget);
413-
expect(find.byType(Image), findsOneWidget);
413+
expect(find.byType(Image), findsNWidgets(2)); // icon tile + preview
414414
});
415415

416416
testWidgets('failing file picker shows an error instead of crashing', (

0 commit comments

Comments
 (0)