diff --git a/example/lib/main.dart b/example/lib/main.dart index 511477d..1df6696 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -26,15 +26,12 @@ const Set _kTouchLikeDeviceTypes = { }; class _MyAppState extends State { - bool dark = false; - @override Widget build(BuildContext context) { return MaterialApp( theme: ThemeData( primarySwatch: Colors.blue, fontFamily: _fontFamily, - brightness: dark ? Brightness.dark : Brightness.light, primaryTextTheme: TextTheme().apply(fontFamily: _fontFamily), ), scrollBehavior: const MaterialScrollBehavior() @@ -76,19 +73,10 @@ class _MyHomePageState extends State { automaticallyImplyLeading: false, elevation: 0.0, actions: [ - IconButton( - onPressed: () { - final appState = context.findAncestorStateOfType<_MyAppState>(); - if (appState == null) return; - appState.setState(() { - appState.dark = !appState.dark; - }); - }, - icon: Icon(Icons.sunny_snowing)), IconButton( onPressed: () => Navigator.push( context, MaterialPageRoute(builder: (v) => RawPickerTest())), - icon: Icon(Icons.add)), + icon: Icon(Icons.add)) ], ), body: Builder( @@ -185,12 +173,6 @@ class _MyHomePageState extends State { showPickerCustomBuilder(context); }, ), - ListTile( - title: Text('16. Select year'), - onTap: () { - showPickerSelectYear(context); - }, - ), const SizedBox(height: 60), ], )), @@ -198,8 +180,7 @@ class _MyHomePageState extends State { } showMsg(String msg) { - final state = ScaffoldMessenger.of(context); - state.showSnackBar(SnackBar(content: Text(msg))); + ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(msg))); } showPicker(BuildContext context) async { @@ -208,11 +189,7 @@ class _MyHomePageState extends State { pickerData: JsonDecoder().convert(PickerData)), changeToFirst: false, textAlign: TextAlign.left, - textStyle: TextStyle( - color: Theme.of(context).brightness == Brightness.dark - ? Colors.yellow - : Colors.blue, - fontFamily: _fontFamily), + textStyle: TextStyle(color: Colors.blue, fontFamily: _fontFamily), selectedTextStyle: TextStyle(color: Colors.red), columnPadding: const EdgeInsets.all(8.0), onConfirm: (Picker picker, List value) { @@ -436,7 +413,7 @@ class _MyHomePageState extends State { ), title: Text("Select DateTime"), textAlign: TextAlign.right, - // selectedTextStyle: TextStyle(color: Colors.blue), + selectedTextStyle: TextStyle(color: Colors.blue), delimiter: [ PickerDelimiter( column: 5, @@ -444,6 +421,7 @@ class _MyHomePageState extends State { width: 16.0, alignment: Alignment.center, child: Text(':', style: TextStyle(fontWeight: FontWeight.bold)), + color: Colors.white, )) ], footer: Container( @@ -567,6 +545,7 @@ class _MyHomePageState extends State { width: 12.0, alignment: Alignment.center, child: Text(':', style: TextStyle(fontWeight: FontWeight.bold)), + color: Colors.white, )), ], title: Text("Select DateTime"), @@ -579,7 +558,7 @@ class _MyHomePageState extends State { picker.showModal(context, backgroundColor: Colors.transparent, builder: (context, view) { return Material( - // color: Colors.white, + color: Colors.white, borderRadius: BorderRadius.only( topLeft: Radius.circular(10), topRight: Radius.circular(10)), child: Container( @@ -694,7 +673,7 @@ class _MyHomePageState extends State { fontWeight: FontWeight.bold, color: Colors.red, fontSize: 14)), - // color: Colors.white, + color: Colors.white, )), ], onSelect: (Picker picker, int index, List selected) { @@ -733,7 +712,7 @@ class _MyHomePageState extends State { fontWeight: FontWeight.bold, color: Colors.red, fontSize: 14)), - // color: Colors.white, + color: Colors.white, )), ], onSelect: (Picker picker, int index, List selected) { @@ -748,21 +727,4 @@ class _MyHomePageState extends State { ); }); } - - showPickerSelectYear(BuildContext context) { - Picker( - adapter: DateTimePickerAdapter( - type: PickerDateTimeType.kY, - yearSuffix: "年", - yearBegin: 1950, - yearEnd: 2025, - ), - title: Text("Select Year"), - onConfirm: (Picker picker, List value) { - print(picker.adapter.text); - }, - onSelect: (Picker picker, int index, List selected) { - print(picker.adapter.toString()); - }).showBottomSheet(context); - } } diff --git a/lib/picker.dart b/lib/picker.dart index 6762f13..0acc638 100644 --- a/lib/picker.dart +++ b/lib/picker.dart @@ -71,7 +71,7 @@ class Picker { final IconThemeData? selectedIconTheme; /// Text scaling factor - final double? textScaleFactor; + final TextScaler? textScaler; final EdgeInsetsGeometry? columnPadding; final Color? backgroundColor, headerColor, containerColor; @@ -123,13 +123,13 @@ class Picker { this.selectedTextStyle, this.selectedIconTheme, this.textAlign = TextAlign.start, - this.textScaleFactor, + this.textScaler, this.title, this.cancel, this.confirm, this.cancelText, this.confirmText, - this.backgroundColor, + this.backgroundColor = Colors.white, this.containerColor, this.headerColor, this.builderHeader, @@ -155,6 +155,7 @@ class Picker { } Widget? get widget => _widget; + PickerWidgetState? get state => _state; int _maxLevel = 1; @@ -208,7 +209,8 @@ class Picker { Color? backgroundColor, PickerWidgetBuilder? builder}) async { return await showModalBottomSheet( - context: context, //state.context, + context: context, + //state.context, isScrollControlled: isScrollControlled, useRootNavigator: useRootNavigator, backgroundColor: backgroundColor, @@ -217,22 +219,15 @@ class Picker { return builder == null ? picker : builder(context, picker); }); } - - /// get widget - Widget getWidget(BuildContext context) { - return builder == null ? picker : builder(context, picker); - } /// show dialog picker Future?> showDialog(BuildContext context, {bool barrierDismissible = true, Color? backgroundColor, - Color? barrierColor, PickerWidgetBuilder? builder, Key? key}) { return Dialog.showDialog>( context: context, - barrierColor: barrierColor, barrierDismissible: barrierDismissible, builder: (BuildContext context) { final actions = []; @@ -325,6 +320,7 @@ class Picker { class PickerDelimiter { final Widget? child; final int column; + PickerDelimiter({required this.child, this.column = 1}); } @@ -344,8 +340,10 @@ class PickerItem { class PickerWidget extends InheritedWidget { final Picker data; + const PickerWidget({Key? key, required this.data, required Widget child}) : super(key: key, child: child); + @override bool updateShouldNotify(covariant PickerWidget oldWidget) => oldWidget.data != data; @@ -360,6 +358,7 @@ class _PickerWidget extends StatefulWidget { final Picker picker; final ThemeData? themeData; final bool isModal; + _PickerWidget( {Key? key, required this.picker, this.themeData, required this.isModal}) : super(key: key); @@ -372,6 +371,7 @@ class _PickerWidget extends StatefulWidget { class PickerWidgetState extends State<_PickerWidget> { final Picker picker; final ThemeData? themeData; + PickerWidgetState({required this.picker, this.themeData}); ThemeData? theme; @@ -433,7 +433,7 @@ class PickerWidgetState extends State<_PickerWidget> { bottom: BorderSide(color: theme!.dividerColor, width: 0.5), ), color: picker.headerColor == null - ? theme?.bottomAppBarTheme.color + ? (theme!.bottomAppBarTheme.color) : picker.headerColor, ), )); @@ -526,7 +526,7 @@ class PickerWidgetState extends State<_PickerWidget> { onPressed: onPressed, child: Text(_txt, overflow: TextOverflow.ellipsis, - textScaleFactor: MediaQuery.of(context).textScaleFactor, + textScaler: MediaQuery.of(context).textScaler, style: textStyle)); } else { return textStyle == null @@ -548,13 +548,13 @@ class PickerWidgetState extends State<_PickerWidget> { PickerAdapter? adapter = picker.adapter; adapter.setColumn(-1); - final _decoration = BoxDecoration( - color: picker.containerColor == null - ? theme!.dialogBackgroundColor - : picker.containerColor, - ); - if (adapter.length > 0) { + var _decoration = BoxDecoration( + color: picker.containerColor == null + ? theme!.dialogTheme.backgroundColor + : picker.containerColor, + ); + for (int i = 0; i < picker._maxLevel; i++) { Widget view = Expanded( flex: adapter.getColumnFlex(i), @@ -613,12 +613,7 @@ class PickerWidgetState extends State<_PickerWidget> { for (int i = 0; i < picker.delimiter!.length; i++) { var o = picker.delimiter![i]; if (o.child == null) continue; - var item = SizedBox( - child: DecoratedBox( - decoration: _decoration, - child: o.child, - ), - height: picker.height); + var item = SizedBox(child: o.child, height: picker.height); if (o.column < 0) items.insert(0, item); else if (o.column >= items.length) @@ -715,9 +710,13 @@ abstract class PickerAdapter { Picker? picker; int getLength(); + int getMaxLevel(); + void setColumn(int index); + void initSelects(); + Widget buildItem(BuildContext context, int index); /// 是否需要更新前面的列 @@ -727,9 +726,6 @@ abstract class PickerAdapter { } Widget makeText(Widget? child, String? text, bool isSel) { - final theme = picker!.textStyle != null || picker!.state?.context == null - ? null - : Theme.of(picker!.state!.context); return Center( child: DefaultTextStyle( overflow: TextOverflow.ellipsis, @@ -737,12 +733,13 @@ abstract class PickerAdapter { textAlign: picker!.textAlign, style: picker!.textStyle ?? TextStyle( - color: theme?.brightness == Brightness.dark - ? Colors.white - : Colors.black87, - fontFamily: theme == null - ? "" - : theme.textTheme.titleLarge?.fontFamily, + color: Colors.black87, + fontFamily: picker?.state?.context != null + ? Theme.of(picker!.state!.context) + .textTheme + .titleLarge! + .fontFamily + : "", fontSize: Picker.DefaultTextSize), child: child != null ? (isSel && picker!.selectedIconTheme != null @@ -752,7 +749,7 @@ abstract class PickerAdapter { ) : child) : Text(text ?? "", - textScaleFactor: picker!.textScaleFactor, + textScaler: picker!.textScaler, style: (isSel ? picker!.selectedTextStyle : null)))); } @@ -763,11 +760,8 @@ abstract class PickerAdapter { items.add( child ?? Text(text, style: (isSel ? picker!.selectedTextStyle : null))); if (suffix != null) items.add(suffix); - final theme = picker!.textStyle != null || picker!.state?.context == null - ? null - : Theme.of(picker!.state!.context); - Color? _txtColor = - theme?.brightness == Brightness.dark ? Colors.white : Colors.black87; + + Color? _txtColor = Colors.black87; double? _txtSize = Picker.DefaultTextSize; if (isSel && picker!.selectedTextStyle != null) { if (picker!.selectedTextStyle!.color != null) @@ -776,18 +770,14 @@ abstract class PickerAdapter { _txtSize = picker!.selectedTextStyle!.fontSize; } - return Center( + return new Center( + //alignment: Alignment.center, child: DefaultTextStyle( overflow: TextOverflow.ellipsis, maxLines: 1, textAlign: picker!.textAlign, style: picker!.textStyle ?? - TextStyle( - color: _txtColor, - fontSize: _txtSize, - fontFamily: theme == null - ? "" - : theme.textTheme.titleLarge?.fontFamily), + TextStyle(color: _txtColor, fontSize: _txtSize), child: Wrap( children: items, ))); @@ -802,6 +792,7 @@ abstract class PickerAdapter { } void doShow() {} + void doSelect(int column, int index) {} int getColumnFlex(int column) { @@ -1290,10 +1281,8 @@ class DateTimePickerAdapter extends PickerAdapter { _columnType = columnType[type]; var month = _columnType.indexWhere((element) => element == 1); var day = _columnType.indexWhere((element) => element == 2); - if (month != -1 && day != -1) { - _needUpdatePrev = day < month || - day < _columnType.indexWhere((element) => element == 0); - } + _needUpdatePrev = + day < month || day < _columnType.indexWhere((element) => element == 0); if (!_needUpdatePrev) { // check am/pm before hour-ap var ap = _columnType.indexWhere((element) => element == 6); diff --git a/test/flutter_picker_test.dart b/test/flutter_picker_test.dart index 88efb2c..c62e31f 100644 --- a/test/flutter_picker_test.dart +++ b/test/flutter_picker_test.dart @@ -4,7 +4,7 @@ import 'package:flutter_test/flutter_test.dart'; void main() { test('adds one to input values', () { - final v = null as List; + final List? v = null; print(v); expect(v, null); // final calculator = Calculator();