简体   繁体   English

为什么 flutter_fortune_wheel 总是在页面打开后立即运行?

[英]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.

相关问题 为什么 flutter 在导航弹出后重新加载页面 - Why flutter reload page after navigation pop 更新应用程序后,MainApplication 是否会一直运行,即使用户没有打开它? - Will MainApplication always run after updating the app, even if the user does not open it? 当我打开我的 flutter 应用程序时,它总是显示登录屏幕几秒钟,即使我已经在上次会话中登录。 在那个主页之后? - When I open my flutter app it always shows Sign In screen for few seconds, even if I already signed In in my last session. And after that Main page? 为什么在 Flutter 运行后出现此错误? - Why I am getting this error after Flutter Run? Flutter:始终保持打开状态的自定义抽屉 - Flutter: Custom Drawer that always stays open Flutter 打开应用程序后崩溃 - Flutter crash after open apps Flutter 页面查看如何不总是从初始页面开始? - Flutter Page View how to not always start at the initialPage? 为什么 flutter 命令“flutter run --release”不起作用? - Why is the flutter command, “flutter run --release” not working? 导航抽屉打开时立即关闭 - Navigation Drawer closes instantly on open 打开pdf始终打开文档的第一页 - Open pdf always open document 1st page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM