[英]why flutter_fortune_wheel always run instantly after page open?
Hi I want use flutter_fortune_wheel in my project.您好,我想在我的项目中使用 flutter_fortune_wheel。 I want the wheel move only user click on button or touch the wheel, But the wheel moved always instantly after screen show.我希望轮子只在用户点击按钮或触摸轮子时移动,但轮子总是在屏幕显示后立即移动。 I set "animateFirst: false", but not working.我设置了“animateFirst:false”,但没有用。 how can i fix this?我怎样才能解决这个问题? this is my fortune_wheel code:这是我的 fortune_wheel 代码:
Expanded(
child: FortuneWheel(
selected: Stream.value(selected),
animateFirst: false,
duration: const Duration(seconds: 3),
items: [
FortuneItem(child: Text("1", style: subtitleText)),
FortuneItem(child: Text("2", style: subtitleText)),
FortuneItem(child: Text("3", style: subtitleText)),
FortuneItem(child: Text("4", style: subtitleText)),
],
),
),
and this is my button code:这是我的按钮代码:
ElevatedButton(
style: ElevatedButton.styleFrom(
fixedSize: const Size.fromHeight(40),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50),
),
backgroundColor: Colors.deepOrange,
// Background color
elevation: 5,
// Elevation
shadowColor: Colors.black),
onPressed: () {
setState(
() {
selected = Random().nextInt(4);
},
);
},
child: Text(
"spin",
style: smallText,
),
),
Try this one试试这个
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_fortune_wheel/flutter_fortune_wheel.dart';
List<FortuneItem> items = const [
FortuneItem(child: Text("1")),
FortuneItem(child: Text("2")),
FortuneItem(child: Text("3")),
FortuneItem(child: Text("4")),
];
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
StreamController<int> selected = StreamController<int>();
@override
initState() {
super.initState();
}
@override
void dispose() {
selected.close();
super.dispose();
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: Column(
children: [
Expanded(
child: FortuneWheel(
selected: selected.stream,
animateFirst: false,
duration: const Duration(seconds: 3),
items: items,
),
),
ElevatedButton(
style: ElevatedButton.styleFrom(
fixedSize: const Size.fromHeight(40),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50),
),
elevation: 5,
shadowColor: Colors.black),
onPressed: () {
setState(
() => selected.add(
Fortune.randomInt(0, items.length),
),
);
},
child: const Text(
"spin",
style: TextStyle(color: Colors.white),
),
)
],
),
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.