mirror of
https://github.com/dancojocaru2000/logic-circuits-simulator.git
synced 2025-02-23 01:29:36 +02:00
Added adding inputs and outputs
This commit is contained in:
parent
bcee2d735d
commit
61dc1f1f45
3 changed files with 109 additions and 20 deletions
62
lib/dialogs/new_ask_for_name.dart
Normal file
62
lib/dialogs/new_ask_for_name.dart
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||||
|
|
||||||
|
class NewAskForNameDialog extends HookWidget {
|
||||||
|
final String title;
|
||||||
|
final String? labelText;
|
||||||
|
|
||||||
|
const NewAskForNameDialog({required this.title, this.labelText, Key? key}) : super(key: key);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final tec = useTextEditingController();
|
||||||
|
final onPressed = useMemoized(() => () {
|
||||||
|
Navigator.of(context).pop(tec.text);
|
||||||
|
}, [tec.text]);
|
||||||
|
|
||||||
|
return Dialog(
|
||||||
|
child: Padding(
|
||||||
|
padding: const EdgeInsets.all(8.0),
|
||||||
|
child: IntrinsicWidth(
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
Align(
|
||||||
|
alignment: Alignment.centerRight,
|
||||||
|
child: IconButton(
|
||||||
|
icon: const Icon(Icons.close),
|
||||||
|
tooltip: 'Close',
|
||||||
|
onPressed: () {
|
||||||
|
Navigator.of(context).pop();
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
Text(
|
||||||
|
title,
|
||||||
|
style: Theme.of(context).textTheme.headline6,
|
||||||
|
),
|
||||||
|
Padding(
|
||||||
|
padding: const EdgeInsets.all(8.0),
|
||||||
|
child: Container(
|
||||||
|
constraints: const BoxConstraints(minWidth: 300),
|
||||||
|
child: TextField(
|
||||||
|
controller: tec,
|
||||||
|
decoration: InputDecoration(
|
||||||
|
border: const OutlineInputBorder(),
|
||||||
|
labelText: labelText,
|
||||||
|
suffixIcon: IconButton(
|
||||||
|
icon: const Icon(Icons.done),
|
||||||
|
onPressed: onPressed,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
onSubmitted: (_) => onPressed(),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
import 'package:collection/collection.dart';
|
import 'package:collection/collection.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||||
|
import 'package:logic_circuits_simulator/dialogs/new_ask_for_name.dart';
|
||||||
import 'package:logic_circuits_simulator/models/project.dart';
|
import 'package:logic_circuits_simulator/models/project.dart';
|
||||||
import 'package:logic_circuits_simulator/state/project.dart';
|
import 'package:logic_circuits_simulator/state/project.dart';
|
||||||
import 'package:logic_circuits_simulator/utils/iterable_extension.dart';
|
import 'package:logic_circuits_simulator/utils/iterable_extension.dart';
|
||||||
|
@ -18,11 +19,11 @@ class EditComponentPage extends HookWidget {
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final anySave = useState(false);
|
final anySave = useState(false);
|
||||||
final projectState = useProvider<ProjectState>();
|
final projectState = useProvider<ProjectState>();
|
||||||
final ce = projectState.index.components.where((c) => c.componentId == component.componentId).first;
|
ComponentEntry ce() => projectState.index.components.where((c) => c.componentId == component.componentId).first;
|
||||||
final truthTable = useState(ce.truthTable?.toList());
|
final truthTable = useState(ce().truthTable?.toList());
|
||||||
final inputs = useState(ce.inputs.toList());
|
final inputs = useState(ce().inputs.toList());
|
||||||
final outputs = useState(ce.outputs.toList());
|
final outputs = useState(ce().outputs.toList());
|
||||||
final componentNameEditingController = useTextEditingController(text: ce.componentName);
|
final componentNameEditingController = useTextEditingController(text: ce().componentName);
|
||||||
useValueListenable(componentNameEditingController);
|
useValueListenable(componentNameEditingController);
|
||||||
final dirty = useMemoized(
|
final dirty = useMemoized(
|
||||||
() {
|
() {
|
||||||
|
@ -38,29 +39,29 @@ class EditComponentPage extends HookWidget {
|
||||||
// Don't allow saving empty outputs
|
// Don't allow saving empty outputs
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (componentNameEditingController.text != ce.componentName) {
|
if (componentNameEditingController.text != ce().componentName) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (!const ListEquality().equals(inputs.value, ce.inputs)) {
|
if (!const ListEquality().equals(inputs.value, ce().inputs)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (!const ListEquality().equals(outputs.value, ce.outputs)) {
|
if (!const ListEquality().equals(outputs.value, ce().outputs)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (!const ListEquality().equals(truthTable.value, ce.truthTable)) {
|
if (!const ListEquality().equals(truthTable.value, ce().truthTable)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
},
|
},
|
||||||
[
|
[
|
||||||
componentNameEditingController.text,
|
componentNameEditingController.text,
|
||||||
ce.componentName,
|
ce().componentName,
|
||||||
inputs.value,
|
inputs.value,
|
||||||
ce.inputs,
|
ce().inputs,
|
||||||
outputs.value,
|
outputs.value,
|
||||||
ce.outputs,
|
ce().outputs,
|
||||||
truthTable.value,
|
truthTable.value,
|
||||||
ce.truthTable,
|
ce().truthTable,
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -136,8 +137,20 @@ class EditComponentPage extends HookWidget {
|
||||||
IconButton(
|
IconButton(
|
||||||
icon: const Icon(Icons.add),
|
icon: const Icon(Icons.add),
|
||||||
tooltip: 'Add new input',
|
tooltip: 'Add new input',
|
||||||
onPressed: () {
|
onPressed: () async {
|
||||||
|
final inputName = await showDialog<String>(
|
||||||
|
context: context,
|
||||||
|
builder: (context) {
|
||||||
|
return const NewAskForNameDialog(
|
||||||
|
title: 'New Input',
|
||||||
|
labelText: 'Input name',
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
if (inputName != null) {
|
||||||
|
truthTable.value = truthTable.value?.expand((element) => [element, element]).toList();
|
||||||
|
inputs.value = inputs.value.toList()..add(inputName);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
@ -216,8 +229,20 @@ class EditComponentPage extends HookWidget {
|
||||||
IconButton(
|
IconButton(
|
||||||
icon: const Icon(Icons.add),
|
icon: const Icon(Icons.add),
|
||||||
tooltip: 'Add new output',
|
tooltip: 'Add new output',
|
||||||
onPressed: () {
|
onPressed: () async {
|
||||||
|
final outputName = await showDialog<String>(
|
||||||
|
context: context,
|
||||||
|
builder: (context) {
|
||||||
|
return const NewAskForNameDialog(
|
||||||
|
title: 'New Output',
|
||||||
|
labelText: 'Output name',
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
if (outputName != null) {
|
||||||
|
truthTable.value = truthTable.value?.map((e) => '${e}0').toList();
|
||||||
|
outputs.value = outputs.value.toList()..add(outputName);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
@ -294,15 +319,17 @@ class EditComponentPage extends HookWidget {
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
] else ...[
|
||||||
|
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
floatingActionButton: !dirty ? null : FloatingActionButton(
|
floatingActionButton: !dirty ? null : FloatingActionButton(
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
if (componentNameEditingController.text.isNotEmpty) {
|
if (componentNameEditingController.text.isNotEmpty) {
|
||||||
await projectState.editComponent(component.copyWith(componentName: componentNameEditingController.text));
|
await projectState.editComponent(ce().copyWith(componentName: componentNameEditingController.text));
|
||||||
}
|
}
|
||||||
await projectState.editComponent(ce.copyWith(
|
await projectState.editComponent(ce().copyWith(
|
||||||
inputs: inputs.value,
|
inputs: inputs.value,
|
||||||
outputs: outputs.value,
|
outputs: outputs.value,
|
||||||
truthTable: truthTable.value,
|
truthTable: truthTable.value,
|
||||||
|
|
|
@ -28,7 +28,7 @@ packages:
|
||||||
name: async
|
name: async
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.8.2"
|
version: "2.9.0"
|
||||||
boolean_selector:
|
boolean_selector:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|
Loading…
Add table
Reference in a new issue