import 'package:cdb_ui/api.dart' as API; import 'package:cdb_ui/pages/flow/active_flow_page.dart'; import 'package:cdb_ui/pages/transaction.dart'; import 'package:flutter/material.dart'; class CreateFlowPage extends StatefulWidget { final API.FlowInfo info; final Function refresh; final API.Flow? previousFlow; const CreateFlowPage(this.info, this.refresh, {this.previousFlow, super.key}); @override State createState() => _CreateFlowPageState(); } class _CreateFlowPageState extends State { List depends = []; void _create(BuildContext context) { if (widget.previousFlow != null) { API .API() .continueFlow(widget.previousFlow!.id, input: depends.map((x) => x.uuid).toList()) .then((x) { API.API().getFlow(x).then((flow) { var info = API.API().getFlowInfo(flow.kind); Navigator.of(context).pushReplacement(MaterialPageRoute( builder: (context) => ActiveFlowPage(flow, info), )); }); }); return; } API .API() .startFlow(widget.info.id, input: depends.map((x) => x.uuid).toList()) .then((flowID) { widget.refresh(); API.API().getFlow(flowID).then((flow) { Navigator.of(context).pushReplacement(MaterialPageRoute( builder: (context) => ActiveFlowPage(flow, widget.info))); }); }); } void selectDependItems(BuildContext context, String itemVariant) { var (item, variant) = API.itemVariant(itemVariant); API.API().getInventoryOfVariant(item, variant).then((transactions) { Navigator.of(context).push(MaterialPageRoute( builder: (context) { return TransactionSelectPage(transactions, onSelect: (t) { if (!depends.contains(t)) { setState(() { depends.add(t); }); } }, exclude: depends); }, )); }); } List buildInputSelection(BuildContext context) { if (widget.info.depends.isEmpty) { return []; } return [ Column( children: widget.info.depends.map((x) { var (item, variant) = API.itemVariant(x); return ElevatedButton( onPressed: () { selectDependItems(context, x); }, child: Text("Add ${API.API().getItem(item).variants[variant]!.name}")); }).toList()), const Divider(), ]; } Widget flowContinuation() { return Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Card( child: Padding( padding: const EdgeInsets.all(8.0), child: Text(API.API().getFlowInfo(widget.previousFlow!.kind).name), ), ), const Card(child: Icon(Icons.arrow_right)), Card( child: Padding( padding: const EdgeInsets.all(8.0), child: Text(widget.info.name), ), ) ], ); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.previousFlow != null ? "Continue to ${widget.previousFlow!.kind}" : "Create new ${widget.info.name} Flow")), body: Column( children: [ if (widget.previousFlow != null) ...[ flowContinuation(), const Divider() ], ...buildInputSelection(context), Card( child: Column( children: depends .map((x) => TransactionCard(x, () {}, onTap: (x) {}, onLongPress: (x) {})) .toList())), ElevatedButton( onPressed: () => _create(context), child: const Text("Create Flow")) ], ), ); } }