Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
14 kB
1
Indexable
Never
import 'dart:async';
import 'package:awesome_dialog/awesome_dialog.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:parstaksimapp/Components/custom_dialog.dart';
import 'package:parstaksimapp/Models/fleet_info_model.dart';
import 'package:parstaksimapp/Models/last_info_model.dart';
import 'package:parstaksimapp/Services/web_service.dart';
import 'package:parstaksimapp/Views/MapPage/map_page.dart';
import 'package:parstaksimapp/Views/Menu/menu.dart';
import 'package:parstaksimapp/Views/Sign%20In/sign_in_page.dart';
import 'package:shared_preferences/shared_preferences.dart';

class HomePage extends StatefulWidget {
  final DeviceLastInfoModel deviceLastInfoModel;
  final String email;
  const HomePage({
    this.deviceLastInfoModel,
    this.email,
    Key key,
  }) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  FleetInfoModel fleetInfoModel;
  //a controller for map
  GoogleMapController mapController;
  //for map's markers
  Map<MarkerId, Marker> markers = <MarkerId, Marker>{};
  //for map's polyline (can show the road map with polylines)
  final Set<Polyline> _polyline = {};
  final Completer<GoogleMapController> _controller = Completer();
  int closedTaxiStatus = 0;
  int hiredTaxiStatus = 0;
  int idleTaxiStatus = 0;
  int unknownTaxiStatus = 0;
  int i = 0;
  DeviceLastInfoModel _deviceLastInfoModel;

  getFleetInfo(String email) async {
    await WebService.fleetInfo(email).then((value) {
      if (value != null) {
        setState(() {
          fleetInfoModel = value;
        });
      }
    });
  }

  getLastInfo(String email) async {
    await WebService.lastInfo(email).then((value) {
      setState(() {
        _deviceLastInfoModel = value;
      });
    });
  }

  Timer timer;
  @override
  void initState() {
    Timer temp;
    timer = temp;
    print("----------------HomePage initState-------------------------------");
    getFleetInfo(widget.email);
    getLastInfo(widget.email);
    // checkTaxiStatus();
    timer = Timer.periodic(Duration(seconds: 15), (Timer t) {
      print("---------------------HomePage timer-----------------------------");
      getFleetInfo(widget.email);
      getLastInfo(widget.email);
      // checkTaxiStatus();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    double height = MediaQuery.of(context).size.height;
    return Scaffold(
      appBar: AppBar(
        title: Text('Ana Sayfa'),
        centerTitle: true,
        automaticallyImplyLeading: false,
        backgroundColor: Colors.blue.shade900,
        actions: [
          IconButton(
              onPressed: () {
                CustomDialog(context, "Çıkmak istediğinize emin misiniz?",
                    () async {
                  SharedPreferences prefs =
                      await SharedPreferences.getInstance();
                  setState(() {
                    prefs.remove("email");
                  });
                  timer.cancel();
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => const SignInPage()),
                  );
                }, "Evet", () {}, "Hayır", DialogType.WARNING);
              },
              icon: const Icon(Icons.logout)),
        ],
        iconTheme: const IconThemeData(color: Colors.white),
        leading: Menu(
          email: widget.email,
          timer: timer,
        ),
      ),
      body: (fleetInfoModel == null || _deviceLastInfoModel == null)
          ? Center(child: CircularProgressIndicator())
          : SingleChildScrollView(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  //üst satır
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: [
                        //Filo Araç Bilgileri
                        _fleetContainer(width, height, 'FİLO DURUMU'),
                        //Filo Günlük Kazanç Durumu
                        _daySumFareContainer(
                            width, height, 'GÜNLÜK KAZANÇ DURUMU'),
                      ],
                    ),
                  ),
                  //alt satır
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: [
                        //Harita
                        _mapContainer(width, height),
                        //Filo Aylık Kazanç Durumu
                        _mounthSumFareContainer(
                            width, height, 'AYLIK KAZANÇ DURUMU'),
                      ],
                    ),
                  )
                ],
              ),
            ),
    );
  }

  //HARİTALAR
  Widget _mapContainer(double width, double height) {
    return SingleChildScrollView(
      child: Column(
        children: [
          //Harita Butonu
          Container(
            width: width * 0.45,
            height: height * 0.1,
            child: TextButton(
              style: TextButton.styleFrom(
                backgroundColor: Colors.blue.shade700,
              ),
              child: Text(
                'HARİTA',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: height * 0.06,
                  fontWeight: FontWeight.bold,
                ),
              ),
              onPressed: () {
                timer.cancel();
                Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => MapPage(email: widget.email)));
              },
            ),
          ),

          //Harita
          Container(
            width: width * 0.45,
            height: height * 0.4,
            decoration: BoxDecoration(
              //borderRadius: BorderRadius.circular(5),
              border: Border.all(width: 1),
            ),
            child: GoogleMap(
              //This part is for scrolling on map
              gestureRecognizers: Set()
                ..add(
                    Factory<PanGestureRecognizer>(() => PanGestureRecognizer()))
                ..add(Factory<ScaleGestureRecognizer>(
                    () => ScaleGestureRecognizer()))
                ..add(
                    Factory<TapGestureRecognizer>(() => TapGestureRecognizer()))
                ..add(Factory<VerticalDragGestureRecognizer>(
                    () => VerticalDragGestureRecognizer())),
              //************/
              mapType: MapType.normal,
              initialCameraPosition: CameraPosition(
                  target: LatLng(39.952262065691784, 32.79434192218846),
                  zoom: 5),
              minMaxZoomPreference: const MinMaxZoomPreference(1, 25),
              onMapCreated: _deviceLastInfoModel == null
                  ? null
                  : (GoogleMapController controller) async {
                      mapController = controller;
                      _controller.complete(controller);
                      for (int i = 0;
                          i < _deviceLastInfoModel.deviceLastInfo.length;
                          i++) {
                        //A marker for places
                        final marker = Marker(
                          markerId: MarkerId("$i"),
                          position: LatLng(
                              _deviceLastInfoModel.deviceLastInfo[i]
                                  .lastLocations.lastLocation.latitude,
                              _deviceLastInfoModel.deviceLastInfo[i]
                                  .lastLocations.lastLocation.longitude),
                          infoWindow: const InfoWindow(),
                        );

                        setState(() {
                          markers[MarkerId(i.toString())] = marker;
                        });
                      }
                    },
              polylines: _polyline,
              markers: Set<Marker>.of(markers.values),
            ),
          ),
        ],
      ),
    );
  }

