Claude commited on
Commit
2489e19
·
1 Parent(s): b57ecd3

Fix AC3: Generate dynamic cards from AI selection instead of hardcoded cards

Browse files

- Added _generate_cards_code() method to dynamically generate card widgets
- Added _StatsCard, _ActionCard, and _ProductCard Flutter widgets
- Modified _generate_complete_screen() to use AI-selected cards from variant
- Cards now personalized based on app_type (fitness, banking, rider, ecommerce, social)
- Replaced hardcoded "Feature 1/2/3" cards with contextual content

Files changed (1) hide show
  1. agents/flutter_screen_generator.py +448 -35
agents/flutter_screen_generator.py CHANGED
@@ -2610,6 +2610,153 @@ class _BackgroundPainter extends CustomPainter {{
2610
  print(f"⚠️ Unknown background ID '{bg_id}', using gradient_linear fallback")
2611
  return self._get_gradient_linear_painter(primary, secondary, accent)
2612
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2613
  def _generate_complete_screen(
2614
  self,
2615
  theme: Dict[str, Any],
@@ -2698,6 +2845,10 @@ class _BackgroundPainter extends CustomPainter {{
2698
  # Get footer code with theme data and text customizations
2699
  footer_code = self._get_footer_code(variant["footer"], theme_data, variant_texts)
2700
 
 
 
 
 
2701
  # Build complete Dart code
2702
  code = f'''import 'package:flutter/material.dart';
2703
 
@@ -2762,41 +2913,7 @@ class HomePage extends StatelessWidget {{
2762
  child: Column(
2763
  crossAxisAlignment: CrossAxisAlignment.start,
2764
  children: [
2765
- _ContentCard(
2766
- title: '{variant["name"]}',
2767
- subtitle: '{variant.get("description", "AI-generated variant")}',
2768
- accentColor: Color(0x{primary}),
2769
- bgColor: Color(0x{surface_color}),
2770
- textColor: Color(0x{text_primary}),
2771
- subtitleColor: Color(0x{text_secondary}),
2772
- borderRadius: {border_radius_medium}.0,
2773
- borderWidth: {border_width}.0,
2774
- isBold: {str(theme_style == "bold").lower()},
2775
- ),
2776
- SizedBox(height: {spacing_unit * 2}.0),
2777
- _ContentCard(
2778
- title: 'Feature 1',
2779
- subtitle: 'Primary content area',
2780
- accentColor: Color(0x{secondary}),
2781
- bgColor: Color(0x{surface_color}),
2782
- textColor: Color(0x{text_primary}),
2783
- subtitleColor: Color(0x{text_secondary}),
2784
- borderRadius: {border_radius_medium}.0,
2785
- borderWidth: {border_width}.0,
2786
- isBold: {str(theme_style == "bold").lower()},
2787
- ),
2788
- SizedBox(height: {spacing_unit * 2}.0),
2789
- _ContentCard(
2790
- title: 'Feature 2',
2791
- subtitle: 'Secondary content area',
2792
- accentColor: Color(0x{accent}),
2793
- bgColor: Color(0x{surface_color}),
2794
- textColor: Color(0x{text_primary}),
2795
- subtitleColor: Color(0x{text_secondary}),
2796
- borderRadius: {border_radius_medium}.0,
2797
- borderWidth: {border_width}.0,
2798
- isBold: {str(theme_style == "bold").lower()},
2799
- ),
2800
  ],
2801
  ),
2802
  ),
@@ -2919,6 +3036,302 @@ class _ContentCard extends StatelessWidget {{
2919
  }}
2920
  }}
2921
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2922
  // Helper widget for tab items (used in tabs header)
2923
  class _TabItem extends StatelessWidget {{
2924
  final String label;
 
2610
  print(f"⚠️ Unknown background ID '{bg_id}', using gradient_linear fallback")
2611
  return self._get_gradient_linear_painter(primary, secondary, accent)
2612
 
2613
+ def _generate_cards_code(
2614
+ self,
2615
+ cards: List[str],
2616
+ theme_data: Dict[str, Any],
2617
+ app_type: str
2618
+ ) -> str:
2619
+ """
2620
+ Generate Dart code for multiple cards based on AI selection
2621
+
2622
+ Args:
2623
+ cards: List of card IDs from variant (e.g., ["workout_summary", "step_counter"])
2624
+ theme_data: Theme configuration with colors, fonts, etc.
2625
+ app_type: App type for content personalization
2626
+
2627
+ Returns:
2628
+ Dart code string with all card widgets
2629
+ """
2630
+
2631
+ if not cards:
2632
+ # Fallback to generic cards if none specified
2633
+ cards = ["info_card", "action_card", "feature_card"]
2634
+
2635
+ # Extract theme values for easy access
2636
+ primary = theme_data["primary"]
2637
+ secondary = theme_data["secondary"]
2638
+ accent = theme_data["accent"]
2639
+ surface = theme_data["surface_color"]
2640
+ text_primary = theme_data["text_primary"]
2641
+ text_secondary = theme_data["text_secondary"]
2642
+ border_radius = theme_data["border_radius_medium"]
2643
+ spacing = theme_data["spacing_unit"]
2644
+ is_bold = theme_data["style"] == "bold"
2645
+
2646
+ cards_code_parts = []
2647
+
2648
+ # Content templates based on app type
2649
+ content_templates = {
2650
+ "fitness": {
2651
+ "titles": ["Today's Workout", "Activity Ring", "Steps", "Calories", "Heart Rate", "Progress"],
2652
+ "values": ["45 min", "85%", "8,420", "1,245 kcal", "72 bpm", "Week 3"],
2653
+ "subtitles": ["Strength training", "Daily goal", "of 10,000", "burned today", "resting", "5 day streak"]
2654
+ },
2655
+ "banking": {
2656
+ "titles": ["Account Balance", "Recent", "Transfer", "Investment", "Savings", "Budget"],
2657
+ "values": ["$12,450", "$-85.50", "$500", "+12.5%", "$5,200", "78%"],
2658
+ "subtitles": ["Main checking", "Starbucks Coffee", "Quick send", "This month", "Emergency fund", "Monthly limit"]
2659
+ },
2660
+ "rider": {
2661
+ "titles": ["Today's Earnings", "Deliveries", "Rating", "Distance", "Online Time", "Bonus"],
2662
+ "values": ["$145.50", "12", "4.8★", "45 km", "6.5 hrs", "+$25"],
2663
+ "subtitles": ["8 hours", "Completed", "368 reviews", "Today", "Active", "Weekend bonus"]
2664
+ },
2665
+ "ecommerce": {
2666
+ "titles": ["Flash Sale", "New Arrivals", "Top Rated", "Your Cart", "Wishlist", "Deals"],
2667
+ "values": ["50% OFF", "Just In", "4.9★", "3 items", "12", "Limited"],
2668
+ "subtitles": ["Ends in 2h", "Check now", "Bestseller", "$127.50", "Saved items", "Today only"]
2669
+ },
2670
+ "social": {
2671
+ "titles": ["Your Story", "Messages", "Notifications", "Followers", "Engagement", "Trending"],
2672
+ "values": ["+24", "5 new", "12", "1,245", "85%", "#Topic"],
2673
+ "subtitles": ["Views today", "Unread", "New today", "This week", "Up 15%", "Popular now"]
2674
+ }
2675
+ }
2676
+
2677
+ templates = content_templates.get(app_type, content_templates["fitness"])
2678
+
2679
+ for idx, card_id in enumerate(cards):
2680
+ # Get template content (cycle through if more cards than templates)
2681
+ template_idx = idx % len(templates["titles"])
2682
+ title = templates["titles"][template_idx]
2683
+ value = templates["values"][template_idx]
2684
+ subtitle = templates["subtitles"][template_idx]
2685
+
2686
+ # Determine accent color (rotate through theme colors)
2687
+ if idx % 3 == 0:
2688
+ accent_color = primary
2689
+ elif idx % 3 == 1:
2690
+ accent_color = secondary
2691
+ else:
2692
+ accent_color = accent
2693
+
2694
+ # Generate card widget based on catalog type
2695
+ card_info = self.CARD_CATALOG.get(card_id, {})
2696
+ card_complexity = card_info.get("complexity", "simple")
2697
+
2698
+ # Generate appropriate card based on complexity and type
2699
+ if "stats" in card_id or "metric" in card_id or card_id in ["step_counter", "calorie_tracker", "heart_rate", "account_balance"]:
2700
+ # Stats card with large number
2701
+ card_code = f"""_StatsCard(
2702
+ title: '{title}',
2703
+ value: '{value}',
2704
+ subtitle: '{subtitle}',
2705
+ accentColor: Color(0x{accent_color}),
2706
+ bgColor: Color(0x{surface}),
2707
+ textColor: Color(0x{text_primary}),
2708
+ subtitleColor: Color(0x{text_secondary}),
2709
+ borderRadius: {border_radius}.0,
2710
+ isBold: {str(is_bold).lower()},
2711
+ )"""
2712
+ elif "workout" in card_id or "exercise" in card_id or card_id in ["delivery_order", "order_item"]:
2713
+ # Action card with progress/status
2714
+ card_code = f"""_ActionCard(
2715
+ title: '{title}',
2716
+ status: '{value}',
2717
+ description: '{subtitle}',
2718
+ accentColor: Color(0x{accent_color}),
2719
+ bgColor: Color(0x{surface}),
2720
+ textColor: Color(0x{text_primary}),
2721
+ subtitleColor: Color(0x{text_secondary}),
2722
+ borderRadius: {border_radius}.0,
2723
+ isBold: {str(is_bold).lower()},
2724
+ )"""
2725
+ elif "product" in card_id or "item" in card_id or card_id in ["flash_sale", "wishlist_item"]:
2726
+ # Product/item card
2727
+ card_code = f"""_ProductCard(
2728
+ name: '{title}',
2729
+ price: '{value}',
2730
+ tag: '{subtitle}',
2731
+ accentColor: Color(0x{accent_color}),
2732
+ bgColor: Color(0x{surface}),
2733
+ textColor: Color(0x{text_primary}),
2734
+ subtitleColor: Color(0x{text_secondary}),
2735
+ borderRadius: {border_radius}.0,
2736
+ isBold: {str(is_bold).lower()},
2737
+ )"""
2738
+ else:
2739
+ # Generic content card (fallback)
2740
+ card_code = f"""_ContentCard(
2741
+ title: '{title}',
2742
+ subtitle: '{subtitle}',
2743
+ accentColor: Color(0x{accent_color}),
2744
+ bgColor: Color(0x{surface}),
2745
+ textColor: Color(0x{text_primary}),
2746
+ subtitleColor: Color(0x{text_secondary}),
2747
+ borderRadius: {border_radius}.0,
2748
+ borderWidth: 2.0,
2749
+ isBold: {str(is_bold).lower()},
2750
+ )"""
2751
+
2752
+ cards_code_parts.append(card_code)
2753
+
2754
+ # Join all cards with spacing
2755
+ cards_with_spacing = f",\n SizedBox(height: {spacing * 2}.0),\n ".join(cards_code_parts)
2756
+
2757
+ return cards_with_spacing
2758
+
2759
+
2760
  def _generate_complete_screen(
2761
  self,
2762
  theme: Dict[str, Any],
 
2845
  # Get footer code with theme data and text customizations
2846
  footer_code = self._get_footer_code(variant["footer"], theme_data, variant_texts)
2847
 
2848
+ # Generate dynamic cards from AI-selected card types
2849
+ cards_list = variant.get("cards", [])
2850
+ cards_code = self._generate_cards_code(cards_list, theme_data, app_type)
2851
+
2852
  # Build complete Dart code
2853
  code = f'''import 'package:flutter/material.dart';
2854
 
 
2913
  child: Column(
2914
  crossAxisAlignment: CrossAxisAlignment.start,
2915
  children: [
2916
+ {cards_code}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2917
  ],
2918
  ),
2919
  ),
 
3036
  }}
3037
  }}
