| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- import 'dart:io';
- import 'package:flutter/material.dart';
- import 'package:flutter_riverpod/flutter_riverpod.dart';
- import 'package:gap/gap.dart';
- import 'package:go_router/go_router.dart';
- import 'package:jiffy/jiffy.dart';
- import 'package:path_provider/path_provider.dart';
- import 'package:scroll_to_index/scroll_to_index.dart';
- import 'package:tp5/core/basic_page.dart';
- import 'package:tp5/core/utils.dart';
- import 'package:tp5/lido/lido_api.dart';
- import 'package:tp5/lido/model/Lidoapi_list.dart';
- import 'package:tp5/lido/w_lidoflt.dart';
- import 'package:tp5/pdf/pdf_page.dart';
- class LidolistPage extends ConsumerStatefulWidget {
- const LidolistPage({super.key, required this.params});
- final LidolistPageParams params;
- @override
- ConsumerState<ConsumerStatefulWidget> createState() => _LidolistPageState();
- }
- class _LidolistPageState extends ConsumerState<LidolistPage> {
- final bottomnavstyle = ElevatedButton.styleFrom(
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(5.0),
- ),
- backgroundColor:
- const Color.fromARGB(255, 0, 36, 53) //elevated btton background color
- );
- final AutoScrollController _scrollCtrl = AutoScrollController();
- List<LidoapiList> legs = <LidoapiList>[];
- _scrollToDate({Jiffy? date}) {
- final jdate = date ?? Jiffy.now().toUtc();
- bool found = false;
- int id = 0;
- for (var leg in legs) {
- if (Jiffy.parseFromMillisecondsSinceEpoch(
- leg.leg?.estimatedDepartureTime ?? 0,
- isUtc: true)
- .isSameOrAfter(jdate.subtract(hours: 1))) {
- found = true;
- break;
- }
- id++;
- }
- Future.delayed(const Duration(milliseconds: 100)).then((value) {
- if (found && mounted && _scrollCtrl.hasClients) {
- _scrollCtrl.scrollToIndex(id,
- duration: const Duration(milliseconds: 1300),
- preferPosition: AutoScrollPosition.begin);
- }
- });
- }
- @override
- void initState() {
- Future.delayed(Duration.zero).then((x) async {
- // print(await ref
- // .read(lidoapiProvider)
- // .login(user: "TAR13176", pass: "FaresWiem08_a"));
- ref.read(isLoadingProvider.notifier).state = true;
- final out = await ref.read(lidoapiProvider).list(
- start: widget.params.datestart,
- end: widget.params.dateend,
- dep: widget.params.dep ?? "",
- des: widget.params.des ?? "",
- al: widget.params.al ?? "",
- fnum: widget.params.fnum ?? "",
- );
- if ((out["error"] == null && out["data"] is List) ||
- (out["cache"] != null)) {
- final data =
- (out["error"] == null ? out["data"] : (out["cache"])["data"]);
- legs.clear();
- for (var e in data ?? []) {
- //print(e);
- legs.add(LidoapiList.fromJson(e));
- }
- } else {
- context.showError(out["error"]);
- }
- setState(() {});
- ref.read(isLoadingProvider.notifier).state = false;
- _scrollToDate();
- //print("Lidolistpage: out= $out");
- // print((LidoapiList.fromJson(out["data"][0])));
- // writeStringToFile("${out["data"][0]}");
- });
- super.initState();
- }
- Future<void> writeStringToFile(String content) async {
- try {
- final directory = await getApplicationDocumentsDirectory();
- final file = File('${directory.path}/lidoapilist.txt');
- // Write the content to the file
- await file.writeAsString(content);
- print('Content written to file: ${file.path}');
- } catch (e) {
- print('Error writing to file: $e');
- }
- }
- @override
- Widget build(BuildContext context) {
- // final now = ref.watch(clockProvider);
- return BasicPage(
- actions: [
- IconButton(
- onPressed: () => context.push("/lido/settings"),
- icon: const Icon(Icons.settings)),
- const Gap(10)
- ],
- bottomNavigationBar: Container(
- padding: const EdgeInsets.all(8),
- // color: Colors.black,
- decoration: BoxDecoration(
- gradient: LinearGradient(
- begin: Alignment.topCenter,
- end: Alignment.bottomCenter,
- colors: [
- Colors.grey[700]!,
- Colors.black,
- ],
- )),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceAround,
- children: [
- ElevatedButton.icon(
- onPressed: () {
- _changeDay(context);
- },
- icon: const Icon(
- Icons.calendar_month), //icon data for elevated button
- label: const Text("Change\n Day"), //label text
- style: bottomnavstyle,
- ),
- const Gap(10),
- ElevatedButton.icon(
- onPressed: () {
- _searchFlights(context);
- },
- icon: const Icon(Icons.manage_search),
- label: const Text("Search\nFlights"),
- style: bottomnavstyle,
- ),
- const Gap(10), //icon data for elevated button
- ],
- ),
- ),
- title: "Lido / List",
- body: ListView.builder(
- itemCount: legs.length,
- itemBuilder: (_, index) => AutoScrollTag(
- key: ValueKey(index),
- controller: _scrollCtrl,
- index: index,
- child: _getItem(_, index)),
- shrinkWrap: true,
- controller: _scrollCtrl,
- ),
- );
- }
- int? _loadingOFP;
- _getItem(BuildContext buildContext, int index) {
- return InkWell(
- onTap: () async {
- final legid = legs[index].briefingId ?? "";
- if (_loadingOFP == null) {
- setState(() {
- _loadingOFP = index;
- });
- final ofp = await ref.read(lidoapiProvider).ofppdf(legid: legid);
- setState(() {
- _loadingOFP = null;
- });
- if (((ofp["data"] is Map) &&
- (ofp["data"]?["downloaded"] ?? false) == true) ||
- (ofp["cache"] != null)) {
- context.push("/pdf",
- extra: PdfPageParams(
- file: ofp["cache"] ?? ofp["data"]?["fileid"] ?? "",
- title: legid,
- initialZoom: 1));
- } else {
- context.showError("Cannot download PDF File !!!");
- }
- }
- },
- child:
- // Text("${legs[index].leg?.departureAirportIcao} ${legs[index].leg?.destinationAirportIcao} ${legs[index].leg?.flightNumber} ${Jiffy.parseFromMillisecondsSinceEpoch(legs[index].leg?.estimatedDepartureTime ?? 0, isUtc: true).format(pattern: "ddMMMyy HHmm")} ${Jiffy.parseFromMillisecondsSinceEpoch(legs[index].leg?.scheduledDepartureTime ?? 0, isUtc: true).format(pattern: "ddMMMyy HHmm")} ${legs[index].leg?.fuel?.blockFuel}"),
- Row(children: [
- if (_loadingOFP == index) const CircularProgressIndicator(),
- Expanded(child: WLidoflt(lidoapilist: legs[index]))
- ]));
- }
- void _changeDay(BuildContext context) {}
- TextEditingController ctrldep = TextEditingController();
- TextEditingController ctrldes = TextEditingController();
- TextEditingController ctrlairline = TextEditingController();
- TextEditingController ctrlfnum = TextEditingController();
- TextEditingController ctrlstartdate = TextEditingController();
- TextEditingController ctrlenddate = TextEditingController();
- void _searchFlights(xcontext) async {
- ref.read(isLoadingProvider.notifier).state = true;
- ref.read(isLoadingProvider.notifier).state = false;
- ctrldep.text = widget.params.dep ?? "";
- ctrldes.text = widget.params.des ?? "";
- ctrlairline.text = widget.params.al ?? "";
- ctrlfnum.text = widget.params.fnum ?? "";
- ctrlstartdate.text =
- widget.params.datestart?.format(pattern: "ddMMMyy") ?? "";
- ctrlenddate.text = widget.params.dateend?.format(pattern: "ddMMMyy") ?? "";
- final res = await showModalBottomSheet(
- context: context,
- builder: (context) => _searchFilter(xcontext, null, null));
- if (res is LidolistPageParams) context.push("/lido/list", extra: res);
- }
- Widget _searchFilter(BuildContext context, DateTime? min, DateTime? max) {
- min = min ?? DateTime.now().subtract(const Duration(days: 7));
- max = max ?? DateTime.now().add(const Duration(days: 7));
- return Container(
- padding: const EdgeInsets.all(20),
- child: Column(
- mainAxisSize: MainAxisSize.min,
- children: [
- Row(
- children: [
- Expanded(
- child: TextField(
- decoration:
- const InputDecoration(labelText: "Departure Airport"),
- maxLength: 3, // Set maximum length
- controller: ctrldep,
- ),
- ),
- const SizedBox(width: 10),
- Expanded(
- child: TextField(
- decoration:
- const InputDecoration(labelText: "Arrival Airport"),
- maxLength: 3, // Set maximum length
- controller: ctrldes,
- ),
- ),
- ],
- ),
- const SizedBox(height: 15),
- Row(
- children: [
- Expanded(
- child: TextField(
- controller: ctrlairline,
- decoration:
- const InputDecoration(labelText: "Airline IATA code"),
- maxLength: 2, // Set maximum length
- ),
- ),
- const SizedBox(width: 10),
- Expanded(
- child: TextField(
- controller: ctrlfnum,
- decoration: const InputDecoration(labelText: "Flight number"),
- maxLength: 6, // Set maximum length
- ),
- ),
- ],
- ),
- const SizedBox(height: 15),
- Row(
- children: [
- Expanded(
- child: TextField(
- decoration:
- const InputDecoration(labelText: "Starting date"),
- controller: ctrlstartdate,
- readOnly:
- true, //set it true, so that user will not able to edit text
- onTap: () async {
- DateTime? pickedDate = await showDatePicker(
- context: context,
- initialDate: DateTime.now().toUtc(),
- firstDate:
- min!, //DateTime.now () - not to allow to choose before today.
- lastDate: max!);
- if (pickedDate != null) {
- String formattedDate =
- Jiffy.parseFromDateTime(pickedDate).format(
- pattern:
- "ddMMMyy"); //formatted date output using intl package => 2021-03-16
- setState(() {
- ctrlstartdate.text = formattedDate;
- if (ctrlenddate.text == "" ||
- Jiffy.parseFromDateTime(pickedDate).isAfter(
- Jiffy.parse(ctrlenddate.text,
- pattern: "ddMMMyy", isUtc: true))) {
- ctrlenddate.text = formattedDate;
- }
- });
- } else {
- print("Date is not selected");
- }
- }),
- ),
- const SizedBox(width: 10),
- Expanded(
- child: TextField(
- decoration: const InputDecoration(labelText: "Ending date"),
- controller: ctrlenddate,
- readOnly:
- true, //set it true, so that user will not able to edit text
- onTap: () async {
- DateTime? pickedDate = await showDatePicker(
- context: context,
- initialDate: DateTime.now().toUtc(),
- firstDate: min!,
- lastDate: max!);
- if (pickedDate != null) {
- String formattedDate =
- Jiffy.parseFromDateTime(pickedDate)
- .format(pattern: "ddMMMyy");
- setState(() {
- ctrlenddate.text = formattedDate;
- if (ctrlstartdate.text == "" ||
- Jiffy.parseFromDateTime(pickedDate).isBefore(
- Jiffy.parse(ctrlstartdate.text,
- pattern: "ddMMMyy", isUtc: true))) {
- ctrlstartdate.text = formattedDate;
- }
- });
- } else {
- print("Date is not selected");
- }
- }),
- ),
- ],
- ),
- const SizedBox(height: 35),
- SizedBox(
- width: 360,
- child: ElevatedButton(
- onPressed: () async =>
- // await context.push("/lido/list",
- // extra: LidolistPageParams(
- // dep: ctrldep.text.toUpperCase(),
- // des: ctrldes.text.toUpperCase(),
- // al: ctrlairline.text.toUpperCase(),
- // fnum: ctrlfnum.text,
- // datestart: ctrlstartdate.text,
- // dateend: ctrlenddate.text,
- // )),
- Navigator.pop(
- context,
- LidolistPageParams(
- dep: ctrldep.text.toUpperCase(),
- des: ctrldes.text.toUpperCase(),
- al: ctrlairline.text.toUpperCase(),
- fnum: ctrlfnum.text,
- datestart: ctrlstartdate.text == ""
- ? null
- : Jiffy.parse(ctrlstartdate.text,
- pattern: "ddMMMyy", isUtc: true),
- dateend: ctrlenddate.text == ""
- ? null
- : Jiffy.parse(ctrlenddate.text,
- pattern: "ddMMMyy", isUtc: true),
- )), // Close the bottom sheet
- style: ElevatedButton.styleFrom(
- backgroundColor: Colors.green, // Set button color to green
- shape: RoundedRectangleBorder(
- borderRadius:
- BorderRadius.circular(15), // Add rounded corners
- ),
- padding:
- const EdgeInsets.symmetric(horizontal: 20, vertical: 15),
- ),
- child: const Text(
- 'Search Flights',
- style: TextStyle(fontSize: 18, color: Colors.white),
- ),
- ),
- ),
- ],
- ),
- );
- }
- _titleInfo(String? title, String? info,
- {double sizetitle = 10,
- double sizeinfo = 16,
- Color color = Colors.blueGrey}) =>
- Column(
- children: [
- Text(
- title ?? "---",
- style: TextStyle(color: Colors.grey, fontSize: sizetitle),
- ),
- Text(info ?? "---",
- style: TextStyle(color: color, fontSize: sizeinfo)),
- ],
- );
- }
- class LidolistPageParams {
- const LidolistPageParams({
- this.dateend,
- this.datestart,
- this.dep,
- this.des,
- this.al,
- this.fnum,
- this.lidouser,
- this.lidopass,
- });
- final Jiffy? dateend;
- final Jiffy? datestart;
- final String? dep;
- final String? des;
- final String? al;
- final String? fnum;
- final String? lidouser;
- final String? lidopass;
- }
|