Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dragging is interrupted when call setState on onSelect #165

Closed
kainosk opened this issue Feb 14, 2022 · 1 comment
Closed

Dragging is interrupted when call setState on onSelect #165

kainosk opened this issue Feb 14, 2022 · 1 comment

Comments

@kainosk
Copy link

kainosk commented Feb 14, 2022

When call setState on onSelect callback, my dragging is interrupted.

Maybe I missed something to use this picker correctly or a bug?

👇 Below is a code to reproduce the problem.

import 'package:flutter/material.dart';
import 'package:flutter_picker/flutter_picker.dart';

void main() {
  runApp(const _MyApp());
}

class _MyApp extends StatelessWidget {
  const _MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const _Page(),
    );
  }
}

class _Page extends StatefulWidget {
  const _Page({Key? key}) : super(key: key);
  @override
  State<_Page> createState() => _PageState();
}

class _PageState extends State<_Page> {
  int _selectedIndex = 0;
  final data = ['0', '1', '2', '3', '4', '5', '6'];

  @override
  Widget build(BuildContext context) {
    final selectedTextStyle =
        Theme.of(context).textTheme.bodyText1!.copyWith(color: Colors.orange);
    return Scaffold(
      appBar: AppBar(
        title: const Text('Issue reproduce sample'),
      ),
      body: Center(
        child: Picker(
          adapter: PickerDataAdapter<String>(pickerdata: data),
          selecteds: [_selectedIndex],
          height: 150,
          itemExtent: 28.0,
          hideHeader: true,
          selectedTextStyle: selectedTextStyle,
          onSelect: (picker, _, selectedIndexes) {
            _onItemSelected(selectedIndexes.first);
          },
        ).makePicker(),
      ),
    );
  }

  void _onItemSelected(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }
}

When I fix makePicker method like below, I can confirm expected behavior.

// 1. add key argument
Widget makePicker([ThemeData? themeData, bool isModal = false, Key? key]) {
    _maxLevel = adapter.maxLevel;
    adapter.picker = this;
    adapter.initSelects();
    _widget = PickerWidget(
      key: key ?? ValueKey(this), // 2. use key if key is passed
      child:
      _PickerWidget(picker: this, themeData: themeData, isModal: isModal),
      data: this,
    );
    return _widget!;
  }

So, I think setState will create new key ('ValueKey(this)') every time. And maybe this is unintentional code...?

Thank you for your great package ❤️

kainosk pushed a commit to kainosk/flutter_picker that referenced this issue Feb 14, 2022
@yangyxd
Copy link
Owner

yangyxd commented Feb 21, 2022

I have incorporated your modifications manually.

@yangyxd yangyxd closed this as completed Feb 21, 2022
yangyxd pushed a commit that referenced this issue Feb 21, 2022
yangyxd pushed a commit that referenced this issue Feb 21, 2022
* Fixed bug: #161, #165 ...
* Add `onBuilder`, `backgroundColor` parameter to `show`, `showDialog`, `showModal`
ymh-matsushita added a commit to ymh-matsushita/flutter_picker that referenced this issue Jul 4, 2024
…lutter-3.22.2-comatibility-with-fix-yangyxd#165

# Conflicts:
#	lib/Picker.dart
kainosk added a commit to kainosk/flutter_picker that referenced this issue Jul 8, 2024
* fix yangyxd#165: add Key parameter to `makePicker`

* accentColor -> colorScheme.secondary

---------

Co-authored-by: Kainosuke OBATA <[email protected]>
Co-authored-by: Tomohiro Furui <[email protected]>
Co-authored-by: kainosk.obata <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants