userx_flutter 1.3.15 copy "userx_flutter: ^1.3.15" to clipboard
userx_flutter: ^1.3.15 copied to clipboard

UserX Mobile app UX-analytics

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter_userx_example/native_text_view.dart';
import 'package:userx_flutter/occlude_wrapper.dart';
import 'package:userx_flutter/userx_flutter.dart';
import 'package:userx_flutter/attributes/attribute.dart';

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String? _platformVersion = 'Unknown';
  static const platform = MethodChannel('com.example.native_view/channel');
  String _nativeMessage = '';

  @override
  void initState() {
    super.initState();
    initPlatformState();

    UserX.start("0c25f625-10a7-4ae4-a824-76568599da29");
    UserX.addEvent("TestEvent", {"AppState": "Started"});
    UserX.setUserId("TestUserId");

    UserX.applyUserAttributes([
      Attribute.bool("logged_in", value: true),
      Attribute.string("user_name", value: "j2001"),
      Attribute.int("age", value: 25),
      Attribute.double("weight", value: 77),
      Attribute.counter("launch_count", value: 3),
    ]);

    UserX.applyUserAttributes([
      Attribute.increasedCounter("launch_count"),
    ]);
  }

  // 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 UserX.platformVersion;
    } 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) {
    // Расскомментировать если необхожимо получить вывод о размерах экрана
    // logScreenInfo(context);
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: OccludeWrapper(child: const Text('Plugin example app')),
        ),

        // Body (required)
        body: Center(
          child: SizedBox(
            height: 200,
            child: NativeTextView(),
          ),
        ),

        // Floating Action Button (optional)
        floatingActionButton: FloatingActionButton(
          onPressed: () {},
          child: Icon(Icons.add),
          backgroundColor: Colors.blue,
        ),

        // Drawer (optional)
        drawer: Drawer(
          child: ListView(
            children: [
              DrawerHeader(
                child: Text('Menu'),
                decoration: BoxDecoration(
                  color: Colors.blue,
                ),
              ),
              ListTile(
                title: Text('Item 1'),
                onTap: () {},
              ),
              ListTile(
                title: Text('Item 2'),
                onTap: () {},
              ),
            ],
          ),
        ),
      ),
    );
  }

  void _onPlatformViewCreated(int id) {
    platform.setMethodCallHandler(_handleMethodCall);
  }

  Future<void> _handleMethodCall(MethodCall call) async {
    switch (call.method) {
      case 'onButtonClicked':
        setState(() {
          _nativeMessage = call.arguments['message'];
        });
        break;
    }
  }
}