fixed primary color picker overscroll

This commit is contained in:
Vadim 2023-02-04 22:56:51 +03:00
parent 00e104de9b
commit b5ebc09d68

View file

@ -13,10 +13,7 @@ class PrimaryColorDialogPicker extends StatefulWidget {
class _PrimaryColorDialogPickerState extends State<PrimaryColorDialogPicker> { class _PrimaryColorDialogPickerState extends State<PrimaryColorDialogPicker> {
late Color _selected = Theme.of(context).primaryColor; late Color _selected = Theme.of(context).primaryColor;
late final ScrollController _scrollController = ScrollController( final ScrollController _scrollController = ScrollController();
initialScrollOffset:
ThemeProvider.primaryColorsList.indexOf(_selected) * (Dimens.grid48 + Dimens.grid8),
);
@override @override
void dispose() { void dispose() {
@ -37,24 +34,30 @@ class _PrimaryColorDialogPickerState extends State<PrimaryColorDialogPicker> {
content: SizedBox( content: SizedBox(
height: Dimens.grid48, height: Dimens.grid48,
width: double.maxFinite, width: double.maxFinite,
child: ListView.separated( child: SingleChildScrollView(
controller: _scrollController, controller: _scrollController,
scrollDirection: Axis.horizontal, scrollDirection: Axis.horizontal,
padding: EdgeInsets.zero, padding: EdgeInsets.zero,
separatorBuilder: (_, __) => const SizedBox(width: Dimens.grid8), child: Row(
itemCount: ThemeProvider.primaryColorsList.length, children: List.generate(
itemBuilder: (_, index) { ThemeProvider.primaryColorsList.length,
final color = ThemeProvider.primaryColorsList[index]; (index) {
return _SelectableColorItem( final color = ThemeProvider.primaryColorsList[index];
color: color, return Padding(
selected: color.value == _selected.value, padding: EdgeInsets.only(left: index == 0 ? 0 : Dimens.paddingS),
onTap: () { child: _SelectableColorItem(
setState(() { color: color,
_selected = color; selected: color.value == _selected.value,
}); onTap: () {
setState(() {
_selected = color;
});
},
),
);
}, },
); ),
}, ),
), ),
), ),
actionsPadding: const EdgeInsets.fromLTRB( actionsPadding: const EdgeInsets.fromLTRB(
@ -79,7 +82,7 @@ class _PrimaryColorDialogPickerState extends State<PrimaryColorDialogPicker> {
} }
} }
class _SelectableColorItem extends StatelessWidget { class _SelectableColorItem extends StatefulWidget {
final Color color; final Color color;
final bool selected; final bool selected;
final VoidCallback onTap; final VoidCallback onTap;
@ -90,19 +93,34 @@ class _SelectableColorItem extends StatelessWidget {
required this.onTap, required this.onTap,
}) : super(key: ValueKey(color)); }) : super(key: ValueKey(color));
@override
State<_SelectableColorItem> createState() => _SelectableColorItemState();
}
class _SelectableColorItemState extends State<_SelectableColorItem> {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
if (widget.selected) {
Scrollable.ensureVisible(context);
}
});
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return GestureDetector( return GestureDetector(
onTap: onTap, onTap: widget.onTap,
child: FilledCircle( child: FilledCircle(
size: Dimens.grid48, size: Dimens.grid48,
color: color, color: widget.color,
child: AnimatedSwitcher( child: AnimatedSwitcher(
duration: Dimens.durationS, duration: Dimens.durationS,
child: selected child: widget.selected
? Icon( ? Icon(
Icons.check, Icons.check,
color: ThemeData.estimateBrightnessForColor(color) == Brightness.light color: ThemeData.estimateBrightnessForColor(widget.color) == Brightness.light
? Colors.black ? Colors.black
: Colors.white, : Colors.white,
) )