Saya memiliki Die StatefulWidget, dan fungsi roll di dalam _DieState.

class Die extends StatefulWidget {
  @override
  _DieState createState() => _DieState();
}

class _DieState extends State<Die> {

  Random rand = Random();
  double size;
  bool hold = false;
  int num;

  @override
  Widget build(BuildContext context) {

    void roll() {
      setState(() {
        if (!hold) {
          num = rand.nextInt(6) + 1;
        }
      });
    }

    size = min(MediaQuery.of(context).size.width * 0.15, MediaQuery.of(context).size.height * 0.15);

    return /**boring visual stuff here*/;
  }
}

Pada Home StatefulWidget saya, saya memiliki tombol "roll" yang dibungkus dengan widget GestureDetector dan 5 Die. Saya tidak tahu apa yang harus dimasukkan ke dalam onTap agar GestureDetector memanggil metode roll() di dalam setiap _DieState.

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  @override
  Widget build(BuildContext context) {
    List<Die> dice = [Die(),Die(),Die(),Die(),Die()];
    return Scaffold(
      body: Column(
        children: [
          Row(
            children: dice,
          ),
          GestureDetector(
            onTap: () {/**somehow call roll() for each Die*/},
          ),
        ],
      ),
    );
  }
}

Bantuan apa pun akan dihargai. Terima kasih.

0
sharkie 4 Juli 2020, 21:35

1 menjawab

Jawaban Terbaik

Coba ini, Gunakan kunci Global untuk melakukan itu

import 'package:flutter/material.dart';
import 'dart:math';

void main(){
  runApp(MaterialApp(home:Home()));
}

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  final GlobalKey<DieState> _callRollKey0 = GlobalKey<DieState>();
  final GlobalKey<DieState> _callRollKey1 = GlobalKey<DieState>();
  final GlobalKey<DieState> _callRollKey2 = GlobalKey<DieState>();
  final GlobalKey<DieState> _callRollKey3 = GlobalKey<DieState>();
  final GlobalKey<DieState> _callRollKey4 = GlobalKey<DieState>();
  @override
  Widget build(BuildContext context) {
    List<Die> dice = [Die(key: _callRollKey0,), Die(key: _callRollKey1), Die(key: _callRollKey2), Die(key: _callRollKey3), Die(key: _callRollKey4)];
    return Scaffold(
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: dice,
          ),
          GestureDetector(
            child: Container(
              margin: EdgeInsets.all(20),
              height: 50,
              width: 50,
              color: Colors.red,
              child: Center(child: Text('tap')),
            ),
            onTap: () {
              _callRollKey0.currentState.roll();
              _callRollKey1.currentState.roll();
              _callRollKey2.currentState.roll();
              _callRollKey3.currentState.roll();
              _callRollKey4.currentState.roll();

            },
          ),
        ],
      ),
    );
  }

}

class Die extends StatefulWidget {
  Die({Key key}):super(key:key);
  @override
  DieState createState() => DieState();
}

class DieState extends State<Die> {

  Random rand = Random();
  double size;
  bool hold = false;
  int num= 1;

  void roll() {
    setState(() {
      if (!hold) {
        num = rand.nextInt(6) + 1;
      }
    });
  }
  @override
  Widget build(BuildContext context) {



    size = min(MediaQuery.of(context).size.width * 0.15, MediaQuery.of(context).size.height * 0.15);

    return Container(
      color: Colors.green,
      width: size,
      height: 50,
      child: Center(
        child:Text(
          '$num',
        ),
      ),
    );
  }
}

Semoga ini membantu

1
Niteesh 4 Juli 2020, 20:07