data_usage 1.0.0 copy "data_usage: ^1.0.0" to clipboard
data_usage: ^1.0.0 copied to clipboard

outdated

A Flutter plugin to fetch data usage on mobile devices

example/lib/main.dart

import 'dart:io';

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Home(),
    );
  }
}

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  List<DataUsageModel> _dataUsage = [];
  IOSDataUsageModel _dataiOSUsage;

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

  Future<void> initPlatformState() async {
    List<DataUsageModel> dataUsage;
    IOSDataUsageModel dataiOSUsage;
    try {
      await DataUsage.init();
      dataUsage = await DataUsage.dataUsageAndroid(
          withAppIcon: true, dataUsageType: DataUsageType.wifi);
      dataiOSUsage = await DataUsage.dataUsageIOS();
    } catch (e) {
      print(e.toString());
    }

    if (!mounted) return;

    setState(() {
      _dataUsage = dataUsage;
      _dataiOSUsage = dataiOSUsage;
    });
  }

  @override
  Widget build(BuildContext context) {
    var size = MediaQuery.of(context).size;
    return Scaffold(
      appBar: AppBar(
        title: const Text('Data Usage Plugin Example'),
      ),
      body: Center(
        child: Platform.isAndroid
            ? Android(dataUsage: _dataUsage, size: size)
            : Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: _dataiOSUsage
                        ?.toJson()
                        ?.entries
                        ?.map((e) => Text(
                              '${e.key}: ${e.value}',
                              overflow: TextOverflow.ellipsis,
                            ))
                        ?.toList() ??
                    []),
      ),
    );
  }
}

class Android extends StatelessWidget {
  const Android({
    Key key,
    @required List<DataUsageModel> dataUsage,
    @required this.size,
  })  : _dataUsage = dataUsage,
        super(key: key);

  final List<DataUsageModel> _dataUsage;
  final Size size;

  @override
  Widget build(BuildContext context) {
    return ListView(
      children: [
        if (_dataUsage != null)
          for (var item in _dataUsage) ...[
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  SizedBox(width: 10),
                  if (item.appIconBytes != null)
                    Container(
                      height: 60,
                      width: 60,
                      decoration: BoxDecoration(
                        image: DecorationImage(
                          image: MemoryImage(item.appIconBytes),
                        ),
                      ),
                    ),
                  SizedBox(width: 10),
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        width: size.width * 0.7,
                        child: Text(
                          '${item.appName}',
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                      SizedBox(height: 10),
                      Container(
                        width: size.width * 0.7,
                        child: Text(
                          '${item.packageName}',
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(color: Colors.grey, fontSize: 11),
                        ),
                      ),
                      SizedBox(height: 6),
                      Row(
                        children: [
                          Text(
                            'Received: ${item.received}  ',
                            style: TextStyle(
                                color: Colors.grey[700], fontSize: 13),
                          ),
                          Text(
                            'Sent: ${item.sent}',
                            style: TextStyle(
                                color: Colors.grey[700], fontSize: 13),
                          ),
                        ],
                      ),
                    ],
                  ),
                ],
              ),
            ),
            Divider()
          ]
      ],
    );
  }
}
22
likes
0
pub points
59%
popularity

Publisher

verified publishercodenka.com

A Flutter plugin to fetch data usage on mobile devices

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, permissions_plugin

More

Packages that depend on data_usage