1
0
mirror of https://github.com/git-touch/git-touch synced 2024-12-17 18:59:23 +01:00

refactor: picker params

This commit is contained in:
Rongjian Zhang 2019-09-19 19:52:18 +08:00
parent 356a7f5e18
commit cc17a3e075
2 changed files with 42 additions and 47 deletions

View File

@ -1,5 +1,5 @@
import 'dart:async';
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
@ -10,6 +10,12 @@ class DialogOption<T> {
DialogOption({this.value, this.widget});
}
class PickerItem<T> {
final T value;
final String text;
PickerItem(this.value, {@required this.text});
}
class AppThemeMap {
static const material = 0;
static const cupertino = 1;
@ -182,11 +188,13 @@ class ThemeModel with ChangeNotifier {
}
}
static Timer _debounce;
Future<T> showPicker<T>(
BuildContext context, {
@required int initialItem,
@required List<Widget> children,
@required Function(int) onSelectedItemChanged,
@required T initialValue,
@required List<PickerItem<T>> items,
@required Function(T item) onChange,
}) {
switch (theme) {
case AppThemeMap.cupertino:
@ -197,12 +205,17 @@ class ThemeModel with ChangeNotifier {
height: 300,
child: CupertinoPicker(
backgroundColor: CupertinoColors.white,
children: children,
children: items.map((item) => Text(item.text)).toList(),
itemExtent: 40,
scrollController:
FixedExtentScrollController(initialItem: initialItem),
onSelectedItemChanged: onSelectedItemChanged,
),
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);
});
}),
);
},
);

View File

@ -7,7 +7,6 @@ import 'package:flutter_highlight/theme_map.dart';
import 'package:git_touch/models/code.dart';
import 'package:git_touch/models/theme.dart';
import 'package:git_touch/scaffolds/simple.dart';
import 'package:git_touch/utils/utils.dart';
import 'package:git_touch/widgets/app_bar_title.dart';
import 'package:git_touch/widgets/table_view.dart';
import 'package:provider/provider.dart';
@ -38,17 +37,12 @@ class CodeSettingsScreen extends StatelessWidget {
onTap: () {
Provider.of<ThemeModel>(context).showPicker(
context,
children: CodeModel.themes.map((k) => Text(k)).toList(),
initialItem:
CodeModel.themes.indexOf(codeProvider.theme),
onSelectedItemChanged: (int value) {
if (_themeDebounce?.isActive ?? false)
_themeDebounce.cancel();
_themeDebounce =
Timer(const Duration(milliseconds: 500), () {
Provider.of<CodeModel>(context)
.setTheme(CodeModel.themes[value]);
});
items: CodeModel.themes
.map((t) => PickerItem(t, text: t))
.toList(),
initialValue: codeProvider.theme,
onChange: (String value) {
Provider.of<CodeModel>(context).setTheme(value);
},
);
}),
@ -58,19 +52,13 @@ class CodeSettingsScreen extends StatelessWidget {
onTap: () {
Provider.of<ThemeModel>(context).showPicker(
context,
children: CodeModel.fontSizes
.map((k) => Text(k.toString()))
items: CodeModel.fontSizes
.map(
(size) => PickerItem(size, text: size.toString()))
.toList(),
initialItem:
CodeModel.fontSizes.indexOf(codeProvider.fontSize),
onSelectedItemChanged: (int value) {
if (_themeDebounce?.isActive ?? false)
_themeDebounce.cancel();
_themeDebounce =
Timer(const Duration(milliseconds: 500), () {
Provider.of<CodeModel>(context)
.setFontSize(CodeModel.fontSizes[value]);
});
initialValue: codeProvider.fontSize,
onChange: (int value) {
Provider.of<CodeModel>(context).setFontSize(value);
},
);
},
@ -81,18 +69,12 @@ class CodeSettingsScreen extends StatelessWidget {
onTap: () {
Provider.of<ThemeModel>(context).showPicker(
context,
children:
CodeModel.fontFamilies.map((k) => Text(k)).toList(),
initialItem: CodeModel.fontFamilies
.indexOf(codeProvider.fontFamily),
onSelectedItemChanged: (int value) {
if (_themeDebounce?.isActive ?? false)
_themeDebounce.cancel();
_themeDebounce =
Timer(const Duration(milliseconds: 500), () {
Provider.of<CodeModel>(context)
.setFontFamily(CodeModel.fontFamilies[value]);
});
items: CodeModel.fontFamilies
.map((v) => PickerItem(v, text: v))
.toList(),
initialValue: codeProvider.fontFamily,
onChange: (String value) {
Provider.of<CodeModel>(context).setFontFamily(value);
},
);
},