Skip to content

Instantly share code, notes, and snippets.

@raunakhajela
Last active January 18, 2019 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raunakhajela/af008f8c065c49c03ed0fa2fc722ca1a to your computer and use it in GitHub Desktop.
Save raunakhajela/af008f8c065c49c03ed0fa2fc722ca1a to your computer and use it in GitHub Desktop.
weight_tracker flutter app
import 'package:flutter/material.dart';
import 'dart:math';
import 'package:intl/intl.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// var _routes = <String, WidgetBuilder>{
// AddContact.routeName: (BuildContext context) => new AddContact(title: 'Add Contact'),
// };
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Weight Tracker',
theme: ThemeData(
primarySwatch: Colors.green,
),
home: MyHomePage(title: 'Weight Tracker'),
// routes: _routes,
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<WeightSave> weightSaves = new List();
void _addWeightSave() {
setState(() {
weightSaves.add(new WeightSave(
new DateTime.now(), new Random().nextInt(100).toDouble()));
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: new ListView(
children: weightSaves.map((WeightSave weightSave) {
//calculating difference
double difference = weightSaves.first == weightSave
? 0.0
: weightSave.weight -
weightSaves[weightSaves.indexOf(weightSave)].weight;
}).toList(),
),
floatingActionButton: new FloatingActionButton(
onPressed: () => _addWeightSave(),
tooltip: 'Add new weight entry',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
class WeightSave {
DateTime dateTime;
double weight;
WeightSave(this.dateTime, this.weight);
}
class WeightListItem extends StatelessWidget {
final WeightSave weightSave;
final double weightDifference;
WeightListItem(this.weightSave, this.weightDifference);
@override
Widget build(BuildContext context) {
return new Padding(
padding: new EdgeInsets.all(16.0),
child: new Row(children: [
new Expanded(
child: new Column(children: [
new Text(
new DateFormat.yMMMMd().format(weightSave.dateTime),
textScaleFactor: 0.9,
textAlign: TextAlign.left,
),
new Text(
new DateFormat.EEEE().format(weightSave.dateTime),
textScaleFactor: 0.8,
textAlign: TextAlign.right,
style: new TextStyle(
color: Colors.grey,
),
),
], crossAxisAlignment: CrossAxisAlignment.start)),
new Expanded(
child: new Text(
weightSave.weight.toString(),
textScaleFactor: 2.0,
textAlign: TextAlign.center,
)),
new Expanded(
child: new Text(
weightDifference.toString(),
textScaleFactor: 1.6,
textAlign: TextAlign.right,
)),
]),
);
}
}
name: tutorial
description: A new Flutter project.
# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# Read more about versioning at semver.org.
version: 1.0.0+1
environment:
sdk: ">=2.0.0-dev.68.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
intl: ^0.15.7
dev_dependencies:
flutter_test:
sdk: flutter
# For information on the generic Dart part of this file, see the
# following page: https://www.dartlang.org/tools/pub/pubspec
# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.io/assets-and-images/#resolution-aware.
# For details regarding adding assets from package dependencies, see
# https://flutter.io/assets-and-images/#from-packages
# To add custom fonts to your application, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts from package dependencies,
# see https://flutter.io/custom-fonts/#from-packages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment