tcg_location 0.0.2 tcg_location: ^0.0.2 copied to clipboard
A new flutter plugin project.
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'dart:async';
import 'dart:isolate';
import 'dart:ui';
import 'package:flutter/services.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:f_logs/f_logs.dart';
import 'package:tcg_location/tcg_location.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
String _sampleData = 'Unknown';
bool _initialized = false;
List<String> _dataItems = new List<String>();
ReceivePort port = ReceivePort();
@override
void initState() {
super.initState();
IsolateNameServer.registerPortWithName(
port.sendPort, 'location_update_send_port');
port.listen((dynamic data) {
print('Event: $data');
setState(() {
FLog.info(className: 'Callback Data', methodName: 'static void callback(String data)', text: data);
_dataItems.add(data);
});
});
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
platformVersion = await TcgLocation.platformVersion;
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
String sampledata;
try {
sampledata = await TcgLocation.sampleLocation;
} on PlatformException {
sampledata = 'Failed to get sample data.';
}
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setInt('counter', 666);
print('Initializing...');
await TcgLocation.initialize();
print('Initialization done');
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
final logList = await FLog.getAllLogs();
final List<String> dataList = new List<String>();
logList.forEach((element) {
dataList.add(element.text + " " + element.timestamp);
});
//await FLog.clearLogs();
setState(() {
_platformVersion = platformVersion;
_sampleData = sampledata;
_dataItems.add("Running on: $_platformVersion");
_dataItems.add("Sample Data: $_sampleData");
_dataItems.addAll(dataList);
});
}
static void callback(String data) {
print("Received value from callback : $data");
try {
final SendPort send =
IsolateNameServer.lookupPortByName('location_update_send_port');
send?.send(data);
// No need to set state, we have what we need. Make network service call
} catch(Exception) {
print(Exception);
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Container(child:
Column(
children: [
Text('Total Log Count: ' + _dataItems.length.toString()),
OutlineButton(
child: Text('Clear logs'),
onPressed: () async {
//final bool initialized = await IosLocation.startLocation(locationCallback);
await FLog.clearLogs();
setState(() {
_dataItems = new List<String>();
_dataItems.add("Running on: $_platformVersion");
_dataItems.add("Sample Data: $_sampleData");
});
},
),
OutlineButton(
child: Text('Initialize'),
onPressed: () async {
//final bool initialized = await IosLocation.startLocation(locationCallback);
final bool initialized = await TcgLocation.startLocation(callback);
setState(() {
_initialized = initialized;
});
},
),
if(_initialized) OutlineButton(
child: Text('Stop'),
onPressed: () async {
final stopped = await TcgLocation.stopLocation();
setState(() {
_initialized = !stopped;
});
},
), if (_dataItems.isNotEmpty)
Expanded(child:
ListView.builder(itemBuilder: (context, index) {
final item = _dataItems[index];
return ListTile(title: Text(item));
},
itemCount: _dataItems.length),)
],
),),
),
),
);
}
}