cdb_ui/lib/pages/flow/create_flow_page.dart

136 lines
3.8 KiB
Dart
Raw Normal View History

2024-09-24 16:00:13 +00:00
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<CreateFlowPage> createState() => _CreateFlowPageState();
}
class _CreateFlowPageState extends State<CreateFlowPage> {
List<API.Transaction> 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) {
2024-09-26 19:35:28 +00:00
var info = API.API().getFlowInfo(flow.kind);
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (context) => ActiveFlowPage(flow, info),
));
2024-09-24 16:00:13 +00:00
});
});
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);
},
));
});
}
2024-09-26 19:01:45 +00:00
List<Widget> buildInputSelection(BuildContext context) {
if (widget.info.depends.isEmpty) {
return [];
}
return [
Column(
children: widget.info.depends.map((x) {
2024-09-26 19:35:28 +00:00
var (item, variant) = API.itemVariant(x);
2024-09-26 19:01:45 +00:00
return ElevatedButton(
onPressed: () {
selectDependItems(context, x);
},
2024-09-26 19:35:28 +00:00
child:
Text("Add ${API.API().getItem(item).variants[variant]!.name}"));
2024-09-26 19:01:45 +00:00
}).toList()),
const Divider(),
];
2024-09-24 16:00:13 +00:00
}
2024-09-26 07:50:53 +00:00
Widget flowContinuation() {
return Row(
2024-09-26 19:01:45 +00:00
mainAxisAlignment: MainAxisAlignment.center,
2024-09-26 07:50:53 +00:00
children: [
Card(
2024-09-26 19:01:45 +00:00
child: Padding(
padding: const EdgeInsets.all(8.0),
2024-09-26 19:35:28 +00:00
child: Text(API.API().getFlowInfo(widget.previousFlow!.kind).name),
2024-09-26 19:01:45 +00:00
),
2024-09-26 07:50:53 +00:00
),
2024-10-08 08:55:30 +00:00
const Card(child: Icon(Icons.arrow_right)),
2024-09-26 07:50:53 +00:00
Card(
2024-09-26 19:01:45 +00:00
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(widget.info.name),
),
2024-09-26 07:50:53 +00:00
)
],
);
}
2024-09-24 16:00:13 +00:00
@override
Widget build(BuildContext context) {
return Scaffold(
2024-09-26 19:01:45 +00:00
appBar: AppBar(
title: Text(widget.previousFlow != null
? "Continue to ${widget.previousFlow!.kind}"
: "Create new ${widget.info.name} Flow")),
2024-09-24 16:00:13 +00:00
body: Column(
children: [
2024-09-26 07:50:53 +00:00
if (widget.previousFlow != null) ...[
flowContinuation(),
const Divider()
],
2024-09-26 19:01:45 +00:00
...buildInputSelection(context),
2024-09-24 16:00:13 +00:00
Card(
child: Column(
children: depends
.map((x) => TransactionCard(x, () {},
onTap: (x) {}, onLongPress: (x) {}))
.toList())),
ElevatedButton(
onPressed: () => _create(context),
child: const Text("Create Flow"))
],
),
);
}
2024-09-26 07:50:53 +00:00
}