Kleine App, die Hundejahre in Menschenjahre umrechnet

Kleine App, die Hundejahre in Menschenjahre umrechnet

Ich habe eine kleine Android-App geschrieben, die Hundejahre in Menschenjahre umrechnet. Die alte Regel "1 Hundejahr entspricht 7 Menschenjahren" stimmt nicht - zumindest nicht ganz. Welpen altern z.B. schneller als Menschenkinder.

Hier ist der Link zum Download der App: dogs_age.apk

Einen Eintrag im Google Playstore gibt es nicht, ich habe keine Google Developper Account.

Für diejenigen, die es interessiert, ist her der Code der zentralen "main.dart". Die App ist in Dart bzw. Flutter geschrieben.

Die Berechnungsroutine stamm von Karsten Johansson, @ksaj(at)infosec(dot)exchange - vielne Dank hierfür!

 

 

import 'package:flutter/material.dart';

import 'package:flutter/services.dart';

 

void main() {

  runApp(const MyApp());

}

 

class MyApp extends StatelessWidget {

  const MyApp({super.key});

 

  @override

  Widget build(BuildContext context) {

    return MaterialApp(

      title: 'Dog Age Calculation',

      theme: ThemeData(

        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),

        useMaterial3: true,

      ),

      home: const MyHomePage(title: 'Dog Age Calculation'),

    );

  }

}

 

class MyHomePage extends StatefulWidget {

  const MyHomePage({super.key, required this.title});

 

  final String title;

 

  @override

  State<MyHomePage> createState() => _MyHomePageState();

}

 

class _MyHomePageState extends State<MyHomePage> {

 

  double _humanAge = 0.0;

 

  final monthsController = TextEditingController(text: '0');

  final yearsController = TextEditingController(text: '0');

 

  @override

  void initState() {

    super.initState();

      monthsController.addListener(_calcAge);

      yearsController.addListener(_calcAge);

    }

 

    double _calcAge() {

     

    _humanAge = 0.0;

 

    /*

      Calculate the human age equivalent of

      a dog given its age in years and months

      as of Karsten Johansson, @ksaj(at)infosec(dot)exchange

    */

 

    int dogMonths = monthsController.text != ''

      ? int.parse(monthsController.text)

      : 0;

    if (dogMonths > 12) {

      dogMonths = 12;

      monthsController.text = '12';

    }

    final dogYears = yearsController.text != ''

      ? int.parse(yearsController.text)

      : 0;


 

    if (dogYears < 1) {

      _humanAge = dogMonths * (15 / 12);

    } else {

      if (dogYears == 1) {

        _humanAge = 15 + dogMonths * (9 / 12);

      } else {

        _humanAge = 15 + 9 + (dogYears - 2) * 5 + dogMonths * (5 / 12);

      }

    }

 

    setState(() {});

     

    return _humanAge;

  }

 

  @override

  Widget build(BuildContext context) {

 

    return Scaffold(

      appBar: AppBar(

        backgroundColor: Theme.of(context).colorScheme.inversePrimary,

        title: Text(widget.title),

      ),

      body: Center(

        child: Column(

          mainAxisAlignment: MainAxisAlignment.center,

          children: <Widget>[

           

            Text('Input the dog\'s age',

              style: Theme.of(context).textTheme.headlineMedium,

            ),

            const SizedBox(height: 20, ),

            Row(

              mainAxisAlignment: MainAxisAlignment.spaceAround,

              children: [

 

                SizedBox(

                  width: 150,

                  child: TextFormField(

                    textAlign: TextAlign.center,

                    controller: yearsController,

                    onTap: () => yearsController.selection = TextSelection(baseOffset: 0, extentOffset: yearsController.text.length),

                    keyboardType: TextInputType.number,

                    inputFormatters: <TextInputFormatter>[

                        FilteringTextInputFormatter.digitsOnly

                      ],

                    decoration: const InputDecoration(

                      border: OutlineInputBorder(),

                      labelText: 'Years',

                      hintText: 'Input Years',

                    ),

                  ),

                ),

 

                SizedBox(

                  width: 150,

                  child: TextFormField(

                    textAlign: TextAlign.center,

                    controller: monthsController,

                    onTap: () => monthsController.selection = TextSelection(baseOffset: 0, extentOffset: monthsController.text.length),

                    keyboardType: TextInputType.number,

                    inputFormatters: <TextInputFormatter>[

                        FilteringTextInputFormatter.digitsOnly

                      ],

                    decoration: const InputDecoration(                      

                      border: OutlineInputBorder(),

                      labelText: 'Months',

                      hintText: 'Input Months',

                    ),

                  ),

                ),

              ],

            ),

            const SizedBox(height: 20, ),

            const Text('Equivalent human age:',),

            Text(

              _humanAge.toStringAsFixed(2),

              style: Theme.of(context).textTheme.headlineMedium,

            ),

          ],

        ),

      ),

    );

  }

}

 

Voriger Beitrag Zurück zur Übersicht Nächster Beitrag