2019-09-19 13:52:18 +02:00
|
|
|
import 'dart:async';
|
2019-09-02 15:52:32 +02:00
|
|
|
import 'dart:io';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:flutter/material.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 13:52:18 +02:00
|
|
|
class PickerItem<T> {
|
|
|
|
final T value;
|
|
|
|
final String text;
|
|
|
|
PickerItem(this.value, {@required this.text});
|
|
|
|
}
|
|
|
|
|
2019-09-15 11:36:09 +02:00
|
|
|
class AppThemeMap {
|
2019-09-02 15:52:32 +02:00
|
|
|
static const material = 0;
|
|
|
|
static const cupertino = 1;
|
2019-09-15 11:36:09 +02:00
|
|
|
static const values = [AppThemeMap.material, AppThemeMap.cupertino];
|
2019-09-02 15:52:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class ThemeModel with ChangeNotifier {
|
|
|
|
static const storageKey = 'theme';
|
|
|
|
|
|
|
|
int _theme;
|
|
|
|
int get theme => _theme;
|
|
|
|
bool get ready => _theme != null;
|
|
|
|
|
|
|
|
init() async {
|
|
|
|
var prefs = await SharedPreferences.getInstance();
|
|
|
|
|
|
|
|
int v = prefs.getInt(storageKey);
|
|
|
|
print('read theme: $v');
|
2019-09-15 11:36:09 +02:00
|
|
|
if (AppThemeMap.values.contains(v)) {
|
2019-09-02 15:52:32 +02:00
|
|
|
_theme = v;
|
|
|
|
} else if (Platform.isIOS) {
|
2019-09-15 11:36:09 +02:00
|
|
|
_theme = AppThemeMap.cupertino;
|
2019-09-02 15:52:32 +02:00
|
|
|
} else {
|
2019-09-15 11:36:09 +02:00
|
|
|
_theme = AppThemeMap.material;
|
2019-09-02 15:52:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> setTheme(int v) async {
|
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
|
|
|
|
_theme = v;
|
|
|
|
await prefs.setInt(storageKey, v);
|
|
|
|
print('write theme: $v');
|
|
|
|
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
void pushRoute({
|
|
|
|
@required BuildContext context,
|
|
|
|
@required WidgetBuilder builder,
|
|
|
|
bool fullscreenDialog = false,
|
|
|
|
}) {
|
|
|
|
switch (theme) {
|
2019-09-15 11:36:09 +02:00
|
|
|
case AppThemeMap.cupertino:
|
2019-09-02 15:52:32 +02:00
|
|
|
Navigator.of(context).push(CupertinoPageRoute(
|
|
|
|
builder: builder,
|
|
|
|
fullscreenDialog: fullscreenDialog,
|
|
|
|
));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Navigator.of(context).push(MaterialPageRoute(
|
|
|
|
builder: builder,
|
|
|
|
fullscreenDialog: fullscreenDialog,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> showConfirm(BuildContext context, String text) {
|
|
|
|
switch (theme) {
|
2019-09-15 11:36:09 +02:00
|
|
|
case AppThemeMap.cupertino:
|
2019-09-02 15:52:32 +02:00
|
|
|
return showCupertinoDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) {
|
|
|
|
return CupertinoAlertDialog(
|
|
|
|
title: Text(text),
|
|
|
|
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(
|
|
|
|
content: Text(
|
|
|
|
text,
|
|
|
|
// style: dialogTextStyle
|
|
|
|
),
|
|
|
|
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-15 11:36:09 +02:00
|
|
|
case AppThemeMap.cupertino:
|
2019-09-02 15:52:32 +02:00
|
|
|
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-15 11:36:09 +02:00
|
|
|
|
2019-09-19 13:52:18 +02:00
|
|
|
static Timer _debounce;
|
|
|
|
|
2019-09-19 14:50:47 +02:00
|
|
|
showPicker<T>(
|
2019-09-15 11:36:09 +02:00
|
|
|
BuildContext context, {
|
2019-09-19 13:52:18 +02:00
|
|
|
@required T initialValue,
|
|
|
|
@required List<PickerItem<T>> items,
|
|
|
|
@required Function(T item) onChange,
|
2019-09-19 14:50:47 +02:00
|
|
|
}) async {
|
2019-09-15 11:36:09 +02:00
|
|
|
switch (theme) {
|
|
|
|
case AppThemeMap.cupertino:
|
|
|
|
return showCupertinoModalPopup<T>(
|
|
|
|
context: context,
|
|
|
|
builder: (context) {
|
|
|
|
return Container(
|
|
|
|
height: 300,
|
|
|
|
child: CupertinoPicker(
|
2019-09-19 13:52:18 +02:00
|
|
|
backgroundColor: CupertinoColors.white,
|
|
|
|
children: items.map((item) => Text(item.text)).toList(),
|
|
|
|
itemExtent: 40,
|
|
|
|
scrollController: FixedExtentScrollController(
|
|
|
|
initialItem: items
|
|
|
|
.indexWhere((item) => item.value == initialValue)),
|
|
|
|
onSelectedItemChanged: (index) {
|
|
|
|
if (_debounce?.isActive ?? false) _debounce.cancel();
|
|
|
|
_debounce = Timer(const Duration(milliseconds: 500), () {
|
|
|
|
return onChange(items[index].value);
|
|
|
|
});
|
|
|
|
}),
|
2019-09-15 11:36:09 +02:00
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
default:
|
2019-09-19 14:50:47 +02:00
|
|
|
final value = await showMenu<T>(
|
2019-09-15 11:36:09 +02:00
|
|
|
context: context,
|
2019-09-19 14:50:47 +02:00
|
|
|
initialValue: initialValue,
|
|
|
|
items: items
|
|
|
|
.map((item) =>
|
|
|
|
PopupMenuItem(value: item.value, child: Text(item.text)))
|
|
|
|
.toList(),
|
|
|
|
position: RelativeRect.fill,
|
2019-09-15 11:36:09 +02:00
|
|
|
);
|
2019-09-19 14:50:47 +02:00
|
|
|
if (value != null) {
|
|
|
|
onChange(value);
|
|
|
|
}
|
2019-09-15 11:36:09 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-02 15:52:32 +02:00
|
|
|
}
|