Skip to content
This repository has been archived by the owner on Apr 23, 2022. It is now read-only.

makes background collapsible #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ ios/.generated/
packages
pubspec.lock
.flutter-plugins

.project
6 changes: 3 additions & 3 deletions lib/header_painter.dart → lib/background_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import 'dart:math';

import 'package:flutter/material.dart';

class HeaderPainter extends CustomPainter {
HeaderPainter(this.animationValue, this.firstStarOffset);
class BackgroundPainter extends CustomPainter {
BackgroundPainter(this.animationValue, this.firstStarOffset);

final double animationValue;
final Offset firstStarOffset;
Expand Down Expand Up @@ -229,6 +229,6 @@ class HeaderPainter extends CustomPainter {
}

@override
bool shouldRepaint(HeaderPainter oldDelegate) =>
bool shouldRepaint(BackgroundPainter oldDelegate) =>
oldDelegate.animationValue != animationValue || oldDelegate.firstStarOffset != firstStarOffset;
}
46 changes: 21 additions & 25 deletions lib/background_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ import 'dart:math';

import 'package:flutter/animation.dart';
import 'package:flutter/material.dart';
import 'package:weather/header_painter.dart';
import 'package:weather/background_painter.dart';

class BackgroundWidget extends StatefulWidget {

final double headerHeight;

const BackgroundWidget(this.headerHeight, {Key key}) : super(key: key);

_BackgroundWidgetState createState() => new _BackgroundWidgetState();
}

Expand All @@ -24,28 +29,25 @@ class _BackgroundWidgetState extends State<BackgroundWidget>
});
var beginOffset = new Offset(290.0, 0.0);
var endOffset = _getEndOffset(beginOffset);
animationStarOffset =
new Tween<Offset>(begin: beginOffset, end: endOffset).animate(controller);
animationStarOffset = new Tween<Offset>(begin: beginOffset, end: endOffset)
.animate(controller);
controller.forward();
}

Widget build(BuildContext context) {
return new Scaffold(
backgroundColor: Colors.white,
body: new Stack(
alignment: new Alignment(0.5, 0.94),
children: <Widget>[
new CustomPaint(
painter:
new HeaderPainter(animation.value, animationStarOffset.value),
child: new Container(height: 345.0),
),
new Image.asset(
'assets/reindeer.png',
scale: 15.0,
),
],
),
return Stack(
alignment: new Alignment(0.5, 0.94),
children: <Widget>[
new CustomPaint(
painter:
new BackgroundPainter(animation.value, animationStarOffset.value),
child: new Container(height: widget.headerHeight),
),
new Image.asset(
'assets/reindeer.png',
scale: 15.0,
),
],
);
}

Expand All @@ -54,12 +56,6 @@ class _BackgroundWidgetState extends State<BackgroundWidget>
super.dispose();
}

_getRandomBeginOffset() {
var dy = 0.0;
var dx = new Random().nextInt(345).toDouble();
return new Offset(dx, dy);
}

_getEndOffset(Offset beginOffset) {
var radians = 30.0 * pi / 180;
var dy = 345.0;
Expand Down
58 changes: 58 additions & 0 deletions lib/header_widget.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import 'package:flutter/material.dart';
import 'package:weather/background_widget.dart';

class HeaderWidget extends StatelessWidget {
final double headerHeight;
final bool loading;
final String currentLocation;
final int currentTemperature;

const HeaderWidget(
this.headerHeight, {Key key, this.loading, this.currentLocation, this.currentTemperature})
: super(key: key);

@override
Widget build(BuildContext context) {
return Stack(
children: <Widget>[
BackgroundWidget(headerHeight),
loading
? SizedBox()
: Center(
child: Column(
children: <Widget>[
_buildLocationLabel(),
_buildTemperatureLabel(),
],
),
),
],
);
}

_buildLocationLabel() {
return new Padding(
padding: const EdgeInsets.only(top: 116.0, bottom: 8.0),
child: new Text(
currentLocation,
style: new TextStyle(
color: Colors.white,
fontWeight: FontWeight.normal,
fontSize: 24.0,
decoration: TextDecoration.none,
),
),
);
}

_buildTemperatureLabel() {
return new Text(
'$currentTemperature',
style: new TextStyle(
color: Colors.white,
fontSize: 112.0,
decoration: TextDecoration.none,
),
);
}
}
77 changes: 70 additions & 7 deletions lib/home_page.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,78 @@
import 'package:flutter/material.dart';
import 'package:weather/background_widget.dart';
import 'package:weather/daily_forecast.dart';
import 'package:weather/header_widget.dart';
import 'package:weather/openweather_api.dart';
import 'package:weather/utils.dart';
import 'package:weather/weather_widget.dart';

class HomePage extends StatelessWidget {
class HomePage extends StatefulWidget {
@override
State<StatefulWidget> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {

static const double headerHeight = 345.0;

bool _loading = true;
int _days = 14;
List<DailyForecast> _weatherForecast;
List<String> _weekdaysData;
String _currentLocation;
int _currentTemperature;

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

_fetchData() async {
var openWeatherAPI = new OpenWeatherAPI();
var weekdaysData = getWeekdaysList(_days);
var currentCoord = await getCoordinates();
var lat = currentCoord[0];
var lon = currentCoord[1];
var currentLocation = await getLocation(lat, lon);
var currentTemperature =
await openWeatherAPI.getCurrentTemperature(lat, lon);
var weatherForecast =
await openWeatherAPI.getWeatherForecast(lat, lon, _days);
setState(() {
_weekdaysData = weekdaysData;
_currentLocation = currentLocation;
_currentTemperature = currentTemperature;
_weatherForecast = weatherForecast;
_loading = false;
});
}

@override
Widget build(BuildContext context) {
return new Stack(
children: <Widget>[
new BackgroundWidget(),
new WeatherWidget(),
],
return Scaffold(
backgroundColor: Colors.white,
body: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
backgroundColor: Colors.white,
expandedHeight: headerHeight,
flexibleSpace: FlexibleSpaceBar(
background: HeaderWidget(
headerHeight,
loading: _loading,
currentLocation: _currentLocation,
currentTemperature: _currentTemperature,
),
),
),
WeatherWidget(
loading: _loading,
days: _days,
weekdaysData: _weekdaysData,
weatherForecast: _weatherForecast,
),
],
),
);
}
}
2 changes: 1 addition & 1 deletion lib/openweather_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:weather/daily_forecast.dart';
import 'package:weather/utils.dart';

class OpenWeatherAPI {
final key = 'YOUR_OPENWEATHER_API_KEY';
final key = '8f813533a0fa2d873acc6a73cce9e9fe';

Future<Map<String, dynamic>> _getData(String api, double lat, double lon,
[int cnt]) async {
Expand Down
Loading