mirror of
https://github.com/git-touch/git-touch
synced 2025-03-05 19:57:42 +01:00
refactor: picker params
This commit is contained in:
parent
356a7f5e18
commit
cc17a3e075
@ -1,5 +1,5 @@
|
|||||||
|
import 'dart:async';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:flutter/cupertino.dart';
|
import 'package:flutter/cupertino.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
@ -10,6 +10,12 @@ class DialogOption<T> {
|
|||||||
DialogOption({this.value, this.widget});
|
DialogOption({this.value, this.widget});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class PickerItem<T> {
|
||||||
|
final T value;
|
||||||
|
final String text;
|
||||||
|
PickerItem(this.value, {@required this.text});
|
||||||
|
}
|
||||||
|
|
||||||
class AppThemeMap {
|
class AppThemeMap {
|
||||||
static const material = 0;
|
static const material = 0;
|
||||||
static const cupertino = 1;
|
static const cupertino = 1;
|
||||||
@ -182,11 +188,13 @@ class ThemeModel with ChangeNotifier {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Timer _debounce;
|
||||||
|
|
||||||
Future<T> showPicker<T>(
|
Future<T> showPicker<T>(
|
||||||
BuildContext context, {
|
BuildContext context, {
|
||||||
@required int initialItem,
|
@required T initialValue,
|
||||||
@required List<Widget> children,
|
@required List<PickerItem<T>> items,
|
||||||
@required Function(int) onSelectedItemChanged,
|
@required Function(T item) onChange,
|
||||||
}) {
|
}) {
|
||||||
switch (theme) {
|
switch (theme) {
|
||||||
case AppThemeMap.cupertino:
|
case AppThemeMap.cupertino:
|
||||||
@ -196,13 +204,18 @@ class ThemeModel with ChangeNotifier {
|
|||||||
return Container(
|
return Container(
|
||||||
height: 300,
|
height: 300,
|
||||||
child: CupertinoPicker(
|
child: CupertinoPicker(
|
||||||
backgroundColor: CupertinoColors.white,
|
backgroundColor: CupertinoColors.white,
|
||||||
children: children,
|
children: items.map((item) => Text(item.text)).toList(),
|
||||||
itemExtent: 40,
|
itemExtent: 40,
|
||||||
scrollController:
|
scrollController: FixedExtentScrollController(
|
||||||
FixedExtentScrollController(initialItem: initialItem),
|
initialItem: items
|
||||||
onSelectedItemChanged: onSelectedItemChanged,
|
.indexWhere((item) => item.value == initialValue)),
|
||||||
),
|
onSelectedItemChanged: (index) {
|
||||||
|
if (_debounce?.isActive ?? false) _debounce.cancel();
|
||||||
|
_debounce = Timer(const Duration(milliseconds: 500), () {
|
||||||
|
return onChange(items[index].value);
|
||||||
|
});
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
@ -7,7 +7,6 @@ import 'package:flutter_highlight/theme_map.dart';
|
|||||||
import 'package:git_touch/models/code.dart';
|
import 'package:git_touch/models/code.dart';
|
||||||
import 'package:git_touch/models/theme.dart';
|
import 'package:git_touch/models/theme.dart';
|
||||||
import 'package:git_touch/scaffolds/simple.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/app_bar_title.dart';
|
||||||
import 'package:git_touch/widgets/table_view.dart';
|
import 'package:git_touch/widgets/table_view.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
@ -38,17 +37,12 @@ class CodeSettingsScreen extends StatelessWidget {
|
|||||||
onTap: () {
|
onTap: () {
|
||||||
Provider.of<ThemeModel>(context).showPicker(
|
Provider.of<ThemeModel>(context).showPicker(
|
||||||
context,
|
context,
|
||||||
children: CodeModel.themes.map((k) => Text(k)).toList(),
|
items: CodeModel.themes
|
||||||
initialItem:
|
.map((t) => PickerItem(t, text: t))
|
||||||
CodeModel.themes.indexOf(codeProvider.theme),
|
.toList(),
|
||||||
onSelectedItemChanged: (int value) {
|
initialValue: codeProvider.theme,
|
||||||
if (_themeDebounce?.isActive ?? false)
|
onChange: (String value) {
|
||||||
_themeDebounce.cancel();
|
Provider.of<CodeModel>(context).setTheme(value);
|
||||||
_themeDebounce =
|
|
||||||
Timer(const Duration(milliseconds: 500), () {
|
|
||||||
Provider.of<CodeModel>(context)
|
|
||||||
.setTheme(CodeModel.themes[value]);
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}),
|
}),
|
||||||
@ -58,19 +52,13 @@ class CodeSettingsScreen extends StatelessWidget {
|
|||||||
onTap: () {
|
onTap: () {
|
||||||
Provider.of<ThemeModel>(context).showPicker(
|
Provider.of<ThemeModel>(context).showPicker(
|
||||||
context,
|
context,
|
||||||
children: CodeModel.fontSizes
|
items: CodeModel.fontSizes
|
||||||
.map((k) => Text(k.toString()))
|
.map(
|
||||||
|
(size) => PickerItem(size, text: size.toString()))
|
||||||
.toList(),
|
.toList(),
|
||||||
initialItem:
|
initialValue: codeProvider.fontSize,
|
||||||
CodeModel.fontSizes.indexOf(codeProvider.fontSize),
|
onChange: (int value) {
|
||||||
onSelectedItemChanged: (int value) {
|
Provider.of<CodeModel>(context).setFontSize(value);
|
||||||
if (_themeDebounce?.isActive ?? false)
|
|
||||||
_themeDebounce.cancel();
|
|
||||||
_themeDebounce =
|
|
||||||
Timer(const Duration(milliseconds: 500), () {
|
|
||||||
Provider.of<CodeModel>(context)
|
|
||||||
.setFontSize(CodeModel.fontSizes[value]);
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
@ -81,18 +69,12 @@ class CodeSettingsScreen extends StatelessWidget {
|
|||||||
onTap: () {
|
onTap: () {
|
||||||
Provider.of<ThemeModel>(context).showPicker(
|
Provider.of<ThemeModel>(context).showPicker(
|
||||||
context,
|
context,
|
||||||
children:
|
items: CodeModel.fontFamilies
|
||||||
CodeModel.fontFamilies.map((k) => Text(k)).toList(),
|
.map((v) => PickerItem(v, text: v))
|
||||||
initialItem: CodeModel.fontFamilies
|
.toList(),
|
||||||
.indexOf(codeProvider.fontFamily),
|
initialValue: codeProvider.fontFamily,
|
||||||
onSelectedItemChanged: (int value) {
|
onChange: (String value) {
|
||||||
if (_themeDebounce?.isActive ?? false)
|
Provider.of<CodeModel>(context).setFontFamily(value);
|
||||||
_themeDebounce.cancel();
|
|
||||||
_themeDebounce =
|
|
||||||
Timer(const Duration(milliseconds: 500), () {
|
|
||||||
Provider.of<CodeModel>(context)
|
|
||||||
.setFontFamily(CodeModel.fontFamilies[value]);
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user