Spaces:
Sleeping
Sleeping
Commit
·
f9485f1
1
Parent(s):
9b90c30
Demo version
Browse files
app.py
CHANGED
|
@@ -6,20 +6,19 @@ from datetime import datetime
|
|
| 6 |
import requests
|
| 7 |
|
| 8 |
# Hugging Face API configuration
|
| 9 |
-
HF_API_URL = "https://api-inference.huggingface.co/models/
|
| 10 |
HF_TOKEN = os.environ.get("HF_TOKEN") # Will be set in Hugging Face Spaces
|
| 11 |
|
| 12 |
-
def generate_code_with_ai(prompt, max_length=
|
| 13 |
-
"""Generate code using the Hugging Face Inference API."""
|
| 14 |
try:
|
| 15 |
-
# Create a more specific prompt for Flutter/Dart code
|
| 16 |
-
full_prompt = f"""
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
- Error handling where appropriate
|
| 23 |
|
| 24 |
```dart
|
| 25 |
"""
|
|
@@ -30,14 +29,16 @@ Generate proper Dart classes, methods, and Flutter widgets. Focus on:
|
|
| 30 |
"inputs": full_prompt,
|
| 31 |
"parameters": {
|
| 32 |
"max_length": max_length,
|
| 33 |
-
"temperature": 0.
|
| 34 |
"do_sample": True,
|
| 35 |
"return_full_text": False,
|
| 36 |
"num_return_sequences": 1,
|
|
|
|
|
|
|
| 37 |
}
|
| 38 |
}
|
| 39 |
|
| 40 |
-
response = requests.post(HF_API_URL, headers=headers, json=payload)
|
| 41 |
response.raise_for_status()
|
| 42 |
|
| 43 |
result = response.json()
|
|
@@ -59,6 +60,9 @@ Generate proper Dart classes, methods, and Flutter widgets. Focus on:
|
|
| 59 |
# Remove any remaining markdown formatting
|
| 60 |
code = code.replace("```", "").strip()
|
| 61 |
|
|
|
|
|
|
|
|
|
|
| 62 |
return code if code else "// AI-generated placeholder code"
|
| 63 |
|
| 64 |
except Exception as e:
|
|
@@ -150,21 +154,23 @@ def generate_flutter_code(description):
|
|
| 150 |
|
| 151 |
|
| 152 |
def generate_main_dart(description):
|
| 153 |
-
"""Generate the main.dart file content using AI."""
|
| 154 |
-
prompt = f"""
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
```dart
|
| 163 |
import 'package:flutter/material.dart';
|
| 164 |
|
| 165 |
-
void main()
|
| 166 |
-
runApp(const MyApp());
|
| 167 |
-
}}
|
| 168 |
|
| 169 |
class MyApp extends StatelessWidget {{
|
| 170 |
const MyApp({{super.key}});
|
|
@@ -172,35 +178,27 @@ class MyApp extends StatelessWidget {{
|
|
| 172 |
@override
|
| 173 |
Widget build(BuildContext context) {{
|
| 174 |
return MaterialApp(
|
| 175 |
-
title: '
|
| 176 |
-
theme: ThemeData(
|
| 177 |
-
primarySwatch: Colors.blue,
|
| 178 |
-
useMaterial3: true,
|
| 179 |
-
),
|
| 180 |
home: const HomePage(),
|
| 181 |
);
|
| 182 |
}}
|
| 183 |
}}
|
| 184 |
|
| 185 |
-
class HomePage extends
|
| 186 |
const HomePage({{super.key}});
|
| 187 |
|
| 188 |
-
@override
|
| 189 |
-
State<HomePage> createState() => _HomePageState();
|
| 190 |
-
}}
|
| 191 |
-
|
| 192 |
-
class _HomePageState extends State<HomePage> {{
|
| 193 |
@override
|
| 194 |
Widget build(BuildContext context) {{
|
| 195 |
return Scaffold(
|
| 196 |
appBar: AppBar(title: const Text('Home')),
|
| 197 |
-
body: const Center(child: Text('
|
| 198 |
);
|
| 199 |
}}
|
| 200 |
}}
|
| 201 |
```"""
|
| 202 |
|
| 203 |
-
ai_generated_code = generate_code_with_ai(prompt, max_length=
|
| 204 |
|
| 205 |
# Add header comment
|
| 206 |
header = f"""// AI-Generated Flutter Application
|
|
@@ -214,80 +212,78 @@ class _HomePageState extends State<HomePage> {{
|
|
| 214 |
|
| 215 |
|
| 216 |
def generate_widgets_dart(description):
|
| 217 |
-
"""Generate the widgets.dart file content using AI."""
|
| 218 |
-
prompt = f"""
|
|
|
|
|
|
|
| 219 |
|
| 220 |
-
|
| 221 |
-
-
|
| 222 |
-
- A
|
| 223 |
-
|
|
|
|
|
|
|
|
|
|
| 224 |
- Follow Material Design guidelines
|
| 225 |
-
- Include proper
|
|
|
|
| 226 |
|
|
|
|
| 227 |
```dart
|
| 228 |
import 'package:flutter/material.dart';
|
| 229 |
|
| 230 |
-
/// Custom info card widget
|
| 231 |
class CustomInfoCard extends StatelessWidget {{
|
| 232 |
final String title;
|
| 233 |
final String description;
|
| 234 |
final IconData? icon;
|
|
|
|
| 235 |
|
| 236 |
const CustomInfoCard({{
|
| 237 |
super.key,
|
| 238 |
required this.title,
|
| 239 |
required this.description,
|
| 240 |
this.icon,
|
|
|
|
| 241 |
}});
|
| 242 |
|
| 243 |
@override
|
| 244 |
Widget build(BuildContext context) {{
|
| 245 |
return Card(
|
| 246 |
elevation: 4,
|
|
|
|
| 247 |
margin: const EdgeInsets.all(16),
|
| 248 |
child: Padding(
|
| 249 |
padding: const EdgeInsets.all(20),
|
| 250 |
child: Column(
|
| 251 |
mainAxisSize: MainAxisSize.min,
|
| 252 |
children: [
|
| 253 |
-
if (icon != null)
|
| 254 |
-
|
| 255 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
const SizedBox(height: 8),
|
| 257 |
-
Text(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
],
|
| 259 |
),
|
| 260 |
),
|
| 261 |
);
|
| 262 |
}}
|
| 263 |
}}
|
| 264 |
-
|
| 265 |
-
/// Custom button with loading state
|
| 266 |
-
class CustomButton extends StatelessWidget {{
|
| 267 |
-
final String text;
|
| 268 |
-
final VoidCallback? onPressed;
|
| 269 |
-
final bool isLoading;
|
| 270 |
-
|
| 271 |
-
const CustomButton({{
|
| 272 |
-
super.key,
|
| 273 |
-
required this.text,
|
| 274 |
-
this.onPressed,
|
| 275 |
-
this.isLoading = false,
|
| 276 |
-
}});
|
| 277 |
-
|
| 278 |
-
@override
|
| 279 |
-
Widget build(BuildContext context) {{
|
| 280 |
-
return ElevatedButton(
|
| 281 |
-
onPressed: isLoading ? null : onPressed,
|
| 282 |
-
child: isLoading
|
| 283 |
-
? const CircularProgressIndicator()
|
| 284 |
-
: Text(text),
|
| 285 |
-
);
|
| 286 |
-
}}
|
| 287 |
-
}}
|
| 288 |
```"""
|
| 289 |
|
| 290 |
-
ai_generated_code = generate_code_with_ai(prompt, max_length=
|
| 291 |
|
| 292 |
# Add header comment
|
| 293 |
header = f"""// Custom Widgets for the Flutter Application
|
|
@@ -301,91 +297,91 @@ class CustomButton extends StatelessWidget {{
|
|
| 301 |
|
| 302 |
|
| 303 |
def generate_models_dart(description):
|
| 304 |
-
"""Generate the models.dart file content using AI."""
|
| 305 |
-
prompt = f"""
|
| 306 |
-
|
| 307 |
-
Create data models with:
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
```dart
|
| 315 |
-
///
|
| 316 |
class Item {{
|
| 317 |
final String id;
|
| 318 |
final String title;
|
| 319 |
final String description;
|
| 320 |
final DateTime createdAt;
|
|
|
|
| 321 |
final bool isCompleted;
|
|
|
|
| 322 |
|
| 323 |
const Item({{
|
| 324 |
required this.id,
|
| 325 |
required this.title,
|
| 326 |
required this.description,
|
| 327 |
required this.createdAt,
|
|
|
|
| 328 |
this.isCompleted = false,
|
|
|
|
| 329 |
}});
|
| 330 |
|
|
|
|
| 331 |
Map<String, dynamic> toJson() => {{
|
| 332 |
'id': id,
|
| 333 |
'title': title,
|
| 334 |
'description': description,
|
| 335 |
'createdAt': createdAt.toIso8601String(),
|
|
|
|
| 336 |
'isCompleted': isCompleted,
|
|
|
|
| 337 |
}};
|
| 338 |
|
|
|
|
| 339 |
factory Item.fromJson(Map<String, dynamic> json) => Item(
|
| 340 |
-
id: json['id'],
|
| 341 |
-
title: json['title'],
|
| 342 |
-
description: json['description'],
|
| 343 |
-
createdAt: DateTime.
|
| 344 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 345 |
);
|
| 346 |
|
|
|
|
| 347 |
Item copyWith({{
|
| 348 |
String? id,
|
| 349 |
String? title,
|
| 350 |
String? description,
|
| 351 |
DateTime? createdAt,
|
|
|
|
| 352 |
bool? isCompleted,
|
|
|
|
| 353 |
}}) => Item(
|
| 354 |
id: id ?? this.id,
|
| 355 |
title: title ?? this.title,
|
| 356 |
description: description ?? this.description,
|
| 357 |
createdAt: createdAt ?? this.createdAt,
|
|
|
|
| 358 |
isCompleted: isCompleted ?? this.isCompleted,
|
| 359 |
-
|
| 360 |
-
}}
|
| 361 |
-
|
| 362 |
-
/// User model
|
| 363 |
-
class User {{
|
| 364 |
-
final String id;
|
| 365 |
-
final String name;
|
| 366 |
-
final String email;
|
| 367 |
-
|
| 368 |
-
const User({{
|
| 369 |
-
required this.id,
|
| 370 |
-
required this.name,
|
| 371 |
-
required this.email,
|
| 372 |
-
}});
|
| 373 |
-
|
| 374 |
-
Map<String, dynamic> toJson() => {{
|
| 375 |
-
'id': id,
|
| 376 |
-
'name': name,
|
| 377 |
-
'email': email,
|
| 378 |
-
}};
|
| 379 |
-
|
| 380 |
-
factory User.fromJson(Map<String, dynamic> json) => User(
|
| 381 |
-
id: json['id'],
|
| 382 |
-
name: json['name'],
|
| 383 |
-
email: json['email'],
|
| 384 |
);
|
| 385 |
}}
|
| 386 |
```"""
|
| 387 |
|
| 388 |
-
ai_generated_code = generate_code_with_ai(prompt, max_length=
|
| 389 |
|
| 390 |
# Add header comment
|
| 391 |
header = f"""// Data Models for the Flutter Application
|
|
@@ -399,30 +395,46 @@ class User {{
|
|
| 399 |
|
| 400 |
|
| 401 |
def generate_services_dart(description):
|
| 402 |
-
"""Generate the services.dart file content using AI."""
|
| 403 |
-
prompt = f"""
|
| 404 |
-
|
| 405 |
-
Create service classes with:
|
| 406 |
-
-
|
| 407 |
-
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 412 |
```dart
|
|
|
|
| 413 |
import 'models.dart';
|
| 414 |
|
| 415 |
-
///
|
| 416 |
class DataService {{
|
| 417 |
final List<Item> _items = [];
|
| 418 |
|
| 419 |
-
/// Get all items
|
| 420 |
List<Item> getAllItems() => List.unmodifiable(_items);
|
| 421 |
|
| 422 |
-
///
|
| 423 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 424 |
|
| 425 |
-
/// Update existing item
|
| 426 |
bool updateItem(String id, Item updatedItem) {{
|
| 427 |
final index = _items.indexWhere((item) => item.id == id);
|
| 428 |
if (index != -1) {{
|
|
@@ -432,7 +444,7 @@ class DataService {{
|
|
| 432 |
return false;
|
| 433 |
}}
|
| 434 |
|
| 435 |
-
///
|
| 436 |
bool deleteItem(String id) {{
|
| 437 |
final index = _items.indexWhere((item) => item.id == id);
|
| 438 |
if (index != -1) {{
|
|
@@ -442,34 +454,62 @@ class DataService {{
|
|
| 442 |
return false;
|
| 443 |
}}
|
| 444 |
|
| 445 |
-
/// Toggle completion status
|
| 446 |
-
bool
|
| 447 |
final index = _items.indexWhere((item) => item.id == id);
|
| 448 |
if (index != -1) {{
|
| 449 |
-
_items[index]
|
|
|
|
|
|
|
|
|
|
| 450 |
return true;
|
| 451 |
}}
|
| 452 |
return false;
|
| 453 |
}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 454 |
}}
|
| 455 |
|
| 456 |
-
///
|
| 457 |
class ValidationService {{
|
|
|
|
| 458 |
static bool isValidEmail(String email) {{
|
| 459 |
-
|
|
|
|
|
|
|
|
|
|
| 460 |
}}
|
| 461 |
|
|
|
|
| 462 |
static bool isNotEmpty(String? value) {{
|
| 463 |
return value != null && value.trim().isNotEmpty;
|
| 464 |
}}
|
| 465 |
|
|
|
|
| 466 |
static bool hasMinLength(String value, int minLength) {{
|
| 467 |
return value.length >= minLength;
|
| 468 |
}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 469 |
}}
|
| 470 |
```"""
|
| 471 |
|
| 472 |
-
ai_generated_code = generate_code_with_ai(prompt, max_length=
|
| 473 |
|
| 474 |
# Add header comment
|
| 475 |
header = f"""// Business Logic and Services
|
|
@@ -715,7 +755,7 @@ def create_interface():
|
|
| 715 |
- **README.md** - Setup and usage instructions
|
| 716 |
|
| 717 |
### ⚠️ Important Notes:
|
| 718 |
-
- This uses
|
| 719 |
- The AI generates Flutter/Dart code based on your description
|
| 720 |
- Always review and test generated code before production use
|
| 721 |
- The AI service may occasionally be slow or unavailable - try again if needed
|
|
|
|
| 6 |
import requests
|
| 7 |
|
| 8 |
# Hugging Face API configuration
|
| 9 |
+
HF_API_URL = "https://api-inference.huggingface.co/models/Qwen/Qwen3-0.6B"
|
| 10 |
HF_TOKEN = os.environ.get("HF_TOKEN") # Will be set in Hugging Face Spaces
|
| 11 |
|
| 12 |
+
def generate_code_with_ai(prompt, max_length=800):
|
| 13 |
+
"""Generate code using the Hugging Face Inference API with Qwen model."""
|
| 14 |
try:
|
| 15 |
+
# Create a more specific prompt for Flutter/Dart code using Qwen's strengths
|
| 16 |
+
full_prompt = f"""You are an expert Flutter developer. Generate clean, working Dart code for this requirement:
|
| 17 |
|
| 18 |
+
{prompt}
|
| 19 |
+
|
| 20 |
+
IMPORTANT: Provide ONLY the Dart code without any explanations or markdown formatting.
|
| 21 |
+
The code should be complete, functional, and follow Flutter best practices.
|
|
|
|
| 22 |
|
| 23 |
```dart
|
| 24 |
"""
|
|
|
|
| 29 |
"inputs": full_prompt,
|
| 30 |
"parameters": {
|
| 31 |
"max_length": max_length,
|
| 32 |
+
"temperature": 0.3, # Lower temperature for more focused code generation
|
| 33 |
"do_sample": True,
|
| 34 |
"return_full_text": False,
|
| 35 |
"num_return_sequences": 1,
|
| 36 |
+
"top_p": 0.9, # Better sampling for code
|
| 37 |
+
"repetition_penalty": 1.1, # Reduce repetition
|
| 38 |
}
|
| 39 |
}
|
| 40 |
|
| 41 |
+
response = requests.post(HF_API_URL, headers=headers, json=payload, timeout=30)
|
| 42 |
response.raise_for_status()
|
| 43 |
|
| 44 |
result = response.json()
|
|
|
|
| 60 |
# Remove any remaining markdown formatting
|
| 61 |
code = code.replace("```", "").strip()
|
| 62 |
|
| 63 |
+
# Additional cleanup for common issues
|
| 64 |
+
code = code.replace("dart\n", "").replace("Dart\n", "")
|
| 65 |
+
|
| 66 |
return code if code else "// AI-generated placeholder code"
|
| 67 |
|
| 68 |
except Exception as e:
|
|
|
|
| 154 |
|
| 155 |
|
| 156 |
def generate_main_dart(description):
|
| 157 |
+
"""Generate the main.dart file content using Qwen AI."""
|
| 158 |
+
prompt = f"""Generate a complete Flutter main.dart file for: {description}
|
| 159 |
+
|
| 160 |
+
Requirements:
|
| 161 |
+
1. Import necessary packages (flutter/material.dart)
|
| 162 |
+
2. Create main() function
|
| 163 |
+
3. Create MyApp widget extending StatelessWidget
|
| 164 |
+
4. Include MaterialApp with proper theme
|
| 165 |
+
5. Create a basic HomePage widget
|
| 166 |
+
6. Include app bar and basic layout
|
| 167 |
+
7. Use modern Flutter syntax with const constructors
|
| 168 |
+
|
| 169 |
+
Code structure:
|
| 170 |
```dart
|
| 171 |
import 'package:flutter/material.dart';
|
| 172 |
|
| 173 |
+
void main() => runApp(const MyApp());
|
|
|
|
|
|
|
| 174 |
|
| 175 |
class MyApp extends StatelessWidget {{
|
| 176 |
const MyApp({{super.key}});
|
|
|
|
| 178 |
@override
|
| 179 |
Widget build(BuildContext context) {{
|
| 180 |
return MaterialApp(
|
| 181 |
+
title: 'Generated App',
|
| 182 |
+
theme: ThemeData(primarySwatch: Colors.blue, useMaterial3: true),
|
|
|
|
|
|
|
|
|
|
| 183 |
home: const HomePage(),
|
| 184 |
);
|
| 185 |
}}
|
| 186 |
}}
|
| 187 |
|
| 188 |
+
class HomePage extends StatelessWidget {{
|
| 189 |
const HomePage({{super.key}});
|
| 190 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
@override
|
| 192 |
Widget build(BuildContext context) {{
|
| 193 |
return Scaffold(
|
| 194 |
appBar: AppBar(title: const Text('Home')),
|
| 195 |
+
body: const Center(child: Text('Welcome to your app!')),
|
| 196 |
);
|
| 197 |
}}
|
| 198 |
}}
|
| 199 |
```"""
|
| 200 |
|
| 201 |
+
ai_generated_code = generate_code_with_ai(prompt, max_length=1000)
|
| 202 |
|
| 203 |
# Add header comment
|
| 204 |
header = f"""// AI-Generated Flutter Application
|
|
|
|
| 212 |
|
| 213 |
|
| 214 |
def generate_widgets_dart(description):
|
| 215 |
+
"""Generate the widgets.dart file content using Qwen AI."""
|
| 216 |
+
prompt = f"""Generate Flutter custom widgets for: {description}
|
| 217 |
+
|
| 218 |
+
Create at least 3 reusable widgets with proper documentation:
|
| 219 |
|
| 220 |
+
1. CustomInfoCard - A styled card widget for displaying information
|
| 221 |
+
2. CustomButton - An enhanced button with loading states
|
| 222 |
+
3. CustomTextField - A styled text input field
|
| 223 |
+
|
| 224 |
+
Requirements:
|
| 225 |
+
- Use proper Dart documentation comments (///)
|
| 226 |
+
- Include null safety and const constructors
|
| 227 |
- Follow Material Design guidelines
|
| 228 |
+
- Include proper parameter validation
|
| 229 |
+
- Use modern Flutter patterns
|
| 230 |
|
| 231 |
+
Example structure:
|
| 232 |
```dart
|
| 233 |
import 'package:flutter/material.dart';
|
| 234 |
|
| 235 |
+
/// Custom info card widget for displaying content
|
| 236 |
class CustomInfoCard extends StatelessWidget {{
|
| 237 |
final String title;
|
| 238 |
final String description;
|
| 239 |
final IconData? icon;
|
| 240 |
+
final Color? backgroundColor;
|
| 241 |
|
| 242 |
const CustomInfoCard({{
|
| 243 |
super.key,
|
| 244 |
required this.title,
|
| 245 |
required this.description,
|
| 246 |
this.icon,
|
| 247 |
+
this.backgroundColor,
|
| 248 |
}});
|
| 249 |
|
| 250 |
@override
|
| 251 |
Widget build(BuildContext context) {{
|
| 252 |
return Card(
|
| 253 |
elevation: 4,
|
| 254 |
+
color: backgroundColor,
|
| 255 |
margin: const EdgeInsets.all(16),
|
| 256 |
child: Padding(
|
| 257 |
padding: const EdgeInsets.all(20),
|
| 258 |
child: Column(
|
| 259 |
mainAxisSize: MainAxisSize.min,
|
| 260 |
children: [
|
| 261 |
+
if (icon != null) ...[
|
| 262 |
+
Icon(icon, size: 48, color: Theme.of(context).primaryColor),
|
| 263 |
+
const SizedBox(height: 12),
|
| 264 |
+
],
|
| 265 |
+
Text(
|
| 266 |
+
title,
|
| 267 |
+
style: Theme.of(context).textTheme.headlineSmall?.copyWith(
|
| 268 |
+
fontWeight: FontWeight.bold,
|
| 269 |
+
),
|
| 270 |
+
textAlign: TextAlign.center,
|
| 271 |
+
),
|
| 272 |
const SizedBox(height: 8),
|
| 273 |
+
Text(
|
| 274 |
+
description,
|
| 275 |
+
style: Theme.of(context).textTheme.bodyMedium,
|
| 276 |
+
textAlign: TextAlign.center,
|
| 277 |
+
),
|
| 278 |
],
|
| 279 |
),
|
| 280 |
),
|
| 281 |
);
|
| 282 |
}}
|
| 283 |
}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
```"""
|
| 285 |
|
| 286 |
+
ai_generated_code = generate_code_with_ai(prompt, max_length=1200)
|
| 287 |
|
| 288 |
# Add header comment
|
| 289 |
header = f"""// Custom Widgets for the Flutter Application
|
|
|
|
| 297 |
|
| 298 |
|
| 299 |
def generate_models_dart(description):
|
| 300 |
+
"""Generate the models.dart file content using Qwen AI."""
|
| 301 |
+
prompt = f"""Generate Flutter data models for: {description}
|
| 302 |
+
|
| 303 |
+
Create comprehensive data models with:
|
| 304 |
+
1. Proper class structure with const constructors
|
| 305 |
+
2. JSON serialization (toJson/fromJson)
|
| 306 |
+
3. CopyWith methods for immutable updates
|
| 307 |
+
4. Proper type annotations and null safety
|
| 308 |
+
5. Documentation comments
|
| 309 |
+
6. At least 2-3 models relevant to the app
|
| 310 |
+
|
| 311 |
+
Requirements:
|
| 312 |
+
- Use final fields for immutability
|
| 313 |
+
- Include proper error handling in fromJson
|
| 314 |
+
- Add copyWith for state management
|
| 315 |
+
- Use meaningful field names
|
| 316 |
+
|
| 317 |
+
Example structure:
|
| 318 |
```dart
|
| 319 |
+
/// Represents a task or item in the application
|
| 320 |
class Item {{
|
| 321 |
final String id;
|
| 322 |
final String title;
|
| 323 |
final String description;
|
| 324 |
final DateTime createdAt;
|
| 325 |
+
final DateTime? updatedAt;
|
| 326 |
final bool isCompleted;
|
| 327 |
+
final String? category;
|
| 328 |
|
| 329 |
const Item({{
|
| 330 |
required this.id,
|
| 331 |
required this.title,
|
| 332 |
required this.description,
|
| 333 |
required this.createdAt,
|
| 334 |
+
this.updatedAt,
|
| 335 |
this.isCompleted = false,
|
| 336 |
+
this.category,
|
| 337 |
}});
|
| 338 |
|
| 339 |
+
/// Convert to JSON for storage/API calls
|
| 340 |
Map<String, dynamic> toJson() => {{
|
| 341 |
'id': id,
|
| 342 |
'title': title,
|
| 343 |
'description': description,
|
| 344 |
'createdAt': createdAt.toIso8601String(),
|
| 345 |
+
'updatedAt': updatedAt?.toIso8601String(),
|
| 346 |
'isCompleted': isCompleted,
|
| 347 |
+
'category': category,
|
| 348 |
}};
|
| 349 |
|
| 350 |
+
/// Create from JSON data
|
| 351 |
factory Item.fromJson(Map<String, dynamic> json) => Item(
|
| 352 |
+
id: json['id'] as String? ?? '',
|
| 353 |
+
title: json['title'] as String? ?? '',
|
| 354 |
+
description: json['description'] as String? ?? '',
|
| 355 |
+
createdAt: DateTime.tryParse(json['createdAt'] as String? ?? '') ?? DateTime.now(),
|
| 356 |
+
updatedAt: json['updatedAt'] != null
|
| 357 |
+
? DateTime.tryParse(json['updatedAt'] as String)
|
| 358 |
+
: null,
|
| 359 |
+
isCompleted: json['isCompleted'] as bool? ?? false,
|
| 360 |
+
category: json['category'] as String?,
|
| 361 |
);
|
| 362 |
|
| 363 |
+
/// Create a copy with updated fields
|
| 364 |
Item copyWith({{
|
| 365 |
String? id,
|
| 366 |
String? title,
|
| 367 |
String? description,
|
| 368 |
DateTime? createdAt,
|
| 369 |
+
DateTime? updatedAt,
|
| 370 |
bool? isCompleted,
|
| 371 |
+
String? category,
|
| 372 |
}}) => Item(
|
| 373 |
id: id ?? this.id,
|
| 374 |
title: title ?? this.title,
|
| 375 |
description: description ?? this.description,
|
| 376 |
createdAt: createdAt ?? this.createdAt,
|
| 377 |
+
updatedAt: updatedAt ?? this.updatedAt,
|
| 378 |
isCompleted: isCompleted ?? this.isCompleted,
|
| 379 |
+
category: category ?? this.category,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 380 |
);
|
| 381 |
}}
|
| 382 |
```"""
|
| 383 |
|
| 384 |
+
ai_generated_code = generate_code_with_ai(prompt, max_length=1000)
|
| 385 |
|
| 386 |
# Add header comment
|
| 387 |
header = f"""// Data Models for the Flutter Application
|
|
|
|
| 395 |
|
| 396 |
|
| 397 |
def generate_services_dart(description):
|
| 398 |
+
"""Generate the services.dart file content using Qwen AI."""
|
| 399 |
+
prompt = f"""Generate Flutter service classes for: {description}
|
| 400 |
+
|
| 401 |
+
Create comprehensive service classes with:
|
| 402 |
+
1. DataService - For managing application data (CRUD operations)
|
| 403 |
+
2. ValidationService - For input validation and utilities
|
| 404 |
+
3. Proper error handling and state management
|
| 405 |
+
4. Documentation comments
|
| 406 |
+
5. Null safety and modern Dart patterns
|
| 407 |
+
|
| 408 |
+
Requirements:
|
| 409 |
+
- Use proper async/await patterns where applicable
|
| 410 |
+
- Include error handling with try-catch blocks
|
| 411 |
+
- Add meaningful method names and documentation
|
| 412 |
+
- Use immutable data structures where possible
|
| 413 |
+
|
| 414 |
+
Example structure:
|
| 415 |
```dart
|
| 416 |
+
import 'dart:convert';
|
| 417 |
import 'models.dart';
|
| 418 |
|
| 419 |
+
/// Service for managing application data and business logic
|
| 420 |
class DataService {{
|
| 421 |
final List<Item> _items = [];
|
| 422 |
|
| 423 |
+
/// Get all items (returns unmodifiable list)
|
| 424 |
List<Item> getAllItems() => List.unmodifiable(_items);
|
| 425 |
|
| 426 |
+
/// Get items by completion status
|
| 427 |
+
List<Item> getItems({{bool? completed}}) {{
|
| 428 |
+
if (completed == null) return getAllItems();
|
| 429 |
+
return _items.where((item) => item.isCompleted == completed).toList();
|
| 430 |
+
}}
|
| 431 |
+
|
| 432 |
+
/// Add a new item to the collection
|
| 433 |
+
void addItem(Item item) {{
|
| 434 |
+
_items.add(item);
|
| 435 |
+
}}
|
| 436 |
|
| 437 |
+
/// Update an existing item by ID
|
| 438 |
bool updateItem(String id, Item updatedItem) {{
|
| 439 |
final index = _items.indexWhere((item) => item.id == id);
|
| 440 |
if (index != -1) {{
|
|
|
|
| 444 |
return false;
|
| 445 |
}}
|
| 446 |
|
| 447 |
+
/// Remove an item by ID
|
| 448 |
bool deleteItem(String id) {{
|
| 449 |
final index = _items.indexWhere((item) => item.id == id);
|
| 450 |
if (index != -1) {{
|
|
|
|
| 454 |
return false;
|
| 455 |
}}
|
| 456 |
|
| 457 |
+
/// Toggle completion status of an item
|
| 458 |
+
bool toggleItemCompletion(String id) {{
|
| 459 |
final index = _items.indexWhere((item) => item.id == id);
|
| 460 |
if (index != -1) {{
|
| 461 |
+
_items[index] = _items[index].copyWith(
|
| 462 |
+
isCompleted: !_items[index].isCompleted,
|
| 463 |
+
updatedAt: DateTime.now(),
|
| 464 |
+
);
|
| 465 |
return true;
|
| 466 |
}}
|
| 467 |
return false;
|
| 468 |
}}
|
| 469 |
+
|
| 470 |
+
/// Clear all items
|
| 471 |
+
void clearAll() {{
|
| 472 |
+
_items.clear();
|
| 473 |
+
}}
|
| 474 |
}}
|
| 475 |
|
| 476 |
+
/// Utility service for validation and data processing
|
| 477 |
class ValidationService {{
|
| 478 |
+
/// Validate email format using regex
|
| 479 |
static bool isValidEmail(String email) {{
|
| 480 |
+
final emailRegex = RegExp(
|
| 481 |
+
r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{{2,}}$',
|
| 482 |
+
);
|
| 483 |
+
return emailRegex.hasMatch(email);
|
| 484 |
}}
|
| 485 |
|
| 486 |
+
/// Check if string is not null or empty
|
| 487 |
static bool isNotEmpty(String? value) {{
|
| 488 |
return value != null && value.trim().isNotEmpty;
|
| 489 |
}}
|
| 490 |
|
| 491 |
+
/// Check minimum length requirement
|
| 492 |
static bool hasMinLength(String value, int minLength) {{
|
| 493 |
return value.length >= minLength;
|
| 494 |
}}
|
| 495 |
+
|
| 496 |
+
/// Validate item data before saving
|
| 497 |
+
static String? validateItem(Item item) {{
|
| 498 |
+
if (!isNotEmpty(item.title)) {{
|
| 499 |
+
return 'Title cannot be empty';
|
| 500 |
+
}}
|
| 501 |
+
if (!hasMinLength(item.title, 3)) {{
|
| 502 |
+
return 'Title must be at least 3 characters long';
|
| 503 |
+
}}
|
| 504 |
+
if (!isNotEmpty(item.description)) {{
|
| 505 |
+
return 'Description cannot be empty';
|
| 506 |
+
}}
|
| 507 |
+
return null; // No validation errors
|
| 508 |
+
}}
|
| 509 |
}}
|
| 510 |
```"""
|
| 511 |
|
| 512 |
+
ai_generated_code = generate_code_with_ai(prompt, max_length=1000)
|
| 513 |
|
| 514 |
# Add header comment
|
| 515 |
header = f"""// Business Logic and Services
|
|
|
|
| 755 |
- **README.md** - Setup and usage instructions
|
| 756 |
|
| 757 |
### ⚠️ Important Notes:
|
| 758 |
+
- This uses **Qwen 3 0.6B** - a powerful AI model optimized for code generation
|
| 759 |
- The AI generates Flutter/Dart code based on your description
|
| 760 |
- Always review and test generated code before production use
|
| 761 |
- The AI service may occasionally be slow or unavailable - try again if needed
|