Spaces:
Running on Zero

GameConfigIdea / exporters /flutter_exporter.py
kwabs22
Claude Opus 4.5
Port changes from duplicate space to original
9328e91
Raw History Blame Contribute Delete
3.79 kB
"""Flutter/Dart exporter for game configs."""
import json
def export_to_flutter(config_json):
"""Export config to Flutter/Dart code."""
try:
config = json.loads(config_json) if isinstance(config_json, str) else config_json
except:
return "Error: Invalid JSON", ""
dart = '''// Flutter Text Adventure Game
// Add to your lib/main.dart or create lib/game_state.dart
import 'package:flutter/material.dart';
// ========== GAME CONFIG ==========
final Map<String, Map<String, dynamic>> gameConfig = ''' + json.dumps(config, indent=2).replace('true', 'true').replace('false', 'false').replace('null', 'null') + ''';
// ========== GAME STATE ==========
class GameState extends ChangeNotifier {
String currentLocation = "''' + list(config.keys())[0] + '''";
String currentState = "''' + list(config[list(config.keys())[0]].keys())[0] + '''";
List<String> inventory = [];
int money = 0;
Map<String, bool> flags = {};
Map<String, dynamic>? get currentStateData {
return gameConfig[currentLocation]?[currentState];
}
String get description => currentStateData?['description'] ?? 'No description';
List<String> get choices =>
List<String>.from(currentStateData?['choices'] ?? []);
void makeChoice(int index) {
if (index < 0 || index >= choices.length) return;
String choice = choices[index];
var transitions = currentStateData?['transitions'] as Map<String, dynamic>?;
if (transitions != null && transitions.containsKey(choice)) {
String nextState = transitions[choice].toString();
if (nextState.contains('/')) {
var parts = nextState.split('/');
currentLocation = parts[0];
currentState = parts[1];
} else {
currentState = nextState;
}
notifyListeners();
}
}
}
// ========== GAME SCREEN WIDGET ==========
class GameScreen extends StatelessWidget {
final GameState gameState;
const GameScreen({Key? key, required this.gameState}) : super(key: key);
@override
Widget build(BuildContext context) {
return ListenableBuilder(
listenable: gameState,
builder: (context, child) {
return Scaffold(
appBar: AppBar(title: Text(gameState.currentLocation)),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
gameState.description,
style: Theme.of(context).textTheme.bodyLarge,
),
const SizedBox(height: 24),
const Text('What do you do?',
style: TextStyle(fontWeight: FontWeight.bold)),
const SizedBox(height: 8),
...gameState.choices.asMap().entries.map((entry) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: ElevatedButton(
onPressed: () => gameState.makeChoice(entry.key),
child: Text(entry.value),
),
);
}).toList(),
],
),
),
);
},
);
}
}
// ========== MAIN APP ==========
void main() {
runApp(MaterialApp(
home: GameScreen(gameState: GameState()),
theme: ThemeData.dark(),
));
}
'''
explanation = """Flutter/Dart Export:
1. Create new Flutter project: flutter create text_adventure
2. Replace lib/main.dart with this code
3. Run: flutter run
Features:
- Uses ChangeNotifier for state management
- Responsive button-based choices
- Dark theme by default
For mobile: flutter build apk (Android) or flutter build ios"""
return explanation, dart