【问题标题】:How to scroll cupertino picker automatically flutter?如何滚动cupertino选择器自动颤动?
【发布时间】:2020-01-13 11:52:11
【问题描述】:

我正在为 IOS 开发一个新的颤振应用程序。我也在使用 Cupertino 选择器,我想自动滚动选择器,而不需要用户进行任何触摸。

我怎样才能做到这一点?

【问题讨论】:

  • 如果您需要帮助,您必须向我们展示您已经尝试过的内容。要提高获得高质量回复的几率,请查看问题指南:stackoverflow.com/help/how-to-ask

标签: ios flutter dart scroll picker


【解决方案1】:

您可以在下面复制粘贴运行完整代码
你可以使用scrollController

代码sn-p

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

CupertinoPicker(
                scrollController: scrollController,
                itemExtent: itemExtent,

工作演示

完整代码

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),
      ),
    );
  }
}

【讨论】:

  • 感谢好友。你拯救了我的一天
猜你喜欢
  • 2020-07-15
  • 1970-01-01
  • 2020-10-16
  • 2021-12-26
  • 2019-11-30
  • 1970-01-01
  • 2022-11-10
  • 2015-01-08
  • 1970-01-01
相关资源
最近更新 更多