Files
fcs/lib/pages/carton/carton_editor.dart

255 lines
8.0 KiB
Dart
Raw Normal View History

2024-09-22 16:49:59 +06:30
import 'package:fcs/constants.dart';
2020-10-18 02:38:46 +06:30
import 'package:fcs/domain/entities/carton.dart';
2020-10-09 17:28:42 +06:30
import 'package:fcs/domain/entities/user.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/helpers/theme.dart';
2024-02-07 17:26:29 +06:30
import 'package:fcs/pages/user_search/user_search.dart';
2024-01-25 17:40:35 +06:30
import 'package:fcs/pages/widgets/local_app_bar.dart';
2020-10-14 13:17:12 +06:30
import 'package:fcs/pages/widgets/local_radio_buttons.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/pages/widgets/local_text.dart';
2020-10-14 13:17:12 +06:30
import 'package:fcs/pages/widgets/local_title.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/pages/widgets/progress.dart';
2020-10-14 16:53:16 +06:30
import 'package:flutter/cupertino.dart';
2020-06-04 01:36:49 +06:30
import 'package:flutter/material.dart';
2021-09-10 12:00:08 +06:30
import 'package:flutter_vector_icons/flutter_vector_icons.dart';
2024-03-04 17:09:47 +06:30
import 'package:provider/provider.dart';
2024-02-05 11:13:12 +06:30
import '../main/util.dart';
2024-02-09 13:49:18 +06:30
import 'carton_package_form.dart';
2024-02-08 17:04:37 +06:30
import 'carton_info.dart';
2024-02-09 13:35:32 +06:30
import 'mix_carton/mix_carton_form.dart';
2024-03-04 17:09:47 +06:30
import 'model/package_selection_model.dart';
2024-02-09 13:49:18 +06:30
import 'widget/carton_row.dart';
2020-10-09 17:28:42 +06:30
2020-10-19 05:13:49 +06:30
class CartonEditor extends StatefulWidget {
2025-03-07 17:41:09 +06:30
const CartonEditor({super.key});
2020-06-04 01:36:49 +06:30
@override
2020-10-19 05:13:49 +06:30
_CartonEditorState createState() => _CartonEditorState();
2020-06-04 01:36:49 +06:30
}
2020-10-19 05:13:49 +06:30
class _CartonEditorState extends State<CartonEditor> {
2025-03-07 17:41:09 +06:30
List<String> cartonTypes = [carton_from_packages, carton_mix_carton];
List<Carton> cartons = [];
2020-06-04 01:36:49 +06:30
2025-03-07 17:41:09 +06:30
bool isLoading = false;
2021-09-10 12:00:08 +06:30
String? _selectedCartonType;
2021-01-09 19:11:47 +06:30
2024-02-05 11:13:12 +06:30
User? _consignee;
User? _sender;
2025-03-07 17:41:09 +06:30
2024-03-04 17:09:47 +06:30
bool _hasPackages = true;
2021-01-25 16:09:41 +06:30
2020-06-04 01:36:49 +06:30
@override
void initState() {
2024-02-05 11:13:12 +06:30
_init();
2020-06-04 01:36:49 +06:30
super.initState();
2024-02-05 11:13:12 +06:30
}
2020-10-14 16:53:16 +06:30
2024-02-05 11:13:12 +06:30
_init() {
2025-03-07 17:41:09 +06:30
_selectedCartonType = carton_from_packages;
2024-02-05 11:13:12 +06:30
if (mounted) {
setState(() {});
2020-06-04 01:36:49 +06:30
}
}
@override
Widget build(BuildContext context) {
2021-01-09 19:11:47 +06:30
bool isFromPackages = _selectedCartonType == carton_from_packages;
2020-12-02 20:55:00 +06:30
2024-02-01 18:07:40 +06:30
final createBtn = Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
InkWell(
onTap: () {
Navigator.pop(context);
},
child: Container(
alignment: Alignment.bottomRight,
height: 45,
width: 130,
decoration: BoxDecoration(
color: primaryColor,
borderRadius: BorderRadius.circular(5),
),
child: TextButton(
onPressed: null,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Flexible(
child: LocalText(context, 'box.done.btn',
color: Colors.white, fontSize: 15),
),
const SizedBox(width: 5),
const Icon(
MaterialCommunityIcons.check_circle_outline,
color: Colors.white,
),
],
),
),
2020-12-02 20:55:00 +06:30
)),
2024-02-01 18:07:40 +06:30
],
2020-10-14 16:53:16 +06:30
);
2020-10-19 05:13:49 +06:30
final cartonTypeBox = LocalRadioButtons(
2025-03-07 17:41:09 +06:30
readOnly: false,
values: cartonTypes,
2020-10-19 05:13:49 +06:30
selectedValue: _selectedCartonType,
2021-09-10 12:00:08 +06:30
callback: (String? v) {
2020-10-19 05:13:49 +06:30
setState(() {
_selectedCartonType = v;
});
});
2020-10-09 17:28:42 +06:30
2024-02-01 18:07:40 +06:30
final cartonTitleBox = LocalTitle(
2025-03-07 17:41:09 +06:30
textKey: cartons.isEmpty ? "box.shipment.boxes" : "box.cartion.count",
translationVariables: cartons.isEmpty ? null : ["${cartons.length}"],
2024-02-01 18:07:40 +06:30
trailing: Padding(
padding: const EdgeInsets.only(right: 5),
child: InkResponse(
radius: 30,
onTap: () async {
2024-02-01 18:07:40 +06:30
//for packages
if (isFromPackages) {
2025-03-07 17:41:09 +06:30
if (_consignee == null) {
showMsgDialog(context, "Error", "Please select consignee");
2024-02-05 11:13:12 +06:30
return;
}
2025-03-07 17:41:09 +06:30
if (_sender == null) {
showMsgDialog(context, "Error", "Please select sender");
2024-02-05 11:13:12 +06:30
return;
}
2024-03-04 17:09:47 +06:30
if (!_hasPackages) {
showMsgDialog(
2025-03-07 17:41:09 +06:30
context, "Error", "No packages in consignee and sender");
2024-03-04 17:09:47 +06:30
return;
}
Carton? carton = await Navigator.push(
2024-02-05 11:13:12 +06:30
context,
CupertinoPageRoute(
2024-02-09 13:49:18 +06:30
builder: (context) => CartonPackageForm(
2025-03-07 17:41:09 +06:30
sender: _sender!, consignee: _consignee!)));
if (carton != null) {
setState(() {
2025-03-07 17:41:09 +06:30
cartons.add(carton);
});
}
2024-02-01 18:07:40 +06:30
}
// for mix cartion
else {
Carton? carton = await Navigator.push(
2024-02-01 18:07:40 +06:30
context,
CupertinoPageRoute(
2024-02-09 13:35:32 +06:30
builder: (context) => const MixCartonForm()));
if (carton != null) {
setState(() {
2025-03-07 17:41:09 +06:30
cartons.add(carton);
});
}
2024-02-01 18:07:40 +06:30
}
},
2024-02-09 12:16:39 +06:30
child: Icon(Icons.add_circle, color: primaryColor),
2020-12-10 20:06:15 +06:30
),
2024-02-01 18:07:40 +06:30
));
2021-01-09 19:11:47 +06:30
2025-03-07 17:41:09 +06:30
final consigneeBox = userSearchBox(context,
lableKey: 'box.consignee.title',
icon: MaterialCommunityIcons.account_arrow_left,
user: _consignee,
onSearch: () => searchUser(context, onUserSelect: (u) {
setState(() {
_consignee = u;
});
_checkPackages();
}, popPage: true));
final senderBox = userSearchBox(context,
lableKey: 'box.sender.title',
icon: MaterialCommunityIcons.account_arrow_right,
user: _sender,
onSearch: () => searchUser(context, onUserSelect: (u) {
setState(() {
_sender = u;
});
_checkPackages();
}, popPage: true));
2021-01-09 19:11:47 +06:30
2024-02-01 18:07:40 +06:30
return LocalProgress(
2025-03-07 17:41:09 +06:30
inAsyncCall: isLoading,
2024-02-01 18:07:40 +06:30
child: Scaffold(
appBar: LocalAppBar(
2025-03-07 17:41:09 +06:30
labelKey: "boxes.new",
2024-02-01 18:07:40 +06:30
backgroundColor: Colors.white,
arrowColor: primaryColor,
labelColor: primaryColor),
body: ListView(
padding: EdgeInsets.only(left: 10, right: 10),
shrinkWrap: true,
children: [
LocalTitle(textKey: "box.select.carton_type"),
cartonTypeBox,
isFromPackages
? Column(
2024-03-04 17:09:47 +06:30
crossAxisAlignment: CrossAxisAlignment.start,
2024-02-01 18:07:40 +06:30
children: [
LocalTitle(textKey: "box.select.sender_and_consignee"),
Row(
2024-03-04 17:09:47 +06:30
crossAxisAlignment: CrossAxisAlignment.start,
2024-02-01 18:07:40 +06:30
children: [
2025-03-07 17:41:09 +06:30
Flexible(child: consigneeBox),
Flexible(child: senderBox)
2024-02-01 18:07:40 +06:30
],
),
2024-03-04 17:09:47 +06:30
!_hasPackages
? Padding(
padding: const EdgeInsets.only(top: 5),
child: Text("No packages in sender and consignee",
style: TextStyle(
color: dangerColor, fontSize: 13)),
)
: const SizedBox()
2024-02-01 18:07:40 +06:30
],
)
: Container(),
cartonTitleBox,
2025-03-07 17:41:09 +06:30
Column(children: _getCartons(context, cartons)),
2024-02-01 18:07:40 +06:30
SizedBox(height: 20),
createBtn,
SizedBox(height: 20),
],
),
),
2020-12-01 19:02:21 +06:30
);
}
2024-03-04 17:09:47 +06:30
_checkPackages() async {
if (_sender != null && _consignee != null) {
var p = await context
.read<PackageSelectionModel>()
.getPackagesBySenderAndConsigneeId(
senderId: _sender!.id ?? "", consigneeId: _consignee!.id ?? "");
setState(() {
_hasPackages = p.isNotEmpty;
});
}
}
2024-02-01 18:07:40 +06:30
List<Widget> _getCartons(BuildContext context, List<Carton> cartons) {
2024-02-08 17:04:37 +06:30
return cartons.map((c) {
return InkWell(
onTap: () async {
Navigator.push(
context,
2024-02-09 13:35:32 +06:30
CupertinoPageRoute(builder: (context) => CartonInfo(carton: c)),
2024-02-08 17:04:37 +06:30
);
},
2024-02-26 17:26:25 +06:30
child: CartonRow(carton: c));
2024-02-01 18:07:40 +06:30
}).toList();
2020-10-19 05:13:49 +06:30
}
2020-06-04 01:36:49 +06:30
}