我想将 ListWheelScrollView 定位到随机给定的索引。 在定位时,旋转动画也必须工作。 如何定位到给定的索引?
编辑:它就像老虎机一样。
答案 0 :(得分:1)
您可以在下面复制粘贴运行完整代码
您可以使用 FixedExtentScrollController
并调用 animateToItem
代码片段
final FixedExtentScrollController _controller = FixedExtentScrollController();
...
body: Center(
child: ListWheelScrollView(
controller: _controller,
itemExtent: 80,
magnification: 1.2,
useMagnifier: true,
physics: FixedExtentScrollPhysics(),
children: listtiles, //List of widgets
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
_controller.animateToItem(random.nextInt(10),
duration: Duration(milliseconds: 500), curve: Curves.linear);
},
child: Icon(Icons.add),
backgroundColor: Colors.green,
),
工作演示
完整代码
import 'package:flutter/material.dart';
import 'dart:math';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final FixedExtentScrollController _controller = FixedExtentScrollController();
Random random = Random();
List<Widget> listtiles = [
GestureDetector(
onTap: () {
print("clicked 1");
},
child: ListTile(
leading: Icon(Icons.portrait),
title: Text("Portrait"),
subtitle: Text("Beautiful View..!"),
trailing: Icon(Icons.arrow_forward_ios),
),
),
GestureDetector(
onTap: () {
print("clicked 2");
},
child: ListTile(
leading: Icon(Icons.landscape),
title: Text("LandScape"),
subtitle: Text("Beautiful View..!"),
trailing: Icon(Icons.remove),
),
),
GestureDetector(
onTap: () {
print("clicked 3");
},
child: ListTile(
leading: Icon(Icons.map),
title: Text("Map"),
subtitle: Text("Map View..!"),
trailing: Icon(Icons.wb_sunny),
),
),
ListTile(
leading: Icon(Icons.landscape),
title: Text("LandScape"),
subtitle: Text("Wonderful View..!"),
trailing: Icon(Icons.wb_sunny),
),
ListTile(
leading: Icon(Icons.list),
title: Text("List Example"),
subtitle: Text("List Wheel Scroll view .!"),
trailing: Icon(Icons.cloud),
),
ListTile(
leading: Icon(Icons.settings),
title: Text("Settings"),
subtitle: Text("Change the setting..!"),
trailing: Icon(Icons.portrait),
),
ListTile(
leading: Icon(Icons.event),
title: Text("Add data"),
subtitle: Text("Data View..!"),
trailing: Icon(Icons.add),
),
ListTile(
leading: Icon(Icons.landscape),
title: Text("LandScape"),
subtitle: Text("Beautiful View..!"),
trailing: Icon(Icons.wb_sunny),
),
ListTile(
leading: Icon(Icons.email),
title: Text("Email"),
subtitle: Text("Check Email..!"),
trailing: Icon(Icons.arrow_forward),
),
ListTile(
leading: Icon(Icons.games),
title: Text("Games"),
subtitle: Text("Play Games..!"),
trailing: Icon(Icons.zoom_out_map),
),
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("ListView ScrollView Wheel"),
),
body: Center(
child: ListWheelScrollView(
controller: _controller,
itemExtent: 80,
magnification: 1.2,
useMagnifier: true,
physics: FixedExtentScrollPhysics(),
children: listtiles, //List of widgets
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
_controller.animateToItem(random.nextInt(10),
duration: Duration(milliseconds: 500), curve: Curves.linear);
},
child: Icon(Icons.add),
backgroundColor: Colors.green,
),
);
}
}