not working
not working

Reputation: 657

How to scroll cupertino picker automatically flutter?

I am developing a new flutter app for IOS. and I am using the Cupertino picker as well and I want to scroll the picker automatically without any touching from the users.

How can I achieve this?

Upvotes: 6

Views: 5984

Answers (1)

chunhunghan
chunhunghan

Reputation: 54377

You can copy paste run full code below
You can use scrollController

code snippet

scrollController.animateTo(itemExtent,
    duration: Duration(milliseconds: 200), curve: Curves.ease);

CupertinoPicker(
                scrollController: scrollController,
                itemExtent: itemExtent,

working demo

enter image description here

full code

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class BuildingProblem {
  static List<Icon> problemListIcons = [];
  static List<String> problemListNames = [];
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  int _selectedColorIndex = 0;
  double itemExtent = 40.0;
  FixedExtentScrollController scrollController;

  void _incrementCounter() {
    scrollController.animateTo(itemExtent,
        duration: Duration(milliseconds: 200), curve: Curves.ease);
    setState(() {
      _counter++;
    });
  }

  @override
  void initState() {
    scrollController =
        FixedExtentScrollController(initialItem: _selectedColorIndex);

    BuildingProblem.problemListIcons.add(Icon(Icons.add));
    BuildingProblem.problemListIcons.add(Icon(Icons.cast));
    BuildingProblem.problemListIcons.add(Icon(Icons.link));
    BuildingProblem.problemListNames.add("add");
    BuildingProblem.problemListNames.add("cast");
    BuildingProblem.problemListNames.add("link");
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Expanded(
              child: CupertinoPicker(
                scrollController: scrollController,
                itemExtent: itemExtent,
                children: <Widget>[
                  for (var i = 0;
                      i < BuildingProblem.problemListIcons.length;
                      i++)
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        BuildingProblem.problemListIcons[i],
                        Padding(
                          padding: EdgeInsets.only(left: 10),
                          child: Text(
                            BuildingProblem.problemListNames[i],
                            style: TextStyle(color: Colors.white70),
                          ),
                        )
                      ],
                    ),
                ],
                onSelectedItemChanged: (int index) {
                  print('good boi');
                },
                looping: true,
                backgroundColor: Color(0xff2e3032),
              ),
            ),
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

Upvotes: 8

Related Questions