flutter 如何将StatelessWidget类更改为setState,以便图标在单击时刷新?

lztngnrs  于 2023-03-03  发布在  Flutter
关注(0)|答案(1)|浏览(205)

我在更改类时遇到了问题,无法在不重新加载应用程序的情况下更改图标。问题是我不知道如何正确地将StateWidget类更改为setState,因为没有它,我不知道应用程序将如何工作

import 'package:flutter/material.dart';

List done = [Icons.check_box];

class SecondRoute extends StatelessWidget {
  const SecondRoute({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blueGrey,
      body: Center(
        child: SafeArea(
          child: Column(
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: AppBar(
                  backgroundColor: Colors.black,
                  title: const Text('your list'),
                ),
              ),
              const Padding(
                padding: EdgeInsets.all(8.0),
                child: TextField(
                  keyboardType: TextInputType.multiline,
                  minLines: 1,//Normal textInputField will be displayed
                  maxLines: 5,
                ),
              ),
              IconButton(
                onPressed: () { done [0] = Icons.question_mark; },
                color: Colors.greenAccent,
                highlightColor: Colors.amberAccent, //<-- SEE HERE
                iconSize: 100,
                icon: Icon(
                  done[0],
                ),
              )
            ],
         ),
        ),
      ),
    );
  }
}

我试图改变第二条路线,但我不知道如何做到这一点,而不破坏整个代码。

xqk2d5yq

xqk2d5yq1#

您需要将其转换为StatefulWidget才能调用setState更多关于StatefulWidget

class SecondRoute extends StatefulWidget {
  const SecondRoute({super.key});

  @override
  State<SecondRoute> createState() => _SecondRouteState();
}

class _SecondRouteState extends State<SecondRoute> {
  List done = [Icons.check_box];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blueGrey,
      body: Center(
        child: SafeArea(
          child: Column(
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: AppBar(
                  backgroundColor: Colors.black,
                  title: const Text('your list'),
                ),
              ),
              const Padding(
                padding: EdgeInsets.all(8.0),
                child: TextField(
                  keyboardType: TextInputType.multiline,
                  minLines: 1, //Normal textInputField will be displayed
                  maxLines: 5,
                ),
              ),
              IconButton(
                onPressed: () {
                  done[0] = Icons.question_mark;
                  setState(() {});
                },
                color: Colors.greenAccent,
                highlightColor: Colors.amberAccent, //<-- SEE HERE
                iconSize: 100,
                icon: Icon(
                  done[0],
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}

相关问题