简体   繁体   English

有状态小部件未重建

[英]Stateful widget not rebuilding

In following code, there's a Greetings list, a Text widget and a 'Refresh' Elevated button.在下面的代码中,有一个问候列表、一个文本小部件和一个“刷新”高架按钮。 The entire app is a Stateful widget.整个应用程序是一个有状态的小部件。 I want the Text to change to the next greeting from the User list, when the user clicks the Refresh button.当用户单击刷新按钮时,我希望文本更改为用户列表中的下一个问候语。

However, when I run it, the Text doesn't change.但是,当我运行它时,文本不会改变。 All that happens is that the 'We have more greetings' text gets printed in the terminal.所发生的一切是“我们有更多的问候”文本被打印在终端中。

Please help if you know how to resolve this!如果您知道如何解决此问题,请提供帮助!

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MyAppState();
  }
}

class _MyAppState extends State<MyApp> {
  var _greetingsIndex = 0;
  var _greetings = [
    "Hey, how are you!",
    "Trust you're fine, my friend!",
    "What's up!!!",
    "How are you doing!",
  ];

  void _refreshGreetings() {
    setState(() {
      _greetingsIndex = _greetingsIndex + 1;
    });
    if (_greetingsIndex < _greetings.length) {
      print('We have more greetings');
    } else {
      setState(() {
        _greetingsIndex = 0;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        home: Scaffold(
          appBar: AppBar(
            title: Text('Greetings app'),
          ),
          body: Column(
            children: [
              Text(_greetings[0]),
              ElevatedButton(
                  onPressed: _refreshGreetings, child: Text('Refresh'))
            ],
          ),
        ));
  }
}

You have a typo - you always show greeting with index 0. Instead of:你有一个错字 - 你总是用索引 0 显示问候语。而不是:

body: Column(
            children: [
              Text(_greetings[0]),
              ElevatedButton(
                  onPressed: _refreshGreetings, child: Text('Refresh'))
            ],
          )

Use利用

body: Column(
            children: [

              // this line needed fixing
              Text(_greetings[_greetingsIndex]),

              ElevatedButton(
                  onPressed: _refreshGreetings, child: Text('Refresh'))
            ],
          )

it is not changing because you are specifically asking for the first element of the list.它没有改变,因为您特别要求列表的第一个元素。 the state is changing and the index is changing just fine, but you are asking for _greetings[0] in your code. state 正在发生变化,并且索引正在发生变化,但是您在代码中要求_greetings[0] change Text(_greetings[0]) to Text(_greetings[_greetingsIndex]) and it should work just fine.Text(_greetings[0])更改为Text(_greetings[_greetingsIndex]) ,它应该可以正常工作。

by the way, this code will break when reaching the end of the list because you are rebuilding before checking the length.顺便说一句,当到达列表末尾时,此代码将中断,因为您在检查长度之前正在重建。 i suggest moving this piece我建议移动这件作品

setState(() {
  _greetingsIndex = _greetingsIndex + 1;
});

to the inside of the if statement, then your _refreshGreetings function should look like this:在 if 语句的内部,您的 _refreshGreetings function 应该如下所示:

void _refreshGreetings() {
  if (_greetingsIndex < _greetings.length) {
    print('We have more greetings');
    setState(() {
      _greetingsIndex = _greetingsIndex + 1;
    });
  }
}

Change this Text(_greetings[0]) to this => Text(_greetings[_greetingsIndex]) .将此Text(_greetings[0])更改为此 => Text(_greetings[_greetingsIndex])

and your refresh function to this:并将您的 function 刷新到此:

void _refreshGreetings() {
    if (_greetingsIndex < _greetings.length) {
    setState(() {
      _greetingsIndex = _greetingsIndex + 1;
    });
      print('We have more greetings');
    } else {
      setState(() {
        _greetingsIndex = 0;
      });
    }
  }

It should solve your problem.它应该可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM