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

picker doesn't update adapter start/end values in setState #115

Closed
fatimahossny opened this issue Jul 13, 2020 · 3 comments
Closed

picker doesn't update adapter start/end values in setState #115

fatimahossny opened this issue Jul 13, 2020 · 3 comments

Comments

@fatimahossny
Copy link

I use picker as a widget and i want to update adapter start & end to new value every time the user click on button ,
but it only keeps the first values it's get, i even try with state to call setState but it still not working . here is my code any help is appreciated

class _MyHomePageState extends State<MyHomePage> {
  int start = 0;
  int end = 5;

  void _updatePicker() {
    setState(() {
      start = 12;
      end = 24;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Picker(
            adapter: NumberPickerAdapter(data: [
              NumberPickerColumn(begin: start, end: end),
              NumberPickerColumn(begin: 0, end: 59),
            ]),
            delimiter: null,
            hideHeader: true,
            itemExtent: 32.0,
            height: 120.0,
            textScaleFactor: 1.1,
            columnPadding: EdgeInsets.all(6.0),
            selectedTextStyle: TextStyle(color: Colors.blue, fontSize: 18.0),
            onSelect: (Picker picker, int i, List value) {
              print(picker.getSelectedValues());
              List selectedValues = picker.getSelectedValues();
            }).makePicker(ThemeData(
          backgroundColor: Colors.white,
        )),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _updatePicker,
        child: Icon(Icons.access_time),
      ),
    );
  }
}
@williams-gunawan
Copy link

williams-gunawan commented Nov 26, 2020

you @fatimahossny can try this, this is not the efficient one, but kinda like workaround

short Explanation: this will force picker widget to force reload, do note that you must wrap one of your picker inside other widget on futureBuilder like the example below.
sorry for my bad english :')

class _MyHomePageState extends State<MyHomePage> {
  int start = 0;
  int end = 5;
  Future forceRebuild = Future.delayed(Duration.zero);
  void _updatePicker() {
    setState(() {
      start = 12;
      end = 24;
       forceRebuild = Future.delayed(Duration.zero);
    });
  }

  @override
  Widget build(BuildContext context) {
   ....
      body: Center(
        child: FutureBuilder(
            future: forceRefreshLuasBangunan,
            builder: (context, snapshot) {
              if (snapshot.connectionState == ConnectionState.waiting) 
                return Container(
                             child: {YourNumberPicker}
                );
              return {YourNumberPicker};
      ),
      .....
}

@yangyxd
Copy link
Owner

yangyxd commented Mar 26, 2021

Thanks! I've fixed this bug.

@yangyxd yangyxd closed this as completed Mar 26, 2021
@doanbh
Copy link

doanbh commented Jul 27, 2021

Thanks! I've fixed this bug.

I've setState but NumberPickerAdapter keep end with count of old state.

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

No branches or pull requests

4 participants