vivanta_connect_flutter 0.5.0+6 copy "vivanta_connect_flutter: ^0.5.0+6" to clipboard
vivanta_connect_flutter: ^0.5.0+6 copied to clipboard

Vivanta Connect for Flutter

example/lib/main.dart

import 'package:example/vivanta.dart';
import 'package:flutter/material.dart';
import 'package:vivanta_connect_flutter/helpers/vivanta_sync.dart';
import 'package:vivanta_connect_flutter/services/preferences.dart';
import 'package:vivanta_connect_flutter/styles/colors.dart';
import 'package:vivanta_connect_flutter/styles/fonts.dart';
import 'package:vivanta_connect_flutter/views/embedded_graph.dart';
import 'package:vivanta_connect_flutter/vivanta_connect_flutter.dart';

void main() => runApp(const Example());

class Example extends StatelessWidget {
  const Example({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Vivanta Connect Example',
      home: Home(),
    );
  }
}

class Home extends StatefulWidget {
  Home({Key? key}) : super(key: key);

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  String companyId = 'Vivanta';
  final Preferences preferences = Preferences();
  late TextEditingController _controller;
  late FocusNode _focusNode;

  @override
  void initState() {
    _controller = TextEditingController();
    _focusNode = FocusNode();
    initAsync();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Vivanta Connect Example'),
      ),
      body: Container(
        width: MediaQuery.of(context).size.width,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            _box(),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 16),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  Align(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      'External User ID:',
                      textAlign: TextAlign.start,
                    ),
                  ),
                  TextField(
                    keyboardType: TextInputType.text,
                    controller: _controller,
                    focusNode: _focusNode,
                    onEditingComplete: () {
                      FocusScope.of(context).unfocus();
                    },
                  ),
                ],
              ),
            ),
            _box(),
            TextButton(
              onPressed: _controller.text.isEmpty
                  ? null
                  : () {
                      Navigator.of(context).push(
                        MaterialPageRoute(
                          builder: (context) => VivantaConnectFlutter(
                            apiKey: Vivanta.apiKey,
                            customerId: Vivanta.customerId,
                            externalUserId: _controller.text,
                            companyId: companyId,
                          ),
                        ),
                      );
                    },
              style: _buttonStyle(),
              child: Text(
                'Connect your device with Vivanta',
                textAlign: TextAlign.center,
                style: VivantaFonts.button,
              ),
            ),
            _box(),
            TextButton(
              onPressed: _controller.text.isEmpty ? null : _sync,
              style: _buttonStyle(),
              child: Text(
                'Sync Apple Health Data',
                textAlign: TextAlign.center,
                style: VivantaFonts.button,
              ),
            ),
            _box(),
            _getButtonToGraph(
              'Open activity graph',
              GraphType.activity,
            ),
            _box(),
            _getButtonToGraph(
              'Open active time graph',
              GraphType.activeTime,
            ),
            _box(),
            _getButtonToGraph(
              'Open sleep graph',
              GraphType.sleep,
            ),
          ],
        ),
      ),
    );
  }

  Future<void> initAsync() async {
    final externalUserId = await preferences.getExternalUserId();
    if (externalUserId.isNotEmpty) {
      _controller.text = externalUserId;
      setState(() {});
      _sync(showSuccess: false);
    }
  }

  ButtonStyle _buttonStyle() => ButtonStyle(
        backgroundColor: MaterialStateProperty.all<Color>(
          VivantaColors.primary,
        ),
        fixedSize: MaterialStateProperty.all<Size>(
          Size(MediaQuery.of(context).size.width - 32, 32),
        ),
      );

  Widget _box() => SizedBox(height: 24);

  Widget _getButtonToGraph(String text, GraphType graphType) {
    return TextButton(
      onPressed: _controller.text.isEmpty
          ? null
          : () {
              Navigator.of(context).push(
                MaterialPageRoute(
                  builder: (context) => EmbeddedGraph(
                    apiKey: Vivanta.apiKey,
                    customerId: Vivanta.customerId,
                    externalUserId: _controller.text,
                    graphType: graphType,
                  ),
                ),
              );
            },
      style: _buttonStyle(),
      child: Text(
        text,
        textAlign: TextAlign.center,
        style: VivantaFonts.button,
      ),
    );
  }

  Future<void> _sync({
    bool showSuccess = true,
  }) async {
    final sync = VivantaSync(
      apiKey: Vivanta.apiKey,
      customerId: Vivanta.customerId,
      externalUserId: _controller.text,
    );
    sync.executeAll().then((value) {
      if (showSuccess) {
        showModalBottomSheet(
          context: context,
          builder: (context) {
            return Container(
              height: 100,
              child: Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    const Text('Data sync finished successfully'),
                    SizedBox(
                      height: 8,
                    ),
                    ElevatedButton(
                      child: const Text('Close'),
                      onPressed: () => Navigator.pop(context),
                    ),
                    SizedBox(
                      height: 8,
                    ),
                  ],
                ),
              ),
            );
          },
        );
      }
    });
  }
}