From cba1173e30a6f4c5fa0ff0234341e603663d6488 Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Sun, 29 Sep 2019 15:02:06 +0800 Subject: [PATCH] refactor: picker group --- lib/models/theme.dart | 10 +++-- lib/screens/code_theme.dart | 76 ++++++++++++++++++++++++++----------- lib/widgets/picker.dart | 35 +---------------- 3 files changed, 62 insertions(+), 59 deletions(-) diff --git a/lib/models/theme.dart b/lib/models/theme.dart index 31ca9ae..ba58d9f 100644 --- a/lib/models/theme.dart +++ b/lib/models/theme.dart @@ -199,7 +199,7 @@ class ThemeModel with ChangeNotifier { switch (theme) { case AppThemeType.cupertino: default: - await showCupertinoModalPopup( + 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); }); }, ), diff --git a/lib/screens/code_theme.dart b/lib/screens/code_theme.dart index 98de1ca..8b27276 100644 --- a/lib/screens/code_theme.dart +++ b/lib/screens/code_theme.dart @@ -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(context); + var themeProvider = Provider.of(context); return SingleScaffold( title: AppBarTitle('Code theme'), body: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ - PickerGroup( + TableView( items: [ - PickerGroupItem( - title: 'Syntax Highlighting', - value: codeProvider.theme, - items: CodeModel.themes.map((v) => PickerItem(v, text: v)), - onChange: (value) { - Provider.of(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(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(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(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(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(context).setFontFamily(value); + }, + ), + ); }, ), ], diff --git a/lib/widgets/picker.dart b/lib/widgets/picker.dart index d239529..bb90039 100644 --- a/lib/widgets/picker.dart +++ b/lib/widgets/picker.dart @@ -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 { final T value; @@ -11,41 +8,13 @@ class PickerItem { } class PickerGroupItem { - final String title; final T value; - final Iterable> items; + final List> 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> items; - - PickerGroup({@required this.items}); - - @override - Widget build(BuildContext context) { - var themeProvider = Provider.of(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); - }, - ); - }), - ); - } - } -}