gpt4 book ai didi

ios - 如何滚动库比蒂诺选取器自动 flutter ?

转载 作者:行者123 更新时间:2023-11-29 05:10:39 25 4
gpt4 key购买 nike

我正在为 IOS 开发一个新的 flutter 应用程序。我也在使用库比蒂诺选择器,我想自动滚动选择器,而无需用户进行任何触摸。

我怎样才能做到这一点?

最佳答案

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

代码片段

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

CupertinoPicker(
scrollController: scrollController,
itemExtent: itemExtent,

工作演示

enter image description here

完整代码

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

关于ios - 如何滚动库比蒂诺选取器自动 flutter ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59716138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com