git-touch-android-ios-app/lib/models/theme.dart

378 lines
10 KiB
Dart
Raw Normal View History

import 'dart:io';
2019-09-23 11:08:51 +02:00
import 'dart:async';
import 'package:fimber/fimber.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
2019-11-03 16:33:24 +01:00
import 'package:git_touch/widgets/action_button.dart';
2019-11-05 14:22:41 +01:00
import 'package:primer/primer.dart';
import 'package:shared_preferences/shared_preferences.dart';
class DialogOption<T> {
final T value;
final Widget widget;
DialogOption({this.value, this.widget});
}
2019-09-19 15:10:50 +02:00
class AppThemeType {
static const material = 0;
static const cupertino = 1;
2019-09-19 15:10:50 +02:00
static const values = [AppThemeType.material, AppThemeType.cupertino];
}
2019-09-29 09:35:33 +02:00
class PickerItem<T> {
final T value;
final String text;
PickerItem(this.value, {@required this.text});
}
class PickerGroupItem<T> {
final T value;
final List<PickerItem<T>> items;
final Function(T value) onChange;
final Function(T value) onClose;
PickerGroupItem({
@required this.value,
@required this.items,
this.onChange,
this.onClose,
});
}
2019-10-02 08:58:11 +02:00
class SelectorItem<T> {
T value;
String text;
SelectorItem({@required this.value, @required this.text});
}
2019-09-29 10:01:03 +02:00
// No animation. For replacing route
// TODO: Go back
class StaticRoute extends PageRouteBuilder {
final WidgetBuilder builder;
StaticRoute({this.builder})
: super(
pageBuilder: (BuildContext context, Animation<double> animation,
Animation<double> secondaryAnimation) {
return builder(context);
},
transitionsBuilder: (BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
Widget child) {
return child;
},
);
}
2019-11-05 14:22:41 +01:00
class Palette {
Color primary;
Color text;
Color secondaryText;
Color tertiaryText;
Color background;
Color border;
Palette({
this.primary,
this.text,
this.secondaryText,
this.tertiaryText,
this.background,
this.border,
});
}
class ThemeModel with ChangeNotifier {
static const storageKey = 'theme';
int _theme;
int get theme => _theme;
bool get ready => _theme != null;
2019-11-06 14:27:37 +01:00
Brightness _brightness = Brightness.light;
2019-11-05 14:22:41 +01:00
Brightness get brightness => _brightness;
2019-11-08 10:24:00 +01:00
Future<void> toggleBrightness() async {
2019-11-05 14:22:41 +01:00
// TODO: Save
2019-11-08 10:24:00 +01:00
_brightness =
_brightness == Brightness.dark ? Brightness.light : Brightness.dark;
2019-11-05 14:22:41 +01:00
notifyListeners();
}
Palette get palette {
switch (brightness) {
case Brightness.light:
return Palette(
primary: PrimerColors.blue500,
text: PrimerColors.gray900,
secondaryText: PrimerColors.gray700,
tertiaryText: PrimerColors.gray500,
background: PrimerColors.white,
border: PrimerColors.gray100,
);
case Brightness.dark:
return Palette(
primary: PrimerColors.blue500,
text: PrimerColors.gray400,
secondaryText: PrimerColors.gray500,
tertiaryText: PrimerColors.gray600,
background: PrimerColors.black,
border: PrimerColors.gray900,
);
default:
return null;
}
}
2019-11-05 08:09:54 +01:00
Future<void> init() async {
var prefs = await SharedPreferences.getInstance();
int v = prefs.getInt(storageKey);
Fimber.d('read theme: $v');
2019-09-19 15:10:50 +02:00
if (AppThemeType.values.contains(v)) {
_theme = v;
} else if (Platform.isIOS) {
2019-09-19 15:10:50 +02:00
_theme = AppThemeType.cupertino;
} else {
2019-09-19 15:10:50 +02:00
_theme = AppThemeType.material;
}
notifyListeners();
}
Future<void> setTheme(int v) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
_theme = v;
await prefs.setInt(storageKey, v);
Fimber.d('write theme: $v');
notifyListeners();
}
2019-09-23 12:13:21 +02:00
pushRoute(
BuildContext context,
WidgetBuilder builder, {
bool fullscreenDialog = false,
}) {
switch (theme) {
2019-09-19 15:10:50 +02:00
case AppThemeType.cupertino:
2019-09-23 12:13:21 +02:00
return Navigator.of(context).push(CupertinoPageRoute(
builder: builder,
fullscreenDialog: fullscreenDialog,
));
default:
2019-09-23 12:13:21 +02:00
return Navigator.of(context).push(MaterialPageRoute(
builder: builder,
fullscreenDialog: fullscreenDialog,
));
}
}
2019-09-23 12:13:21 +02:00
pushReplacementRoute(BuildContext context, WidgetBuilder builder) {
2019-09-29 10:01:03 +02:00
return Navigator.of(context).pushReplacement(StaticRoute(builder: builder));
2019-09-23 12:13:21 +02:00
}
2019-10-03 06:55:17 +02:00
Future<bool> showConfirm(BuildContext context, Widget content) {
switch (theme) {
2019-09-19 15:10:50 +02:00
case AppThemeType.cupertino:
return showCupertinoDialog(
context: context,
builder: (context) {
return CupertinoAlertDialog(
2019-10-03 06:55:17 +02:00
title: content,
actions: <Widget>[
CupertinoDialogAction(
child: const Text('cancel'),
isDefaultAction: true,
onPressed: () {
Navigator.pop(context, false);
},
),
CupertinoDialogAction(
child: const Text('OK'),
onPressed: () {
Navigator.pop(context, true);
},
),
],
);
},
);
default:
return showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
2019-10-03 06:55:17 +02:00
content: content,
actions: <Widget>[
FlatButton(
child: const Text('CANCEL'),
onPressed: () {
Navigator.pop(context, false);
},
),
FlatButton(
child: const Text('OK'),
onPressed: () {
Navigator.pop(context, true);
},
)
],
);
},
);
}
}
Future<T> showDialogOptions<T>(
BuildContext context, List<DialogOption<T>> options) {
var title = Text('Pick your reaction');
var cancelWidget = Text('Cancel');
switch (theme) {
2019-09-19 15:10:50 +02:00
case AppThemeType.cupertino:
return showCupertinoDialog<T>(
context: context,
builder: (BuildContext context) {
return CupertinoAlertDialog(
title: title,
actions: options.map((option) {
return CupertinoDialogAction(
child: option.widget,
onPressed: () {
Navigator.pop(context, option.value);
},
);
}).toList()
..add(
CupertinoDialogAction(
child: cancelWidget,
isDestructiveAction: true,
onPressed: () {
Navigator.pop(context, null);
},
),
),
);
},
);
default:
return showDialog(
context: context,
builder: (BuildContext context) {
return SimpleDialog(
title: title,
children: options.map<Widget>((option) {
return SimpleDialogOption(
child: option.widget,
onPressed: () {
Navigator.pop(context, option.value);
},
);
}).toList()
..add(SimpleDialogOption(
child: cancelWidget,
onPressed: () {
Navigator.pop(context, null);
},
)),
);
},
);
}
}
2019-09-23 11:08:51 +02:00
2019-10-02 08:58:11 +02:00
showSelector<T>({
@required BuildContext context,
@required Iterable<SelectorItem<T>> items,
@required T selected,
}) async {
switch (theme) {
case AppThemeType.cupertino:
default:
return showMenu<T>(
context: context,
initialValue: selected,
items: items
.map((item) =>
PopupMenuItem(value: item.value, child: Text(item.text)))
.toList(),
position: RelativeRect.fromLTRB(1, 10, 0, 0),
);
}
}
2019-09-23 11:08:51 +02:00
2019-10-02 08:58:11 +02:00
static Timer _debounce;
2019-09-29 09:35:33 +02:00
String _selectedItem;
2019-09-23 11:08:51 +02:00
showPicker(BuildContext context, PickerGroupItem<String> groupItem) async {
switch (theme) {
case AppThemeType.cupertino:
2019-09-28 18:25:14 +02:00
default:
2019-09-29 09:02:06 +02:00
await showCupertinoModalPopup(
2019-09-23 11:08:51 +02:00
context: context,
builder: (context) {
return Container(
2019-09-28 18:25:14 +02:00
height: 216,
2019-09-23 11:08:51 +02:00
child: CupertinoPicker(
backgroundColor: CupertinoColors.white,
children: groupItem.items.map((v) => Text(v.text)).toList(),
itemExtent: 40,
scrollController: FixedExtentScrollController(
initialItem: groupItem.items
.toList()
.indexWhere((v) => v.value == groupItem.value)),
onSelectedItemChanged: (index) {
2019-09-29 09:35:33 +02:00
_selectedItem = groupItem.items[index].value;
2019-09-29 09:02:06 +02:00
2019-09-29 09:35:33 +02:00
if (groupItem.onChange != null) {
if (_debounce?.isActive ?? false) {
_debounce.cancel();
}
_debounce = Timer(const Duration(milliseconds: 500), () {
groupItem.onChange(_selectedItem);
});
}
2019-09-23 11:08:51 +02:00
},
),
);
},
);
2019-09-29 09:35:33 +02:00
if (groupItem.onClose != null) {
groupItem.onClose(_selectedItem);
}
2019-09-23 11:08:51 +02:00
}
}
2019-11-03 16:33:24 +01:00
showActions(BuildContext context, List<ActionItem> actionItems) async {
final value = await showCupertinoModalPopup<int>(
context: context,
builder: (BuildContext context) {
return CupertinoActionSheet(
title: Text('Actions'),
actions: actionItems.asMap().entries.map((entry) {
return CupertinoActionSheetAction(
child: Text(entry.value.text),
onPressed: () {
Navigator.pop(context, entry.key);
},
);
}).toList(),
cancelButton: CupertinoActionSheetAction(
child: const Text('Cancel'),
isDefaultAction: true,
onPressed: () {
Navigator.pop(context);
},
),
);
},
);
if (value != null) {
actionItems[value].onPress(context);
}
}
}