dart_unit_converter 0.0.3+1 copy "dart_unit_converter: ^0.0.3+1" to clipboard
dart_unit_converter: ^0.0.3+1 copied to clipboard

A data unit converter package.

example/lib/main.dart

// ignore_for_file: avoid_print, depend_on_referenced_packages

import 'package:dart_unit_converter/dart_unit_converter.dart';
import 'package:flutter/material.dart';
import 'package:intl/date_symbol_data_local.dart' as date_local_symbol;

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Example Data Converter Apps',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const HomePage(),
    );
  }
}

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

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final converter = DataConverter();
  final dataInputController = TextEditingController();
  final dataValueController = TextEditingController();
  final timeInputController = TextEditingController();
  final timeValueController = TextEditingController();

    /// EXAMPLE OF DATA CONVERTER -- [parsedDataValue]
    void useConverterFromByte() {
      final parsedDataValue = int.parse(dataInputController.text);

      // FROM EXTENSION
      dataValueController.text = parsedDataValue.convertDataToString();

      // FROM FUNCTION
      dataValueController.text = converter.continousDataConvert(parsedDataValue);

      // CUSTOM CONVERT
      dataValueController.text = converter
          .convert(parsedDataValue, TypeDataConvert.bit, TypeDataConvert.megaByte)
          .toString();
    }

  /// EXAMPLE OF TIME CONVERTER -- [parsedTimeValue]
  void useConverterFromSeconds() {
    final parsedTimeValue = timeInputController.text;
    timeValueController.text = parsedTimeValue.toStringTimeDiff();
  }

  @override
  void initState() {
    super.initState();
    date_local_symbol.initializeDateFormatting();
    timeInputController.text = '2022-08-22 20:18:04Z';
    setDefaultMessage(MessageLocalType.id);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Example Apps'),
      ),
      body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.all(18.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              byteToStringSection(),
              const SizedBox(height: 50),
              timeConvertSection(),
            ],
          ),
        ),
      ),
    );
  }

  // TIME CONVERT EXAMPLE
  Widget timeConvertSection() {
    return Column(
      children: [
        const SizedBox(height: 24),
        const Text(
          'From DateTime Pared to Customable String',
          textAlign: TextAlign.justify,
          style: TextStyle(
            fontSize: 16,
          ),
        ),
        Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Expanded(
              flex: 2,
              child: TextFormField(
                textAlign: TextAlign.center,
                controller: timeInputController,
              ),
            ),
          ],
        ),
        const Padding(
          padding: EdgeInsets.fromLTRB(10, 20, 10, 0),
          child: Text('is'),
        ),
        TextFormField(
          controller: timeValueController,
          textAlign: TextAlign.center,
          enabled: false,
        ),
        const SizedBox(height: 10),
        ElevatedButton(
          onPressed: useConverterFromSeconds,
          child: const Text('Convert'),
        ),
      ],
    );
  }

  /// DATA CONVERT EXAMPLE [converter]
  Widget byteToStringSection() {
    return Column(
      children: [
        const SizedBox(height: 24),
        const Text(
          'From Byte Value to String',
          textAlign: TextAlign.center,
          style: TextStyle(
            fontSize: 16,
          ),
        ),
        Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Expanded(
              flex: 2,
              child: TextFormField(
                controller: dataInputController,
                keyboardType: TextInputType.number,
                onChanged: (value) {
                  if (value != '') {
                    useConverterFromByte();
                  } else {
                    dataValueController.text = '';
                  }
                },
              ),
            ),
            const Padding(
              padding: EdgeInsets.fromLTRB(10, 20, 10, 0),
              child: Text('='),
            ),
            Expanded(
              flex: 1,
              child: TextFormField(
                controller: dataValueController,
                keyboardType: TextInputType.number,
                textAlign: TextAlign.center,
                enabled: false,
              ),
            ),
          ],
        )
      ],
    );
  }
}
2
likes
130
points
8
downloads

Publisher

verified publishervarx.id

Weekly Downloads

A data unit converter package.

Repository (GitLab)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter, intl

More

Packages that depend on dart_unit_converter