car_doctor_epass_sos 1.1.0 copy "car_doctor_epass_sos: ^1.1.0" to clipboard
car_doctor_epass_sos: ^1.1.0 copied to clipboard

Sos sdk for CarDoctor

example/lib/main.dart

import 'dart:convert';

import 'package:car_doctor_epass_sos/car_doctor_epass_sos.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart';


void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.orange,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool loading = false;

  // final String LOGIN_URL = 'https://stg-sso-api.cardoctor.com.vn/oauth/token';
  // final String API_URL = 'https://stg-api.cardoctor.com.vn';
  final String API_URL = 'https://api.cardoctor.com.vn';
  final String LOGIN_URL = 'https://sso-api.cardoctor.com.vn/oauth/token';
  // final String API_URL = 'https://api.cardoctor.com.vn';
  final String SSO_URL = 'https://sso-api.cardoctor.com.vn';
  final String IMAGE_URL = 'https://api.cardoctor.com.vn/cms/files/';
  // final String LOGIN_URL = 'http://demo.aggregatoricapaci.com:9999/oauth/token';
  // final String API_URL = 'http://demo.aggregatoricapaci.com:56789';
  // final String IMAGE_URL = 'http://demo.aggregatoricapaci.com:56789/cms/files/';
  final String MAP_KEY = 'd353070a772a526aa6bf282e766aef16';
  final String USERNAME = 'BOSS';
  final String PASSWORD = r'abc@1235';
  // final String USERNAME = '0985684955';
  // final String PASSWORD = r'abc@123';
  final String BASIC_AUTHORIZATION = 'Basic Q2FyRG9jdG9yOjJxYXpYU1dAM2VkY1ZGUiQ1dGdiTkhZXjd1am08S0kqOW9sLj86UCk6KSk=';
  // final String LOGIN_URL = '';
  // final String API_URL = '';
  // final String IMAGE_URL = '';
  // final String MAP_KEY = '';
  // final String USERNAME = '';
  // final String PASSWORD = r'';
  // final String BASIC_AUTHORIZATION = '';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Builder(builder: (context) {
        return Center(
          child: loading
              ? const SizedBox(
                  width: 60,
                  height: 60,
                  child: CircularProgressIndicator(),
                )
              : GestureDetector(
                  onTap: () async {
                    await openCarDoctorSos(context);
                  },
                  child: const SosIcon(),
                ),
        );
      }),
    );
  }

  Future openCarDoctorSos(BuildContext context) async {
    String? token = await getToken(context);
    if (token == null) return;
    final option = CarDoctorOption(
      bearerToken: token,
      apiUrl: API_URL,
      sosUrl: '',
      imageUrl: IMAGE_URL,
      mapKey: MAP_KEY,
    );
    final sdkSos = CarDoctorSdkSos.instance;
    sdkSos.init(option);
    sdkSos.open(context);
  }

  Future<String?> getToken(BuildContext context) async {
    Map<String, String> headers = {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Authorization': BASIC_AUTHORIZATION,
    };
    setState(() {
      loading = true;
    });
    try {
      final res = await post(Uri.parse(LOGIN_URL), headers: headers, body: {
        'grant_type': 'password',
        'username': USERNAME,
        'password': PASSWORD,
      });
      debugPrint('res: ${res.body}');
      setState(() {
        loading = false;
      });
      return jsonDecode(res.body)['access_token'];
    } catch (e) {
      debugPrint('error: ${e.toString()}');
      setState(() {
        loading = false;
      });
      final res = await Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) {
            return const ConnectionErrorWidget();
          },
        ),
      );
      if (res != null) {
        openCarDoctorSos(context);
      }
    }
    return null;
  }
}