Skip to content

Commit 6853a32

Browse files
committed
theme provider created
1 parent 5477bc1 commit 6853a32

File tree

10 files changed

+182
-64
lines changed

10 files changed

+182
-64
lines changed

lib/components/button.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Button extends StatelessWidget {
1515
@override
1616
Widget build(BuildContext context) {
1717
return Material(
18-
color: Colors.white,
18+
color: Colors.transparent,
1919
child: InkWell(
2020
onTap: () => this.onPress(),
2121
borderRadius: BorderRadius.all(

lib/components/buttons/text.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ import 'package:flutter/material.dart';
44
class ButtonText extends StatelessWidget {
55
final String text;
66
final Function onPress;
7+
final Brightness brightness;
78

8-
ButtonText(this.text, {this.onPress});
9+
ButtonText(
10+
this.text, {
11+
this.onPress,
12+
this.brightness,
13+
});
914

1015
@override
1116
Widget build(BuildContext context) {
@@ -14,7 +19,8 @@ class ButtonText extends StatelessWidget {
1419
this.text,
1520
style: TextStyle(
1621
fontSize: 20,
17-
color: Colors.grey[800],
22+
color:
23+
brightness == Brightness.light ? Colors.white : Colors.grey[800],
1824
),
1925
),
2026
size: 50,

lib/components/equal.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ import 'package:flutter/material.dart';
22

33
class EqualButton extends StatelessWidget {
44
final Function onPressed;
5+
final Color color;
56

6-
EqualButton({this.onPressed});
7+
EqualButton({
8+
this.onPressed,
9+
this.color,
10+
});
711

812
@override
913
Widget build(BuildContext context) {
@@ -19,7 +23,7 @@ class EqualButton extends StatelessWidget {
1923
],
2024
),
2125
child: Material(
22-
color: Theme.of(context).primaryColor,
26+
color: color,
2327
borderRadius: BorderRadius.all(
2428
Radius.circular(8),
2529
),

lib/components/header.dart

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import 'package:calc/components/buttons/icon.dart';
2+
import 'package:calc/services/theme.dart';
3+
import 'package:flutter/material.dart';
4+
import 'package:provider/provider.dart';
5+
6+
class Header extends StatefulWidget {
7+
@override
8+
_HeaderState createState() => _HeaderState();
9+
}
10+
11+
class _HeaderState extends State<Header> {
12+
@override
13+
Widget build(BuildContext context) {
14+
return Consumer<ThemeProvider>(
15+
builder: (context, theme, _) => Container(
16+
height: 45,
17+
width: double.infinity,
18+
child: Row(
19+
crossAxisAlignment: CrossAxisAlignment.center,
20+
mainAxisAlignment: MainAxisAlignment.end,
21+
children: [
22+
ButtonIcon(
23+
icon: theme.data().primaryColor == Colors.blueGrey[800]
24+
? Icons.wb_sunny
25+
: Icons.nightlight_round,
26+
size: 46,
27+
onPress: () => this.changeMode(theme),
28+
brightness: theme.data().brightness,
29+
),
30+
],
31+
),
32+
),
33+
);
34+
}
35+
36+
void changeMode(ThemeProvider themeProvider) {
37+
// if was dark mode
38+
if (themeProvider.data().primaryColor == Colors.blueGrey[800]) {
39+
themeProvider.setThemeByName('blue');
40+
} else
41+
themeProvider.setThemeByName('dark');
42+
}
43+
}

lib/components/pad.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@ import 'package:calc/interface/history.dart';
44
class Pad extends StatelessWidget {
55
final String entry;
66
final List<History> histories;
7+
final Brightness brightness;
78

89
Pad({
910
this.entry,
1011
this.histories,
12+
this.brightness,
1113
});
1214

1315
@override
1416
Widget build(BuildContext context) {
1517
return Container(
16-
height: (MediaQuery.of(context).size.height / 2.5) + 35,
18+
height: (MediaQuery.of(context).size.height / 2.5) - 1,
1719
child: ListView(
1820
reverse: true,
1921
children: [
@@ -28,6 +30,7 @@ class Pad extends StatelessWidget {
2830
fontSize: 28,
2931
width: MediaQuery.of(context).size.width - (75 + 20),
3032
),
33+
color: brightness == Brightness.light ? Colors.white : Colors.black,
3134
),
3235
Column(
3336
children: histories
@@ -39,7 +42,9 @@ class Pad extends StatelessWidget {
3942
fontSize: 24,
4043
width: MediaQuery.of(context).size.width - (75 + 85),
4144
),
42-
color: Colors.grey[700],
45+
color: brightness == Brightness.light
46+
? Colors.white54
47+
: Colors.grey[700],
4348
),
4449
)
4550
.toList(),
@@ -52,7 +57,7 @@ class Pad extends StatelessWidget {
5257
Widget row(
5358
History history, {
5459
double fontSize = 28,
55-
Color color = Colors.black,
60+
Color color,
5661
}) {
5762
return Container(
5863
margin: EdgeInsets.fromLTRB(0, 10, 0, 0),

lib/components/view.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ import 'package:flutter/material.dart';
33
class View extends StatelessWidget {
44
final Widget child;
55
final double margin;
6+
final Color color;
67

78
View({
89
this.child,
910
this.margin = 25,
11+
this.color,
1012
});
1113

1214
@override
@@ -16,7 +18,7 @@ class View extends StatelessWidget {
1618
height: MediaQuery.of(context).size.height - (this.margin * 2),
1719
margin: EdgeInsets.symmetric(vertical: this.margin),
1820
decoration: BoxDecoration(
19-
color: Colors.white,
21+
color: color,
2022
borderRadius: BorderRadius.only(
2123
topLeft: Radius.circular(10),
2224
bottomLeft: Radius.circular(10),

lib/database/themes.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import 'package:flutter/material.dart';
2+
3+
Map<String, ThemeData> themes = {
4+
'blue': ThemeData(
5+
primaryColor: Colors.blue,
6+
backgroundColor: Colors.white,
7+
brightness: Brightness.dark,
8+
),
9+
'dark': ThemeData(
10+
primaryColor: Colors.blueGrey[800],
11+
backgroundColor: Colors.blueGrey[700],
12+
brightness: Brightness.light,
13+
),
14+
};

lib/main.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1+
import 'package:calc/services/theme.dart';
12
import 'package:calc/views/main.dart';
23
import 'package:flutter/material.dart';
4+
import 'package:provider/provider.dart';
35

46
void main() {
5-
runApp(App());
7+
runApp(
8+
ChangeNotifierProvider(
9+
create: (_) => ThemeProvider(),
10+
child: App(),
11+
),
12+
);
613
}
714

815
// ignore: non_constant_identifier_names

lib/services/theme.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import 'package:calc/database/themes.dart';
2+
import 'package:flutter/material.dart';
3+
import 'package:flutter/services.dart';
4+
5+
class ThemeProvider with ChangeNotifier {
6+
ThemeData _theme;
7+
ThemeData data() => _theme;
8+
9+
ThemeProvider() {
10+
_theme = themes['blue'];
11+
_setSystemColor();
12+
notifyListeners();
13+
}
14+
15+
void setThemeByName(String name) {
16+
ThemeData themeData = themes[name];
17+
if (themeData != null) {
18+
_theme = themeData;
19+
_setSystemColor();
20+
notifyListeners();
21+
}
22+
}
23+
24+
void _setSystemColor() {
25+
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
26+
systemNavigationBarColor: _theme.primaryColor,
27+
systemNavigationBarIconBrightness: Brightness.light,
28+
statusBarColor: _theme.primaryColor,
29+
statusBarBrightness: Brightness.light,
30+
));
31+
}
32+
}

lib/views/main.dart

Lines changed: 59 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ import 'package:calc/components/buttons.dart';
22
import 'package:calc/components/buttons/backspace.dart';
33
import 'package:calc/components/buttons/text.dart';
44
import 'package:calc/components/equal.dart';
5+
import 'package:calc/components/header.dart';
56
import 'package:calc/components/numbers.dart';
67
import 'package:calc/components/pad.dart';
78
import 'package:calc/components/view.dart';
89
import 'package:calc/database/buttons.dart';
910
import 'package:calc/interface/history.dart';
11+
import 'package:calc/services/theme.dart';
1012
import 'package:calc/services/vibration.dart' as vibration;
1113
import 'package:flutter/material.dart';
12-
import 'package:flutter/services.dart';
14+
import 'package:provider/provider.dart';
1315

1416
class MainView extends StatefulWidget {
1517
@override
@@ -22,63 +24,66 @@ class _MainViewState extends State<MainView> {
2224

2325
@override
2426
Widget build(BuildContext context) {
25-
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
26-
systemNavigationBarColor: Theme.of(context).primaryColor,
27-
systemNavigationBarIconBrightness: Brightness.light,
28-
statusBarColor: Theme.of(context).primaryColor,
29-
statusBarBrightness: Brightness.light,
30-
));
31-
return Scaffold(
32-
backgroundColor: Theme.of(context).primaryColor,
33-
body: SafeArea(
34-
child: Row(
35-
children: [
36-
NumbersColumn(
37-
onPressed: (String value) => this.pushToEntry(value),
38-
),
39-
View(
40-
child: Column(
41-
children: [
42-
Pad(
43-
entry: entry,
44-
histories: histories,
45-
),
46-
Buttons(
47-
count: 4,
48-
children: buttons
49-
.map(
50-
(button) => ButtonText(
51-
button,
52-
onPress: () => this.pushToHistory(button),
27+
return Consumer<ThemeProvider>(
28+
builder: (context, theme, _) => Scaffold(
29+
backgroundColor: theme.data().primaryColor,
30+
body: SafeArea(
31+
child: Row(
32+
children: [
33+
NumbersColumn(
34+
onPressed: (String value) => this.pushToEntry(value),
35+
),
36+
View(
37+
color: theme.data().backgroundColor,
38+
child: Column(
39+
children: [
40+
Header(),
41+
Pad(
42+
entry: entry,
43+
histories: histories,
44+
brightness: theme.data().brightness,
45+
),
46+
Buttons(
47+
count: 4,
48+
children: buttons
49+
.map(
50+
(button) => ButtonText(
51+
button,
52+
onPress: () => this.pushToHistory(button),
53+
brightness: theme.data().brightness,
54+
),
55+
)
56+
.toList(),
57+
),
58+
Spacer(),
59+
Container(
60+
margin: EdgeInsets.symmetric(
61+
horizontal: 20,
62+
),
63+
child: Row(
64+
children: [
65+
ButtonText(
66+
'C',
67+
onPress: () => this.clear(),
68+
brightness: theme.data().brightness,
69+
),
70+
Spacer(),
71+
ButtonBackspace(
72+
brightness: theme.data().brightness,
73+
onPress: () => this.backspace(),
5374
),
54-
)
55-
.toList(),
56-
),
57-
Spacer(),
58-
Container(
59-
margin: EdgeInsets.symmetric(
60-
horizontal: 20,
75+
],
76+
),
6177
),
62-
child: Row(
63-
children: [
64-
ButtonText(
65-
'C',
66-
onPress: () => this.clear(),
67-
),
68-
Spacer(),
69-
ButtonBackspace(
70-
onPress: () => this.backspace(),
71-
),
72-
],
78+
EqualButton(
79+
color: theme.data().primaryColor,
80+
onPressed: () => this.calculate(),
7381
),
74-
),
75-
EqualButton(
76-
onPressed: () => this.calculate(),
77-
),
78-
],
82+
],
83+
),
7984
),
80-
),
81-
],
85+
],
86+
),
8287
),
8388
),
8489
);

0 commit comments

Comments
 (0)