capture_flutter_beta 1.0.2 copy "capture_flutter_beta: ^1.0.2" to clipboard
capture_flutter_beta: ^1.0.2 copied to clipboard

unlistedoutdated

This is the beta version of Socket Mobile Inc's Flutter SDK (only works for android with companion).

example/example.dart

// ignore_for_file: avoid_print
import 'dart:convert';
// all classes are aggregated in index.dart
import 'package:capture_flutter_beta/classes/gen/property_ids_types.dart';
import 'package:flutter/material.dart';
import 'package:capture_flutter_beta/classes/index.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

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

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  String _status = 'starting';
  String _message = '--';
  List<Map> _devices = [];
  String _newName = '';
  // ignore: unused_field
  dynamic _capture, _newCapture, _currentScan;
  final _nameController = TextEditingController();

  var logger = Logger((message, arg) => {
    if(message != null && message.isNotEmpty) {
      print(message + " " + arg + '\n\n')
    } else {
      print(arg + '\n\n')
    }
  });

  void _updateVals (String stat, String mess){
      setState(() {
            _status = stat;
            _message = mess;
          });
  }

  @override
  void initState() {

    _nameController.addListener(_handleNameChange);

    final appInfo = AppInfo(
      'web:com.socketmobile.reactjs.native.example.example',
      'bb57d8e1-f911-47ba-b510-693be162686a',
      'MC0CFQC85w0MsxDng4wHBICX7+iCOiSqfAIUdSerA4/MJ2kYBGAGgIG/YHemNr8='
    );

    Capture capture = Capture(logger);

    setState((){
      _capture = capture;
    });

    var stat = _status;
    var mess = _message;

    capture.open(appInfo, _onCaptureEvent)
      .then((response) {
        if (response.runtimeType == JRpcAndIosError){
         stat = response['error']['code'].toString();
         mess = response['error']['message'];
        } else {
          var breh;
          if (response.runtimeType == int){
            breh = response;
          } else {
            breh = response['result']['handle'];
          }
          stat = 'handle: $breh';
          mess = 'capture open success';
        }
         _updateVals(stat, mess);
    });
    super.initState();
  }

  void _handleGetNameProperty(){
    // example using friendly name
    // to use another property, change id and type 
    // to correspond to desired property 
    // CapturePropertyIds
    var property = CaptureProperty(
      CapturePropertyIds().friendlyNameDevice, 
      CapturePropertyTypes().none, 
      {}
    );

    var stat = 'Status -- ', mess = 'Message -- ';

    _newCapture.getProperty(property).then((response){
      print(response);
      if (response['error'] != null){
        stat = response['error']['code'].toString();
        mess = response['error']['code'] == -32602 ? 'JSON parse error' : response['error']['message'];
      } else {
        stat = 'Get Property';
        mess = 'Successfully Retrieved "name" property for device.';
      }
       _updateVals(stat, mess);
    });

  }

  void _handleSetNameProperty(){
    // example using friendly name
    // to use another property, change id and type 
    // to correspond to desired property 
    var property = CaptureProperty(
      CapturePropertyIds().friendlyNameDevice, 
      CapturePropertyTypes().string, 
      _newName
    );

    _newCapture.setProperty(property).then((response){
      var stat = 'Status -- ', mess = 'Message -- ';
       if (response['error'] != null){
        stat = response['error']['code'].toString();
        mess = response['error']['code'] == -32602 ? 'JSON parse error' : response['error']['message'];
      } else {
        print('hellooooo');
        print(response);
        stat = 'Set Property';
        mess = 'Successfully set "name" property to "$_newName".';
        setState(() {
          _newName = '';
        });
        _nameController.text = '';
      }
      _updateVals(stat, mess);
    });
  }

  void _handleNameChange() {
   setState(() {
      _newName = _nameController.text;
   });
  }

  _onCaptureEvent(e, handle) {

    if (e == null) {
      return;
    }

    logger.log('onCaptureEvent from $handle: ', json.encode(e));

    switch (e['id']) {
      case CaptureEventIds.deviceArrival:
        {
          var newCapture = Capture(logger);

          setState(() {
            _newCapture = newCapture;
          });

          var arr = _devices;
          
          var guid = e['value']['guid'];
          var name = e['value']['name'];
          logger.log('Device Arrival =>', name + ' ($guid)');
         
          newCapture.openDevice(guid, _capture).then((res){
            print(res);
            if (res.runtimeType != int){
              _updateVals("${res['error']['code']}", res['error']['message'] );
            } else {
              if (!arr.contains(e['value'])){
                arr.add(e['value']);
                setState(() {
                  _devices = arr;
                });
              } 
              _handleGetNameProperty();
              _updateVals('Device Arrival', 'Successfully added "$name"');
            }
          });

        }
        break;

      case CaptureEventIds.deviceRemoval:
        {
          var guid = e['value']['guid'];
          var name = e['value']['name'];
          var arr = _devices;
          arr.removeWhere((element) => element['guid'] == guid);
          setState(() {
            _devices = arr;
          });
          logger.log('Device Removal =>', name + ' ($guid)');
          _updateVals('Device Closed', 'Successfully removed "$name"');
        }
        break;

      case CaptureEventIds.decodedData:
        {
          setState(() {
            //storing scanned data in state for future use
            _currentScan = json.encode(e);
          });
          _updateVals('Scanned Data', json.encode(e));
        }
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              Row(children: <Widget>[
                const Text(
                  'Status: ',
                ),
                Text(
                  _status,
                  style: Theme.of(context).textTheme.bodyText1,
                ),
              ]),
              Row(children: <Widget>[
                const Text(
                  'Message: ',
                ),
                Flexible(
                  child:Text(
                    _message,
                    style: Theme.of(context).textTheme.bodyText1,
                  ), 
                ),
              ]),
              
              Row (children: [
                SizedBox(
                  width: 300,
                  child: Padding(
                    padding: const EdgeInsets.only(
                      left: 40,
                      top: 20,
                      right: 40,
                      bottom: 20,
                    ),
                    child: TextField(
                      decoration: InputDecoration(
                      suffixIcon: IconButton(
                        icon: const Icon(Icons.add_box),
                        onPressed: _newName.isNotEmpty ? _handleSetNameProperty : null,
                      ),
                      labelText: 'Edit Device Name',
                      border: const OutlineInputBorder(),
                      ),
                      controller: _nameController,
                    ),
                  ),
                ),
                const Flexible(
                  child: Text('*illustrate the get/set property functionality, can be done with any device property')
                )
              ]),

              Row(children: const <Widget>[
                 Text(
                  'Devices',
                ),
              ]),
              _devices.isEmpty ? const Center(child: Text('No Devices Available')) : ListView.builder(
                  shrinkWrap: true,
                  padding: const EdgeInsets.only(left: 10, right: 10),
                  itemCount: _devices.length,
                  itemBuilder: (BuildContext context, int index){
                    return  Text(
                        '${index + 1}. ' + _devices[index]['name'] + ': ' + _devices[index]['guid']
                    );
                  },
                ),
            ],
          ),
        )
      );
  }
}
0
likes
0
points
141
downloads

Publisher

verified publishersocketmobile.com

Weekly Downloads

This is the beta version of Socket Mobile Inc's Flutter SDK (only works for android with companion).

Repository

License

unknown (license)

Dependencies

build_verify, cupertino_icons, flutter, http, json_annotation, web_socket_channel

More

Packages that depend on capture_flutter_beta