Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.5 kB
1
Indexable
Never
import 'package:awesome_dialog/awesome_dialog.dart';
import 'package:flutter/material.dart';
import 'package:parstaksimapp/Components/constants.dart';
import 'package:parstaksimapp/Components/custom_appbar.dart';
import 'package:parstaksimapp/Components/custom_button.dart';
import 'package:parstaksimapp/Components/custom_dialog.dart';
import 'package:parstaksimapp/Components/custom_text_field.dart';
import 'package:parstaksimapp/Services/web_service.dart';
import 'package:parstaksimapp/Views/Menu/menu.dart';
import 'package:parstaksimapp/Views/SplashScreen/splash_screen.dart';
import 'package:shared_preferences/shared_preferences.dart';

class AddDevice extends StatefulWidget {
  final String widgetemail;
  const AddDevice({Key key, this.widgetemail}) : super(key: key);

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

class _AddDeviceState extends State<AddDevice> {
  //controllers for text fields
  TextEditingController deviceNameController = TextEditingController();
  TextEditingController deviceImeiController = TextEditingController();
  //to save email
  String email;
  getEmail() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    setState(() {
      email = prefs.getString("email");
    });
  }

  @override
  void initState() {
    getEmail();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('HARİTA'),
        centerTitle: true,
        automaticallyImplyLeading: false,
        backgroundColor: Colors.blue.shade900,
        leading: Menu(
          email: email,
        ),
      ),
      body: Center(
        child: SingleChildScrollView(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              //device name
              CustomTextField(
                  padding: 10,
                  hintText: "Cihaz İsmi Giriniz",
                  controller: deviceNameController,
                  obscureText: false,
                  suffixIcon: const Icon(
                    Icons.device_unknown_rounded,
                    color: primaryColor,
                  ),
                  keyboardType: TextInputType.text,
                  labelText: "Cihaz İsmi",
                  height: 1),
              //device imei
              CustomTextField(
                  padding: 10,
                  hintText: "Cihaz Seri Numarasını Giriniz",
                  controller: deviceImeiController,
                  obscureText: false,
                  suffixIcon: const Icon(
                    Icons.payments_sharp,
                    color: primaryColor,
                  ),
                  keyboardType: TextInputType.text,
                  labelText: "Cihaz Seri No",
                  height: 1),
              //a button to add
              CustomButton(
                  label: "Cihaz Ekle",
                  onPressed: () async {
                    if (deviceImeiController.text.isEmpty ||
                        deviceImeiController.text.isEmpty) {
                      CustomDialog(context, "Boş alan bırakmayınız", () {},
                          "Tamam", null, null, DialogType.ERROR);
                    } else {
                      await WebService.addDevice(
                              email,
                              deviceNameController.text,
                              deviceImeiController.text)
                          .then((value) {
                        if (value != null) {
                          CustomDialog(context, "Araç Başarıyla Eklendi", () {
                            Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) => SplashScreen(
                                        email: email,
                                      )),
                            );
                          }, "Tamam", null, null, DialogType.SUCCES);
                        } else {
                          CustomDialog(context, "İşlem Başarısız", () {},
                              "Tamam", null, null, DialogType.ERROR);
                        }
                      });
                    }
                  },
                  padding: 10,
                  color: primaryColor)
            ],
          ),
        ),
      ),
    );
  }
}