zhuge 1.0.5
zhuge: ^1.0.5 copied to clipboard
This is the official flutter plugin for Zhugeio,with this plugin you can easily collect your app data on Android and iOS.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:zhuge/zhuge.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
late TextEditingController userNameTF;
late TextEditingController userKeyTF;
late TextEditingController userValueTF;
late TextEditingController eventTF;
late TextEditingController eventKeyTF;
late TextEditingController eventValueTF;
late TextEditingController eventKeyTF2;
late TextEditingController eventValueTF2;
late TextEditingController priceTF;
late TextEditingController productIdTF;
late TextEditingController productQuantityTF;
late TextEditingController typeTF;
@override
void initState() {
super.initState();
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.
// We also handle the message potentially returning null.
try {
platformVersion =
await Zhuge.platformVersion ?? 'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// 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;
setState(() {
_platformVersion = platformVersion;
});
}
@override
Widget build(BuildContext context) {
userNameTF = new TextEditingController();
userKeyTF = new TextEditingController();
userValueTF = new TextEditingController();
eventTF = new TextEditingController();
eventKeyTF = new TextEditingController();
eventValueTF = new TextEditingController();
eventKeyTF2 = new TextEditingController();
eventValueTF2 = new TextEditingController();
priceTF = new TextEditingController();
productIdTF = new TextEditingController();
productQuantityTF = new TextEditingController();
typeTF = new TextEditingController();
_buildButton(int tag, String title) {
return new Container (
margin: EdgeInsets.only(top: 10.0, left: 15.0),
height: 30.0,
width: 150.0,
child: new MaterialButton(
color: Colors.blue,
textColor: Colors.white,
child: new Text(title),
onPressed: () {
clickButton(tag);
},
),
);
}
_buildTF(String hintText, TextEditingController _tf) {
return new Container(
margin: EdgeInsets.only(top: 5.0,left: 15.0,right: 15),
height: 39.0,
// SingleChildScrollView
child: new TextField(
controller: _tf,
decoration: InputDecoration(
contentPadding: EdgeInsets.all(5.0),
border: OutlineInputBorder (
borderRadius: BorderRadius.circular(4.0),
),
fillColor: Colors.white70,
filled: true,
hintText: hintText,
),
),
);
}
_buildTitle(String title) {
return new Container(
margin: EdgeInsets.only(top: 20.0,left: 15.0,right: 15.0),
child: Text(
title,
style: TextStyle(fontWeight: FontWeight.bold),
),
);
}
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin Zhuge app'),
),
body: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
//用户追踪
_buildTitle("UserID: "),
_buildTF("用户名", userNameTF),
Row(
children: <Widget>[
new Expanded(child: _buildTF("key", userKeyTF)),
new Expanded(child: _buildTF("value", userValueTF)),
],
),
_buildButton(0, "追踪用户"),
//自定义事件追踪
_buildTitle("自定义事件:"),
_buildTF("事件名称", eventTF),
Row(
children: <Widget>[
new Expanded(
child: _buildTF("Key1",eventKeyTF),
),
new Expanded(
child: _buildTF("Value1",eventValueTF),
),
],
),
Row(
children: <Widget>[
new Expanded(
child: _buildTF("Key2",eventKeyTF2),
),
new Expanded(
child: _buildTF("Value2", eventValueTF2),
),
],
),
_buildButton(1,"追踪事件"),
//收入事件统计
_buildTitle("收入事件:"),
Row(
children: <Widget>[
new Expanded(
child: _buildTF("Price",priceTF),
),
new Expanded(
child: _buildTF("ProductQuantity",productQuantityTF),
),
],
),
Row(
children: <Widget>[
new Expanded(
child: _buildTF("ProductId",productIdTF),
),
new Expanded(
child: _buildTF("Type",typeTF),
),
],
),
_buildButton(2,"追踪收入"),
],
),
)
),
);
}
clickButton(int tag) {
if(tag == 0) {
// Zhugeio.identify("zhangsan", {"age":18});
Zhuge.identify(userNameTF.text, getUserInfo());
} else if (tag == 1) {
Zhuge.track(eventTF.text, getDefaultEventsInfo());
} else if (tag == 2) {
Zhuge.trackRevenue(getRevenueEventsInfo());
}
}
Map getUserInfo() {
Map userPro;
userPro = {userNameTF.text:userValueTF.text};
print( userNameTF.text + "== $userPro" );
return userPro;
}
Map getDefaultEventsInfo() {
Map defaultPro;
defaultPro = {eventKeyTF.text:eventValueTF.text, eventKeyTF2.text:eventValueTF2.text};
print( eventTF.text + "== $defaultPro" );
return defaultPro;
}
Map getRevenueEventsInfo() {
Map revenuePro;
// revenuePro.put("price",100.12); //数值型属性不要带引号
// revenuePro.put("productID","小米NFC手环");
// revenuePro.put("productQuantity",2); //数值型属性不要带引号
// revenuePro.put("revenueType","手环");
revenuePro = {"price":priceTF.text,
"productQuantity":productQuantityTF.text,
"productID":productIdTF.text,
"revenueType":typeTF.text};
print("RevenuePro == $revenuePro" );
return revenuePro;
}
}