1
0
mirror of https://github.com/git-touch/git-touch synced 2024-12-17 10:48:50 +01:00

refactor: picker group

This commit is contained in:
Rongjian Zhang 2019-09-29 15:02:06 +08:00
parent 2c64179715
commit cba1173e30
3 changed files with 62 additions and 59 deletions

View File

@ -199,7 +199,7 @@ class ThemeModel with ChangeNotifier {
switch (theme) {
case AppThemeType.cupertino:
default:
await showCupertinoModalPopup<void>(
await showCupertinoModalPopup(
context: context,
builder: (context) {
return Container(
@ -213,10 +213,12 @@ class ThemeModel with ChangeNotifier {
.toList()
.indexWhere((v) => v.value == groupItem.value)),
onSelectedItemChanged: (index) {
if (_debounce?.isActive ?? false) _debounce.cancel();
if (_debounce?.isActive ?? false) {
_debounce.cancel();
}
_debounce = Timer(const Duration(milliseconds: 500), () {
return groupItem
.onChange(groupItem.items.toList()[index].value);
groupItem.onChange(groupItem.items[index].value);
});
},
),

View File

@ -3,9 +3,11 @@ import 'package:flutter/material.dart';
import 'package:flutter_highlight/flutter_highlight.dart';
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/single.dart';
import 'package:git_touch/widgets/app_bar_title.dart';
import 'package:git_touch/widgets/picker.dart';
import 'package:git_touch/widgets/table_view.dart';
import 'package:provider/provider.dart';
class CodeThemeScreen extends StatelessWidget {
@ -17,39 +19,69 @@ class CodeThemeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
var codeProvider = Provider.of<CodeModel>(context);
var themeProvider = Provider.of<ThemeModel>(context);
return SingleScaffold(
title: AppBarTitle('Code theme'),
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
PickerGroup(
TableView(
items: [
PickerGroupItem(
title: 'Syntax Highlighting',
value: codeProvider.theme,
items: CodeModel.themes.map((v) => PickerItem(v, text: v)),
onChange: (value) {
Provider.of<CodeModel>(context).setTheme(value);
TableViewItem(
text: Text('Syntax Highlighting'),
rightWidget: Text(codeProvider.theme),
onTap: () {
themeProvider.showPicker(
context,
PickerGroupItem(
value: codeProvider.theme,
items: CodeModel.themes
.map((v) => PickerItem(v, text: v))
.toList(),
onChange: (value) {
Provider.of<CodeModel>(context).setTheme(value);
},
),
);
},
),
PickerGroupItem(
title: 'Font Size',
value: codeProvider.fontSize.toString(),
items: CodeModel.fontSizes
.map((v) => PickerItem(v.toString(), text: v.toString())),
onChange: (value) {
Provider.of<CodeModel>(context)
.setFontSize(int.tryParse(value) ?? 16);
TableViewItem(
text: Text('Font Size'),
rightWidget: Text(codeProvider.fontSize.toString()),
onTap: () {
themeProvider.showPicker(
context,
PickerGroupItem(
value: codeProvider.fontSize.toString(),
items: CodeModel.fontSizes
.map((v) =>
PickerItem(v.toString(), text: v.toString()))
.toList(),
onChange: (value) {
Provider.of<CodeModel>(context)
.setFontSize(int.tryParse(value) ?? 16);
},
),
);
},
),
PickerGroupItem(
title: 'Font Family',
value: codeProvider.fontFamily,
items:
CodeModel.fontFamilies.map((v) => PickerItem(v, text: v)),
onChange: (String value) {
Provider.of<CodeModel>(context).setFontFamily(value);
TableViewItem(
text: Text('Font Family'),
rightWidget: Text(codeProvider.fontFamily),
onTap: () {
themeProvider.showPicker(
context,
PickerGroupItem(
value: codeProvider.fontFamily,
items: CodeModel.fontFamilies
.map((v) => PickerItem(v, text: v))
.toList(),
onChange: (String value) {
Provider.of<CodeModel>(context).setFontFamily(value);
},
),
);
},
),
],

View File

@ -1,8 +1,5 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:git_touch/models/theme.dart';
import 'package:git_touch/widgets/table_view.dart';
import 'package:provider/provider.dart';
class PickerItem<T> {
final T value;
@ -11,41 +8,13 @@ class PickerItem<T> {
}
class PickerGroupItem<T> {
final String title;
final T value;
final Iterable<PickerItem<T>> items;
final List<PickerItem<T>> items;
final Function(T value) onChange;
PickerGroupItem({
@required this.title,
@required this.value,
@required this.items,
@required this.onChange,
});
}
class PickerGroup extends StatelessWidget {
final Iterable<PickerGroupItem<String>> items;
PickerGroup({@required this.items});
@override
Widget build(BuildContext context) {
var themeProvider = Provider.of<ThemeModel>(context);
switch (themeProvider.theme) {
case AppThemeType.cupertino:
default:
// TODO: Material
return TableView(
items: items.map((item) {
return TableViewItem(
text: Text(item.title),
rightWidget: Text(item.value),
onTap: () {
themeProvider.showPicker(context, item);
},
);
}),
);
}
}
}