cardoctor_sdk_sos 1.0.7 copy "cardoctor_sdk_sos: ^1.0.7" to clipboard
cardoctor_sdk_sos: ^1.0.7 copied to clipboard

Sos sdk for CarDoctor

example/lib/main.dart

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:cardoctor_sdk_sos/cardoctor_sdk_sos.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.blue,
      ),
      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 = '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 = '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,
      imageUrl: IMAGE_URL,
      mapKey: MAP_KEY,
    );
    final sdkSos = CarDoctorSdkSos(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,
      });
      setState(() {
        loading = false;
      });
      return jsonDecode(res.body)['access_token'];
    } catch (e) {
      setState(() {
        loading = false;
      });
      final res = await Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) {
            return const ConnectionErrorWidget();
          },
        ),
      );
      if (res != null) {
        openCarDoctorSos(context);
      }
    }
    return null;
  }
}