簡體   English   中英

為什么 flutter_fortune_wheel 總是在頁面打開后立即運行?

[英]why flutter_fortune_wheel always run instantly after page open?

您好,我想在我的項目中使用 flutter_fortune_wheel。 我希望輪子只在用戶點擊按鈕或觸摸輪子時移動,但輪子總是在屏幕顯示后立即移動。 我設置了“animateFirst:false”,但沒有用。 我怎樣才能解決這個問題? 這是我的 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)),
                  
                ],
              ),
            ),

這是我的按鈕代碼:

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

試試這個

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM