首页 > 解决方案 > 按下按钮后更新文本 - 在 Flutter 中

问题描述

我目前正在研究随机辛普森剧集生成器。

我希望用户在按下一个按钮后获得一个随机季节和一个随机剧集。目前“onpressed”启动了一个名为“randomEpisode”的函数。在那里,季节和剧集是随机挑选的。

现在我不知道如何更新显示季节和剧集的文本。

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

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

class _HomeState extends State<Home> {

  Random random = new Random();
  int season = 0;
  int episode = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.yellow,
      body: SafeArea(
        child: Container(
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage('assets/Loading.jpg'),
              fit: BoxFit.fitWidth,
            ),
          ),
          child: Center(
            child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisSize: MainAxisSize.max,
                mainAxisAlignment: MainAxisAlignment.start,
                children: <Widget>[
                  SizedBox(height: 40.0),
                  Text(
                    'Season: $season ',
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 50.0,
                      letterSpacing: 2.0,
                      color: Colors.black,
                    ),
                  ),
                  SizedBox(height: 10.0),
                  Text(
                    'Episode:  $episode',
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 50.0,
                      color: Colors.black,
                    ),
                  ),
                  SizedBox(height: 40.0),
                  RaisedButton(
                    onPressed: () => randomEpisode(),
                    color: Colors.blue,
                    elevation: 5,
                    child: const Text('Random',
                        style: TextStyle(
                          fontSize: 20,
                          color: Colors.black,
                        )),
                  ),
                ]),
          ),
        ),
      ),
    );
  }


  randomEpisode() {
    List<int> numberepisodes = [
      13,
      22,
      24,
      22,
      22,
      25,
      25,
      25,
      25,
      23,
      22,
      21,
      22,
      22,
      22,
      21,
      22,
      22,
      20,
      21,
      23,
      22,
      22,
      22,
      22,
      22,
      22,
      22,
      21,
      23,
      22,
    ];

    Random random = new Random();
    int season;
    int episode;
    int maxseason;
    int maxepisode;
    //_changeEpisode();
    print('numberepisodes: $numberepisodes');
    maxseason = numberepisodes.length;
    print('maxseason $maxseason');
    season =
        1 + random.nextInt(numberepisodes.length - 1);

    print('season $season');
    maxepisode = numberepisodes[season - 1];
    print('maxepisodes $maxepisode');

    episode =
        1 + random.nextInt(numberepisodes[season - 1] - 1);
    print('Season: $season , Episode:  $episode');

  }


}

标签: flutterdartbuttonwidget

解决方案


randomEpisode()从方法中删除以下局部变量

int season;
int episode;

并在方法结束时调用以下randomEpisode()方法。

setState(() {});


推荐阅读