Skip to content

Commit

Permalink
Merge pull request #20 from RoyARG02/v1.0+
Browse files Browse the repository at this point in the history
Add version info, change theme model, change back behaviour in analysis
  • Loading branch information
royarg02 authored Jan 20, 2020
2 parents fd9875e + 1dad1ff commit fef3e73
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 85 deletions.
138 changes: 90 additions & 48 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,27 @@ import 'package:provider/provider.dart';

// * Prefs import
import 'package:soil_moisture_app/prefs/user_prefs.dart';
import 'package:soil_moisture_app/states/selected_card_state.dart';

// * State import
import 'package:soil_moisture_app/states/selected_card_state.dart';
import 'package:soil_moisture_app/states/theme_state.dart';

// * ui import
import 'package:soil_moisture_app/ui/build_theme.dart';
import 'package:soil_moisture_app/ui/options.dart';

// * utils import
import 'package:soil_moisture_app/utils/app_info.dart';
import 'package:soil_moisture_app/utils/sizes.dart';

// * Pages Import
import 'pages/Analysis.dart';
import 'pages/Overview.dart';

void main() async {
WidgetsFlutterBinding
.ensureInitialized(); //for awaiting to load sharedPreferences
WidgetsFlutterBinding.ensureInitialized(); //for awaiting
await loadPrefs();
await loadAppInfo();
runApp(
MultiProvider(
providers: [
Expand All @@ -43,70 +45,110 @@ class Root extends StatelessWidget {
final String title = 'Soif';
@override
Widget build(BuildContext context) {
var _themeState = Provider.of<ThemeState>(context);
return MaterialApp(
title: title,
debugShowCheckedModeBanner: false,
home: DefaultTabController(length: 2, child: Home()),
theme: Provider.of<ThemeState>(context).getTheme,
home: Home(),
theme: buildLightTheme(),
darkTheme: buildDarkTheme(),
themeMode: determineThemeMode(_themeState.isDarkTheme),
);
}
}

class Home extends StatelessWidget {
final List<Widget> _children = [
class Home extends StatefulWidget {
final List<Widget> _tabPages = [
Overview(),
Analysis(),
];
@override
@override
_HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> with SingleTickerProviderStateMixin {
TabController _controller;
void initState() {
super.initState();
_controller = TabController(
vsync: this,
initialIndex: 0,
length: widget._tabPages.length,
);
}

@override
void dispose() {
_controller.dispose();
super.dispose();
}

Future<bool> _popScopeInvoke() async {
if (this._controller.index == 1) {
this._controller.index = 0;
return Future<bool>.value(false);
} else {
await getPrefs.setBool(
'isDarkTheme', Provider.of<ThemeState>(context).isDarkTheme);
return Future<bool>.value(true);
}
}

@override
Widget build(BuildContext context) {
print(appWidth(context));
return Scaffold(
appBar: AppBar(
title: Image.asset(
(Provider.of<ThemeState>(context).isDarkTheme)
? './assets/images/Soif_sk_dark.png'
: './assets/images/Soif_sk.png',
height: appWidth(context) * 0.08,
return WillPopScope(
onWillPop: _popScopeInvoke,
child: Scaffold(
appBar: AppBar(
title: Image.asset(
(Provider.of<ThemeState>(context).isDarkTheme)
? './assets/images/Soif_sk_dark.png'
: './assets/images/Soif_sk.png',
height: appWidth(context) * 0.08,
),
centerTitle: true,
),
centerTitle: true,
),
body: TabBarView(
children: _children,
physics: NeverScrollableScrollPhysics(),
),
bottomNavigationBar: Container(
color: Theme.of(context).accentColor,
child: Row(
children: <Widget>[
Flexible(
child: TabBar(
tabs: <Widget>[
Tab(
icon: Icon(
Icons.remove_red_eye,
body: TabBarView(
controller: this._controller,
children: widget._tabPages,
physics: NeverScrollableScrollPhysics(),
),
bottomNavigationBar: Container(
color: Theme.of(context).accentColor,
child: Row(
children: <Widget>[
Flexible(
child: TabBar(
controller: this._controller,
tabs: <Widget>[
Tab(
icon: Icon(
Icons.remove_red_eye,
),
text: 'Overview',
),
text: 'Overview',
Tab(
icon: Icon(
Icons.timeline,
),
text: 'Analysis',
)
],
unselectedLabelStyle: TextStyle(
fontSize: appWidth(context) * 0.025,
fontFamily: 'Ocrb',
),
labelStyle: TextStyle(
fontSize: appWidth(context) * 0.035,
fontFamily: 'Ocrb',
),
Tab(
icon: Icon(
Icons.timeline,
),
text: 'Analysis',
)
],
unselectedLabelStyle: TextStyle(
fontSize: appWidth(context) * 0.025,
fontFamily: 'Ocrb',
),
labelStyle: TextStyle(
fontSize: appWidth(context) * 0.035,
fontFamily: 'Ocrb',
),
),
),
Options()
],
Options()
],
),
),
),
);
Expand Down
28 changes: 21 additions & 7 deletions lib/pages/Credits.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import 'package:url_launcher/url_launcher.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';

// * utils import
import 'package:soil_moisture_app/utils/app_info.dart';
import 'package:soil_moisture_app/utils/sizes.dart';

// * data import
Expand Down Expand Up @@ -119,20 +120,36 @@ class _CreditsState extends State<Credits> {
centerTitle: true,
),
body: SafeArea(
minimum: EdgeInsets.symmetric(horizontal: appWidth(context) * 0.03),
child: ListView(
padding: EdgeInsets.all(appWidth(context) * 0.03),
physics:
AlwaysScrollableScrollPhysics(parent: BouncingScrollPhysics()),
children: <Widget>[
SizedBox(
height: appWidth(context) * 0.03,
),
Image.asset(
(Provider.of<ThemeState>(context).isDarkTheme)
? './assets/images/Soif_sk_dark.png'
: './assets/images/Soif_sk.png',
height: appWidth(context) * 0.25,
),
Padding(
padding: EdgeInsets.all(appHeight(context) * 0.01),
child: RichText(
textAlign: TextAlign.center,
strutStyle: StrutStyle(fontSize: 14.0),
text: TextSpan(
style: Theme.of(context).textTheme.body2.copyWith(
color: (Provider.of<ThemeState>(context).isDarkTheme)
? subtleWhiteTextColor
: subtleBlackTextColor,
),
children: [
TextSpan(text: 'Version ${getAppInfo.version}\n'),
TextSpan(text: 'Built with '),
WidgetSpan(child: FlutterLogo()),
],
),
),
),
Padding(
padding: EdgeInsets.symmetric(vertical: appWidth(context) * 0.03),
child: Text(
Expand Down Expand Up @@ -189,9 +206,6 @@ class _CreditsState extends State<Credits> {
onTap: () => _launchUrl(apiUrl),
),
),
SizedBox(
height: appWidth(context) * 0.03,
)
],
),
),
Expand Down
28 changes: 4 additions & 24 deletions lib/states/theme_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,18 @@ import 'package:flutter/services.dart';

// * Prefs Import
import 'package:soil_moisture_app/prefs/user_prefs.dart';

// * ui import
import 'package:soil_moisture_app/ui/build_theme.dart';

class ThemeState with ChangeNotifier {
ThemeData _appTheme;
SystemUiOverlayStyle _systemUiTheme;
bool isDarkTheme = getPrefs.getBool('isDarkTheme') ?? false;
bool isDarkTheme;
ThemeState() {
_appTheme = (isDarkTheme) ? buildDarkTheme() : buildLightTheme();
_systemUiTheme = (isDarkTheme) ? buildDarkSystemUi() : buildLightSystemUi();
SystemChrome.setSystemUIOverlayStyle(_systemUiTheme);
isDarkTheme = getPrefs.getBool('isDarkTheme') ?? false;
SystemChrome.setSystemUIOverlayStyle(appSystemUiTheme(isDarkTheme));
}

ThemeData get getTheme => _appTheme;
SystemUiOverlayStyle get getSystemUiTheme => _systemUiTheme;

void toggleTheme() {
if (isDarkTheme) {
_appTheme = buildLightTheme();
_systemUiTheme = buildLightSystemUi();
} else {
_appTheme = buildDarkTheme();
_systemUiTheme = buildDarkSystemUi();
}
isDarkTheme = !isDarkTheme;
SystemChrome.setSystemUIOverlayStyle(_systemUiTheme);
SystemChrome.setSystemUIOverlayStyle(appSystemUiTheme(isDarkTheme));
notifyListeners();
updateThemePrefs();
}

void updateThemePrefs() async {
await getPrefs.setBool('isDarkTheme', isDarkTheme);
}
}
9 changes: 9 additions & 0 deletions lib/ui/build_theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ import 'package:flutter/services.dart';
// * ui import
import 'package:soil_moisture_app/ui/colors.dart';

// * Helper functions for returning appropriate themes
ThemeMode determineThemeMode(bool isDarkTheme) {
return isDarkTheme ? ThemeMode.dark : ThemeMode.light;
}

SystemUiOverlayStyle appSystemUiTheme(bool isDarkTheme) {
return isDarkTheme ? buildDarkSystemUi() : buildLightSystemUi();
}

// * Text theme to be used throughout the app
TextTheme appTextTheme = TextTheme(
display4: TextStyle(fontFamily: 'Ocrb'),
Expand Down
11 changes: 6 additions & 5 deletions lib/ui/options.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* options
* Displays a bottom options menu for 'Pump Threshold Control' and 'About', for now.
* Displays a bottom options menu for 'Dark Theme', 'Pump Threshold Control' and 'About'.
*/

import 'package:flutter/material.dart';
Expand All @@ -22,22 +22,23 @@ import 'package:soil_moisture_app/states/theme_state.dart';

class Options extends StatelessWidget {
void showOptions(BuildContext context) {
var _themeState = Provider.of<ThemeState>(context);
showModalBottomSheet(
isScrollControlled: true,
context: context,
builder: (context) => Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ListTile(
leading: (Provider.of<ThemeState>(context).isDarkTheme)
leading: (_themeState.isDarkTheme)
? Icon(FontAwesomeIcons.solidLightbulb)
: Icon(FontAwesomeIcons.lightbulb),
title: Text('Dark Theme'),
trailing: Switch(
value: Provider.of<ThemeState>(context).isDarkTheme,
onChanged: (_) => Provider.of<ThemeState>(context).toggleTheme(),
value: _themeState.isDarkTheme,
onChanged: (_) => _themeState.toggleTheme(),
),
onTap: () => Provider.of<ThemeState>(context).toggleTheme(),
onTap: () => _themeState.toggleTheme(),
),
ListTile(
leading: Icon(FontAwesomeIcons.slidersH),
Expand Down
15 changes: 15 additions & 0 deletions lib/utils/app_info.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* app_info
*
* Fetches the app properties from the system.
*/

import 'package:package_info/package_info.dart';

PackageInfo _appInfo;

loadAppInfo() async {
_appInfo = await PackageInfo.fromPlatform();
}

PackageInfo get getAppInfo => _appInfo;
7 changes: 7 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.8"
package_info:
dependency: "direct main"
description:
name: package_info
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.0+13"
path:
dependency: transitive
description:
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: soil_moisture_app
description: A new Flutter project.

version: 1.0.2+10002
version: 1.0.3+10003

environment:
sdk: ">=2.2.2 <3.0.0"
Expand All @@ -16,6 +16,7 @@ dependencies:
flutter_image: ^3.0.0
provider: ^3.1.0+1
shared_preferences: ^0.5.6
package_info: ^0.4.0+13

cupertino_icons: ^0.1.3
http:
Expand Down

0 comments on commit fef3e73

Please sign in to comment.