refactor: extract picker widget

This commit is contained in:
Rongjian Zhang
2019-09-19 22:01:30 +08:00
parent cd87829b30
commit 9b2e9686d7
4 changed files with 121 additions and 103 deletions

View File

@@ -20,7 +20,7 @@ class CodeModel with ChangeNotifier {
];
String _theme = 'github';
int _fontSize = 14;
int _fontSize = 16;
String _fontFamily = 'System';
String get theme => _theme;

View File

@@ -1,4 +1,3 @@
import 'dart:async';
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
@@ -10,12 +9,6 @@ class DialogOption<T> {
DialogOption({this.value, this.widget});
}
class PickerItem<T> {
final T value;
final String text;
PickerItem(this.value, {@required this.text});
}
class AppThemeType {
static const material = 0;
static const cupertino = 1;
@@ -187,53 +180,4 @@ class ThemeModel with ChangeNotifier {
);
}
}
static Timer _debounce;
showPicker<T>(
BuildContext context, {
@required T initialValue,
@required List<PickerItem<T>> items,
@required Function(T item) onChange,
}) async {
switch (theme) {
case AppThemeType.cupertino:
await showCupertinoModalPopup<void>(
context: context,
builder: (context) {
return Container(
height: 300,
child: CupertinoPicker(
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);
});
},
),
);
},
);
break;
default:
final value = await showMenu<T>(
context: context,
initialValue: initialValue,
items: items
.map((item) =>
PopupMenuItem(value: item.value, child: Text(item.text)))
.toList(),
position: RelativeRect.fill,
);
if (value != null) {
onChange(value);
}
}
}
}