Skip to content

Commit

Permalink
make the future and today to the same city.
Browse files Browse the repository at this point in the history
  • Loading branch information
nb312 committed Dec 13, 2018
1 parent 50b0b3a commit 0f66cbc
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 18 deletions.
1 change: 1 addition & 0 deletions lib/const/string_const.dart
Expand Up @@ -9,3 +9,4 @@ const ASSET_IMAGE_PATH = "assets/images/";
const CONTENT_IS_EMPTY = "This content is empty!";

const BOTTOM_STR_ITEMS = ["Today", "Future"];

4 changes: 4 additions & 0 deletions lib/main_page.dart
Expand Up @@ -43,6 +43,10 @@ class _MainState extends State<MainPage> {
return list;
}

void initState() {
super.initState();
}

Widget _body() {
switch (_currentPage) {
case 0:
Expand Down
40 changes: 30 additions & 10 deletions lib/page/future_page.dart
Expand Up @@ -14,6 +14,8 @@ import 'package:flutter_weather/const/size_const.dart';
import 'package:flutter_weather/view/MBarChart.dart';
import 'package:flutter_weather/view/MPieChart.dart';
import 'package:flutter_weather/const/color_const.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_weather/util/share_prefer.dart';

class FuturePage extends StatefulWidget {
@override
Expand All @@ -24,11 +26,27 @@ class _FutureState extends State<FuturePage> {
CityItem cityItem;
List<ForecastWeatherItem> forecastItems;

Future<Null> updateCity() async {
var sp = await SharedPreferences.getInstance();
var list = sp.getStringList(LOCAL_CITY);
if (list != null && list.isNotEmpty) {
cityItem =
CityItem(id: int.parse(list[0]), name: list[1], country: list[2]);
} else {
await sp.setStringList(
LOCAL_CITY,
["1796236", "Shanghai", "CN"],
);
cityItem = CityItem(id: 1796236, name: "Shanghai", country: "CN");
}
}

@override
void initState() {
super.initState();
cityItem = CityItem(id: 1796236, name: "Shanghai", country: "CN");
_updateData(cityItem);
updateCity().then((_) {
_updateData(cityItem);
});
EventUtil.busEvent.on<CityEvent>().listen((event) {
print("hello aaaaa");
setState(() {
Expand Down Expand Up @@ -126,7 +144,7 @@ class _FutureState extends State<FuturePage> {

Widget _tabs() {
return Container(
color:MAIN_COLOR,
color: MAIN_COLOR,
constraints: BoxConstraints.expand(height: 40.0),
child: TabBar(
tabs: [
Expand Down Expand Up @@ -186,13 +204,15 @@ class _FutureState extends State<FuturePage> {
child: Column(
children: <Widget>[
_tabs(),
Expanded(
child: TabBarView(children: [
_bodyChart(ChartType.Line),
_bodyChart(ChartType.Bar),
_bodyChart(ChartType.Circle),
]),
),
cityItem == null
? Container()
: Expanded(
child: TabBarView(children: [
_bodyChart(ChartType.Line),
_bodyChart(ChartType.Bar),
_bodyChart(ChartType.Circle),
]),
),
],
)),
);
Expand Down
18 changes: 14 additions & 4 deletions lib/page/main_drawer.dart
Expand Up @@ -14,6 +14,8 @@ import 'package:flutter_weather/http/HttpUtil.dart';
import 'package:flutter_weather/data/CurrentWeatherItem.dart';
import 'package:flutter_weather/const/color_const.dart';
import 'package:flutter_weather/data/event/base_event.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_weather/util/share_prefer.dart';

class MainDrawer extends StatefulWidget {
@override
Expand Down Expand Up @@ -73,6 +75,12 @@ class _DrawerState extends State<MainDrawer> {
);
}

Future<Null> _updateCity(CityItem item) async {
var sp = await SharedPreferences.getInstance();
await sp.setStringList(
LOCAL_CITY, [item.id.toString(), item.name, item.country]);
}

Widget _cityWidget(CityItem item) {
return Container(
decoration: BoxDecoration(
Expand All @@ -84,10 +92,12 @@ class _DrawerState extends State<MainDrawer> {
margin: EdgeInsets.only(bottom: 1.0),
child: ListTile(
onTap: () {
var event = CityEvent(cityItem: item);
EventUtil.busEvent.fire(event);
print("city.name: ${item.name},id: ${item.id}");
Navigator.pop(context);
_updateCity(item).then((_) {
var event = CityEvent(cityItem: item);
EventUtil.busEvent.fire(event);
print("city.name: ${item.name},id: ${item.id}");
Navigator.pop(context);
});
},
leading: CircleAvatar(
child: _textWhite(item.name[0].toUpperCase(),
Expand Down
26 changes: 23 additions & 3 deletions lib/page/today_page.dart
Expand Up @@ -17,6 +17,8 @@ import 'package:bubble_tab_indicator/bubble_tab_indicator.dart';
import 'package:flutter_weather/const/color_const.dart';
import 'package:flutter_weather/view/MBarChart.dart';
import 'package:flutter_weather/view/MPieChart.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_weather/util/share_prefer.dart';

class TodayPage extends StatefulWidget {
@override
Expand All @@ -36,10 +38,26 @@ class _TodayState extends State<TodayPage> {
CityItem cityItem;
CurrentWeatherItem weatherItem;

Future<Null> updateCity() async {
var sp = await SharedPreferences.getInstance();
var list = sp.getStringList(LOCAL_CITY);
if (list != null && list.isNotEmpty) {
cityItem =
CityItem(id: int.parse(list[0]), name: list[1], country: list[2]);
} else {
await sp.setStringList(
LOCAL_CITY,
["1796236", "Shanghai", "CN"],
);
cityItem = CityItem(id: 1796236, name: "Shanghai", country: "CN");
}
}

void initState() {
super.initState();
cityItem = CityItem(id: 1796236, name: "Shanghai", country: "CN");
_updateWeather(cityItem);
updateCity().then((_) {
_updateWeather(cityItem);
});
EventUtil.busEvent.on<CityEvent>().listen((event) {
print("hello aaaaa");
setState(() {
Expand Down Expand Up @@ -94,7 +112,9 @@ class _TodayState extends State<TodayPage> {
),
child: Center(
child: Text(
"${cityItem.name} ${weatherItem == null ? "" : " ${weatherItem.temp}F"}",
cityItem == null
? ""
: "${cityItem.name} ${weatherItem == null ? "" : " ${weatherItem.temp}F"}",
style: TextStyle(
color: Colors.blueAccent,
fontWeight: FontWeight.w700,
Expand Down
2 changes: 1 addition & 1 deletion lib/util/ResourceUtil.dart
Expand Up @@ -10,6 +10,7 @@ import 'package:flutter_weather/const/string_const.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:shared_preferences/shared_preferences.dart';

const _CITIES_JSON = "assets/json/citys.json";
final databaseReference = FirebaseDatabase.instance.reference();
Expand All @@ -30,7 +31,6 @@ class ResourceUtil {
}
return list;
}

static Image imageCover(name, {double size}) {
return Image.asset(
"$ASSET_IMAGE_PATH$name",
Expand Down
8 changes: 8 additions & 0 deletions lib/util/share_prefer.dart
@@ -0,0 +1,8 @@
///
/// Created by NieBin on 18-12-13
/// Github: https://github.com/nb312
/// Email: niebin312@gmail.com
///
///
const LOCAL_CITY = "local_city";
2 changes: 2 additions & 0 deletions pubspec.yaml
Expand Up @@ -29,6 +29,8 @@ dependencies:
firebase_database: 1.0.3
google_sign_in: ^3.2.4
firebase_auth: ^0.6.6
shared_preferences: ^0.4.2


dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 0f66cbc

Please sign in to comment.