flutter_mobile_diagnostics 0.1.0 copy "flutter_mobile_diagnostics: ^0.1.0" to clipboard
flutter_mobile_diagnostics: ^0.1.0 copied to clipboard

Device health diagnostics for Flutter. Identity plus battery, memory, storage, network, Bluetooth, security, and performance with JSON reports.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter_mobile_diagnostics/flutter_mobile_diagnostics.dart';
import 'package:flutter_mobile_diagnostics_example/pages/dashboard_page.dart';
import 'package:flutter_mobile_diagnostics_example/pages/domain_detail_page.dart';
import 'package:flutter_mobile_diagnostics_example/pages/json_preview_page.dart';
import 'package:flutter_mobile_diagnostics_example/pages/live_watch_page.dart';
import 'package:flutter_mobile_diagnostics_example/pages/settings_page.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(const DiagnosticsExampleApp());
}

class DiagnosticsExampleApp extends StatefulWidget {
  const DiagnosticsExampleApp({
    super.key,
    this.diagnostics = const MobileDiagnostics(),
    this.watchInterval = const Duration(seconds: 3),
  });

  final MobileDiagnostics diagnostics;
  final Duration watchInterval;

  @override
  State<DiagnosticsExampleApp> createState() => _DiagnosticsExampleAppState();
}

class _DiagnosticsExampleAppState extends State<DiagnosticsExampleApp> {
  DiagnosticsOptions _options = DiagnosticsOptions.defaults;
  DiagnosticsReport? _report;
  Object? _error;
  bool _loading = true;
  int _tabIndex = 0;
  String _platformVersion = 'unknown';

  @override
  void initState() {
    super.initState();
    _loadPlatformVersion();
    _refresh();
  }

  Future<void> _loadPlatformVersion() async {
    try {
      final version = await widget.diagnostics.getPlatformVersion();
      if (!mounted) {
        return;
      }
      setState(() => _platformVersion = version ?? 'unknown');
    } catch (_) {
      if (!mounted) {
        return;
      }
      setState(() => _platformVersion = 'unavailable');
    }
  }

  Future<void> _refresh() async {
    setState(() {
      _loading = true;
      _error = null;
    });
    MobileDiagnostics.clearCache();
    try {
      final report =
          await widget.diagnostics.getFullReport(options: _options);
      if (!mounted) {
        return;
      }
      setState(() {
        _report = report;
        _loading = false;
      });
    } catch (error) {
      if (!mounted) {
        return;
      }
      setState(() {
        _error = error;
        _loading = false;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Mobile Diagnostics',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xFF1565C0)),
        useMaterial3: true,
      ),
      home: HomeShell(
        tabIndex: _tabIndex,
        onTabChanged: (index) => setState(() => _tabIndex = index),
        loading: _loading,
        error: _error,
        report: _report,
        options: _options,
        platformVersion: _platformVersion,
        diagnostics: widget.diagnostics,
        watchInterval: widget.watchInterval,
        onRefresh: _refresh,
        onOptionsChanged: (options) {
          setState(() => _options = options);
        },
      ),
    );
  }
}

class HomeShell extends StatelessWidget {
  const HomeShell({
    super.key,
    required this.tabIndex,
    required this.onTabChanged,
    required this.loading,
    required this.error,
    required this.report,
    required this.options,
    required this.platformVersion,
    required this.diagnostics,
    required this.watchInterval,
    required this.onRefresh,
    required this.onOptionsChanged,
  });

  final int tabIndex;
  final ValueChanged<int> onTabChanged;
  final bool loading;
  final Object? error;
  final DiagnosticsReport? report;
  final DiagnosticsOptions options;
  final String platformVersion;
  final MobileDiagnostics diagnostics;
  final Duration watchInterval;
  final VoidCallback onRefresh;
  final ValueChanged<DiagnosticsOptions> onOptionsChanged;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('flutter_mobile_diagnostics'),
        actions: [
          IconButton(
            tooltip: 'Refresh full report',
            onPressed: loading ? null : onRefresh,
            icon: const Icon(Icons.refresh),
          ),
        ],
      ),
      body: switch (tabIndex) {
        1 => LiveWatchPage(
            diagnostics: diagnostics,
            options: options,
            interval: watchInterval,
          ),
        2 => JsonPreviewPage(
            diagnostics: diagnostics,
            options: options,
          ),
        3 => SettingsPage(
            options: options,
            platformVersion: platformVersion,
            onChanged: onOptionsChanged,
          ),
        _ => DashboardPage(
            loading: loading,
            error: error,
            report: report,
            onRefresh: onRefresh,
            onOpenDomain: (domain) {
              final current = report;
              if (current == null) {
                return;
              }
              Navigator.of(context).push(
                MaterialPageRoute<void>(
                  builder: (_) => DomainDetailPage(
                    domain: domain,
                    diagnostics: diagnostics,
                    options: options,
                    report: current,
                  ),
                ),
              );
            },
          ),
      },
      bottomNavigationBar: NavigationBar(
        selectedIndex: tabIndex,
        onDestinationSelected: onTabChanged,
        destinations: const [
          NavigationDestination(
            icon: Icon(Icons.monitor_heart_outlined),
            label: 'Dashboard',
          ),
          NavigationDestination(
            icon: Icon(Icons.sensors_outlined),
            label: 'Live',
          ),
          NavigationDestination(
            icon: Icon(Icons.data_object_outlined),
            label: 'JSON',
          ),
          NavigationDestination(
            icon: Icon(Icons.settings_outlined),
            label: 'Settings',
          ),
        ],
      ),
    );
  }
}
0
likes
0
points
98
downloads

Publisher

unverified uploader

Weekly Downloads

Device health diagnostics for Flutter. Identity plus battery, memory, storage, network, Bluetooth, security, and performance with JSON reports.

Repository (GitHub)
View/report issues

Topics

#diagnostics #device #battery #crash-reporting #health

License

unknown (license)

Dependencies

flutter, flutter_web_plugins, plugin_platform_interface, web

More

Packages that depend on flutter_mobile_diagnostics

Packages that implement flutter_mobile_diagnostics