//Filo Durumu
  Widget _fleetContainer(double width, double height, String text) {
    return Container(
      width: width * 0.45,
      height: height * 0.5,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(5),
        border: Border.all(width: 1),
        color: Colors.blue.shade200,
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          _infoContainer(width, height, text),
          _rows(
            width,
            height,
            'Toplam Taksi Sayısı',
            '${_deviceLastInfoModel.deviceLastInfo.length}',
          ),
          _rows(
            width,
            height,
            'Taksimetresi Kapalı Taksi Sayısı',
            '${closedTaxiStatus}',
          ),
          _rows(
            width,
            height,
            'Anlık Kiralanmış Taksi Sayısı',
            '${hiredTaxiStatus}',
          ),
          _rows(
            width,
            height,
            'Anlık Boşta Taksi Sayısı',
            '${idleTaxiStatus}',
          ),
        ],
      ),
    );
  }

//Günlük Kazanç Durumu
  Widget _daySumFareContainer(double width, double height, String text) {
    return Container(
      width: width * 0.45,
      height: height * 0.5,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(5),
        border: Border.all(width: 1),
        color: Colors.blue.shade200,
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          _infoContainer(width, height, text),
          _rows(
            width,
            height,
            'Tarih',
            '${DateFormat('dd/MM/yyy').format(DateTime.now())}',
          ),
          _rows(
            width,
            height,
            'Günlük Toplam Kazanç',
            '${fleetInfoModel.fleetInfo.daySumFare / 1000} TL',
          ),
          _rows(
            width,
            height,
            'Günlük Toplam Kiralama Sayısı',
            '${fleetInfoModel.fleetInfo.dayRentCount}',
          ),
          SizedBox(height: height * 0.001),
        ],
      ),
    );
  }

//Aylık Kazanç Durumu
  Widget _mounthSumFareContainer(double width, double height, String text) {
    return Container(
      width: width * 0.45,
      height: height * 0.5,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(5),
        border: Border.all(width: 1),
        color: Colors.blue.shade200,
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          _infoContainer(width, height, text),
          _rows(
            width,
            height,
            'Tarih',
            '${DateFormat('MM/yyy').format(DateTime.now())}',
          ),
          _rows(
            width,
            height,
            'Aylık Toplam Kazanç',
            '${fleetInfoModel.fleetInfo.monthsFare / 1000} TL',
          ),
          _rows(
            width,
            height,
            'Aylık Toplam Kiralama Sayısı',
            '${fleetInfoModel.fleetInfo.monthsRentCount}',
          ),
          SizedBox(height: height * 0.001),
        ],
      ),
    );
  }

// text : answer row yapısı
  Widget _rows(double width, double height, String text, String answerText) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        _texts(width, height, text),
        _twoDots(width, height),
        _answers(width, height, answerText),
      ],
    );
  }

//customText
  Widget _texts(double width, double height, String text) {
    return Padding(
      padding: const EdgeInsets.all(3),
      child: Container(
        width: width * 0.25,
        height: height * 0.07,
        child: Text(
          text,
          style: TextStyle(
            fontSize: height * 0.04,
            fontWeight: FontWeight.bold,
          ),
        ),
      ),
    );
  }

//: yapısı
  Widget _twoDots(double width, double height) {
    return Padding(
      padding: const EdgeInsets.all(3),
      child: Container(
        width: width * 0.05,
        height: height * 0.07,
        child: Text(
          ':',
          style: TextStyle(
            fontSize: height * 0.04,
            fontWeight: FontWeight.bold,
          ),
        ),
      ),
    );
  }

//Apiden gelen cevap
  Widget _answers(double width, double height, String text) {
    return Padding(
      padding: const EdgeInsets.all(3.0),
      child: Container(
        width: width * 0.1,
        height: height * 0.07,
        alignment: Alignment.centerRight,
        child: Text(
          text,
          style: TextStyle(
            fontSize: height * 0.04,
            fontWeight: FontWeight.bold,
          ),
        ),
      ),
    );
  }

//Container Başlıkları
  Widget _infoContainer(double width, double height, String text) {
    return Container(
      width: width * 0.45,
      height: height * 0.1,
      alignment: Alignment.center,
      decoration: BoxDecoration(
        color: Colors.blue.shade700,
        borderRadius: BorderRadius.circular(5),
      ),
      child: _infoTexts(text, height),
    );
  }

  @override
  void dispose() {
    mapController.dispose();
    super.dispose();
  }

  Text _infoTexts(String text, double height) {
    return Text(
      text,
      style: TextStyle(
        color: Colors.white,
        fontSize: height * 0.06,
        fontWeight: FontWeight.bold,
      ),
    );
  }
}