From f376b280d2038872f5cbe4c76859ce435a5a7d9f Mon Sep 17 00:00:00 2001 From: Manas Hejmadi Date: Sun, 3 Aug 2025 17:51:27 +0530 Subject: [PATCH] Implemented Flutter Code Export Functionality --- lib/widgets/ai_ui_desginer_widgets.dart | 142 ++++++++++++++---------- 1 file changed, 85 insertions(+), 57 deletions(-) diff --git a/lib/widgets/ai_ui_desginer_widgets.dart b/lib/widgets/ai_ui_desginer_widgets.dart index 6139f456..886e113f 100644 --- a/lib/widgets/ai_ui_desginer_widgets.dart +++ b/lib/widgets/ai_ui_desginer_widgets.dart @@ -4,6 +4,7 @@ import 'package:apidash/services/agentic_services/agent_caller.dart'; import 'package:apidash/widgets/widget_sending.dart'; import 'package:apidash_design_system/apidash_design_system.dart'; import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:genai/agentic_engine/blueprint.dart'; import 'package:stac/stac.dart' as stac; @@ -245,17 +246,19 @@ class _FrameWorkSelectorPageState extends State { } } -class SDUIPreviewPage extends StatefulWidget { +class SDUIPreviewPage extends ConsumerStatefulWidget { final String sduiCode; final Function() onNext; const SDUIPreviewPage( {super.key, required this.onNext, required this.sduiCode}); @override - State createState() => _SDUIPreviewPageState(); + ConsumerState createState() => _SDUIPreviewPageState(); } -class _SDUIPreviewPageState extends State { +class _SDUIPreviewPageState extends ConsumerState { + bool exportingCode = false; + @override Widget build(BuildContext context) { return Container( @@ -288,72 +291,97 @@ class _SDUIPreviewPageState extends State { ), ), kVSpacer20, - Container( - width: double.infinity, - decoration: BoxDecoration( - color: Colors.black, // Dark background - borderRadius: BorderRadius.circular(15), - border: Border.all( - color: Colors.grey, // Border color - width: 1, + if (!exportingCode) ...[ + Container( + width: double.infinity, + decoration: BoxDecoration( + color: Colors.black, // Dark background + borderRadius: BorderRadius.circular(15), + border: Border.all( + color: Colors.grey, // Border color + width: 1, + ), + ), + child: TextField( + maxLines: 3, // Makes the text box taller + style: TextStyle(color: Colors.white), // White text + decoration: InputDecoration( + hintText: 'Any Modifications?', + hintStyle: TextStyle(color: Colors.grey), // Grey hint text + border: InputBorder.none, // Removes the default border + contentPadding: EdgeInsets.all(16), // Padding inside the box + ), ), ), - child: TextField( - maxLines: 3, // Makes the text box taller - style: TextStyle(color: Colors.white), // White text - decoration: InputDecoration( - hintText: 'Any Modifications?', - hintStyle: TextStyle(color: Colors.grey), // Grey hint text - border: InputBorder.none, // Removes the default border - contentPadding: EdgeInsets.all(16), // Padding inside the box - ), - ), - ), - kVSpacer20, + kVSpacer20, + ], Row( mainAxisAlignment: MainAxisAlignment.end, children: [ Align( alignment: Alignment.centerRight, - child: FilledButton.tonalIcon( - style: FilledButton.styleFrom( - padding: kPh12, - minimumSize: const Size(44, 44), - ), - onPressed: () { - widget.onNext(); - }, - icon: Icon( - Icons.download, - ), - label: const SizedBox( - child: Text( - "Export Code", - ), - ), - ), + child: (exportingCode) + ? Container( + child: CircularProgressIndicator( + strokeWidth: 1, + ), + margin: EdgeInsets.only(right: 10), + ) + : FilledButton.tonalIcon( + style: FilledButton.styleFrom( + padding: kPh12, + minimumSize: const Size(44, 44), + ), + onPressed: () async { + setState(() { + exportingCode = true; + }); + final ans = await APIDashAgentCaller.instance.call( + StacToFlutterBot(), + ref: ref, + input: AgentInputs( + variables: {'VAR_CODE': widget.sduiCode}), + ); + final exportedCode = ans['CODE']; + Clipboard.setData(ClipboardData(text: ans['CODE'])); + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text("Copied to clipboard!"))); + setState(() { + exportingCode = false; + }); + }, + icon: Icon( + Icons.download, + ), + label: const SizedBox( + child: Text( + "Export Code", + ), + ), + ), ), kHSpacer10, - Align( - alignment: Alignment.centerRight, - child: FilledButton.tonalIcon( - style: FilledButton.styleFrom( - padding: kPh12, - minimumSize: const Size(44, 44), - ), - onPressed: () { - widget.onNext(); - }, - icon: Icon( - Icons.generating_tokens, - ), - label: const SizedBox( - child: Text( - "Make Modifications", + if (!exportingCode) + Align( + alignment: Alignment.centerRight, + child: FilledButton.tonalIcon( + style: FilledButton.styleFrom( + padding: kPh12, + minimumSize: const Size(44, 44), + ), + onPressed: () { + widget.onNext(); + }, + icon: Icon( + Icons.generating_tokens, + ), + label: const SizedBox( + child: Text( + "Make Modifications", + ), ), ), ), - ), ], ), ],