3038
 
3039
+ // Stats Card Widget (for metrics and numbers)
3040
+ class _StatsCard extends StatelessWidget {{
3041
+ final String title;
3042
+ final String value;
3043
+ final String subtitle;
3044
+ final Color accentColor;
3045
+ final Color bgColor;
3046
+ final Color textColor;
3047
+ final Color subtitleColor;
3048
+ final double borderRadius;
3049
+ final bool isBold;
3050
+
3051
+ const _StatsCard({{
3052
+ required this.title,
3053
+ required this.value,
3054
+ required this.subtitle,
3055
+ required this.accentColor,
3056
+ required this.bgColor,
3057
+ required this.textColor,
3058
+ required this.subtitleColor,
3059
+ required this.borderRadius,
3060
+ required this.isBold,
3061
+ }});
3062
+
3063
+ @override
3064
+ Widget build(BuildContext context) {{
3065
+ return Container(
3066
+ padding: const EdgeInsets.all(20),
3067
+ decoration: BoxDecoration(
3068
+ color: bgColor,
3069
+ borderRadius: BorderRadius.circular(borderRadius),
3070
+ border: isBold ? Border.all(
3071
+ color: accentColor.withOpacity(0.3),
3072
+ width: 2,
3073
+ ) : null,
3074
+ boxShadow: [
3075
+ BoxShadow(
3076
+ color: isBold ? accentColor.withOpacity(0.2) : Colors.black.withOpacity(0.1),
3077
+ blurRadius: isBold ? 12 : 8,
3078
+ offset: Offset(0, isBold ? 6 : 4),
3079
+ ),
3080
+ ],
3081
+ ),
3082
+ child: Column(
3083
+ crossAxisAlignment: CrossAxisAlignment.start,
3084
+ children: [
3085
+ Text(
3086
+ title,
3087
+ style: TextStyle(
3088
+ fontSize: 14,
3089
+ color: subtitleColor,
3090
+ fontWeight: FontWeight.w500,
3091
+ ),
3092
+ ),
3093
+ const SizedBox(height: 8),
3094
+ Text(
3095
+ value,
3096
+ style: TextStyle(
3097
+ fontSize: isBold ? 36 : 32,
3098
+ fontWeight: isBold ? FontWeight.w800 : FontWeight.w700,
3099
+ color: accentColor,
3100
+ ),
3101
+ ),
3102
+ const SizedBox(height: 4),
3103
+ Text(
3104
+ subtitle,
3105
+ style: TextStyle(
3106
+ fontSize: 13,
3107
+ color: subtitleColor.withOpacity(0.7),
3108
+ ),
3109
+ ),
3110
+ ],
3111
+ ),
3112
+ );
3113
+ }}
3114
+ }}
3115
+
3116
+ // Action Card Widget (for actions with status)
3117
+ class _ActionCard extends StatelessWidget {{
3118
+ final String title;
3119
+ final String status;
3120
+ final String description;
3121
+ final Color accentColor;
3122
+ final Color bgColor;
3123
+ final Color textColor;
3124
+ final Color subtitleColor;
3125
+ final double borderRadius;
3126
+ final bool isBold;
3127
+
3128
+ const _ActionCard({{
3129
+ required this.title,
3130
+ required this.status,
3131
+ required this.description,
3132
+ required this.accentColor,
3133
+ required this.bgColor,
3134
+ required this.textColor,
3135
+ required this.subtitleColor,
3136
+ required this.borderRadius,
3137
+ required this.isBold,
3138
+ }});
3139
+
3140
+ @override
3141
+ Widget build(BuildContext context) {{
3142
+ return Container(
3143
+ padding: const EdgeInsets.all(18),
3144
+ decoration: BoxDecoration(
3145
+ color: bgColor,
3146
+ borderRadius: BorderRadius.circular(borderRadius),
3147
+ border: isBold ? Border.all(
3148
+ color: accentColor.withOpacity(0.3),
3149
+ width: 2,
3150
+ ) : null,
3151
+ boxShadow: [
3152
+ BoxShadow(
3153
+ color: isBold ? accentColor.withOpacity(0.2) : Colors.black.withOpacity(0.1),
3154
+ blurRadius: isBold ? 12 : 8,
3155
+ offset: Offset(0, isBold ? 6 : 4),
3156
+ ),
3157
+ ],
3158
+ ),
3159
+ child: Row(
3160
+ children: [
3161
+ Container(
3162
+ padding: const EdgeInsets.all(12),
3163
+ decoration: BoxDecoration(
3164
+ color: accentColor.withOpacity(0.15),
3165
+ borderRadius: BorderRadius.circular(12),
3166
+ ),
3167
+ child: Icon(
3168
+ Icons.flash_on_rounded,
3169
+ color: accentColor,
3170
+ size: 24,
3171
+ ),
3172
+ ),
3173
+ const SizedBox(width: 16),
3174
+ Expanded(
3175
+ child: Column(
3176
+ crossAxisAlignment: CrossAxisAlignment.start,
3177
+ children: [
3178
+ Text(
3179
+ title,
3180
+ style: TextStyle(
3181
+ fontSize: isBold ? 17 : 16,
3182
+ fontWeight: isBold ? FontWeight.w700 : FontWeight.w600,
3183
+ color: textColor,
3184
+ ),
3185
+ ),
3186
+ const SizedBox(height: 4),
3187
+ Text(
3188
+ description,
3189
+ style: TextStyle(
3190
+ fontSize: 13,
3191
+ color: subtitleColor,
3192
+ ),
3193
+ ),
3194
+ ],
3195
+ ),
3196
+ ),
3197
+ Container(
3198
+ padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
3199
+ decoration: BoxDecoration(
3200
+ color: accentColor,
3201
+ borderRadius: BorderRadius.circular(20),
3202
+ ),
3203
+ child: Text(
3204
+ status,
3205
+ style: const TextStyle(
3206
+ fontSize: 12,
3207
+ fontWeight: FontWeight.w600,
3208
+ color: Colors.white,
3209
+ ),
3210
+ ),
3211
+ ),
3212
+ ],
3213
+ ),
3214
+ );
3215
+ }}
3216
+ }}
3217
+
3218
+ // Product Card Widget (for items and products)
3219
+ class _ProductCard extends StatelessWidget {{
3220
+ final String name;
3221
+ final String price;
3222
+ final String tag;
3223
+ final Color accentColor;
3224
+ final Color bgColor;
3225
+ final Color textColor;
3226
+ final Color subtitleColor;
3227
+ final double borderRadius;
3228
+ final bool isBold;
3229
+
3230
+ const _ProductCard({{
3231
+ required this.name,
3232
+ required this.price,
3233
+ required this.tag,
3234
+ required this.accentColor,
3235
+ required this.bgColor,
3236
+ required this.textColor,
3237
+ required this.subtitleColor,
3238
+ required this.borderRadius,
3239
+ required this.isBold,
3240
+ }});
3241
+
3242
+ @override
3243
+ Widget build(BuildContext context) {{
3244
+ return Container(
3245
+ decoration: BoxDecoration(
3246
+ color: bgColor,
3247
+ borderRadius: BorderRadius.circular(borderRadius),
3248
+ border: isBold ? Border.all(
3249
+ color: accentColor.withOpacity(0.3),
3250
+ width: 2,
3251
+ ) : null,
3252
+ boxShadow: [
3253
+ BoxShadow(
3254
+ color: isBold ? accentColor.withOpacity(0.2) : Colors.black.withOpacity(0.1),
3255
+ blurRadius: isBold ? 12 : 8,
3256
+ offset: Offset(0, isBold ? 6 : 4),
3257
+ ),
3258
+ ],
3259
+ ),
3260
+ child: Column(
3261
+ crossAxisAlignment: CrossAxisAlignment.start,
3262
+ children: [
3263
+ Container(
3264
+ height: 140,
3265
+ decoration: BoxDecoration(
3266
+ gradient: LinearGradient(
3267
+ colors: [accentColor.withOpacity(0.3), accentColor.withOpacity(0.1)],
3268
+ begin: Alignment.topLeft,
3269
+ end: Alignment.bottomRight,
3270
+ ),
3271
+ borderRadius: BorderRadius.vertical(top: Radius.circular(borderRadius)),
3272
+ ),
3273
+ child: Center(
3274
+ child: Icon(
3275
+ Icons.shopping_bag_rounded,
3276
+ size: 48,
3277
+ color: accentColor,
3278
+ ),
3279
+ ),
3280
+ ),
3281
+ Padding(
3282
+ padding: const EdgeInsets.all(16),
3283
+ child: Column(
3284
+ crossAxisAlignment: CrossAxisAlignment.start,
3285
+ children: [
3286
+ Row(
3287
+ mainAxisAlignment: MainAxisAlignment.spaceBetween,
3288
+ children: [
3289
+ Expanded(
3290
+ child: Text(
3291
+ name,
3292
+ style: TextStyle(
3293
+ fontSize: isBold ? 17 : 16,
3294
+ fontWeight: isBold ? FontWeight.w700 : FontWeight.w600,
3295
+ color: textColor,
3296
+ ),
3297
+ ),
3298
+ ),
3299
+ Text(
3300
+ price,
3301
+ style: TextStyle(
3302
+ fontSize: isBold ? 18 : 16,
3303
+ fontWeight: FontWeight.w700,
3304
+ color: accentColor,
3305
+ ),
3306
+ ),
3307
+ ],
3308
+ ),
3309
+ const SizedBox(height: 6),
3310
+ Container(
3311
+ padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
3312
+ decoration: BoxDecoration(
3313
+ color: accentColor.withOpacity(0.15),
3314
+ borderRadius: BorderRadius.circular(6),
3315
+ ),
3316
+ child: Text(
3317
+ tag,
3318
+ style: TextStyle(
3319
+ fontSize: 11,
3320
+ fontWeight: FontWeight.w600,
3321
+ color: accentColor,
3322
+ ),
3323
+ ),
3324
+ ),
3325
+ ],
3326
+ ),
3327
+ ),
3328
+ ],
3329
+ ),
3330
+ );
3331
+ }}
3332
+ }}
3333
+
3334
+
3335
  // Helper widget for tab items (used in tabs header)
3336
  class _TabItem extends StatelessWidget {{
3337
  final String label;