我想将按钮 colored颜色 从浅蓝色更改为绿色,以及文本. 我可以改变文字,但我不能改变 colored颜色 .我还没解决. 我想在'onPressed()'中使用'setState()'来在我按下按钮时改变 colored颜色 . 如果我使用styleFrom,我可以在setState中使用styleFrom来更改 colored颜色 吗? 我该怎么办? 有它的源代码.

import 'package:flutter/material.dart';

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

/// This is the main application widget.
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: const MyStatefulWidget(),
      ),
    );
  }
}

/// This is the stateful widget that the main application instantiates.
class MyStatefulWidget extends StatefulWidget {
  const MyStatefulWidget({Key? key}) : super(key: key);

  @override
  State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}

/// This is the private State class that goes with MyStatefulWidget.
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  String frontText = "Press the Button to see studuent infomation";
  @override
  Widget build(BuildContext context) { // Defining Main Widget plqcing on Scaffold
    final ButtonStyle style = ElevatedButton.styleFrom();

    return Center(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Text(
            "$frontText", key: Key("textKey"),
            style: TextStyle(color: Colors.blue, fontSize: 15),
          ),
          const SizedBox(height: 20),
          ElevatedButton(
            style: ElevatedButton.styleFrom( // Setting the ElevatedButton style
                backgroundColor: Colors.lightBlue[50],
                foregroundColor: Colors.blue,
                textStyle: TextStyle(fontSize: 20)
            ),
            child: const Text('Push'),
            onPressed: () {
              setState(() { // once push the button, text printed over the button will be changed
                frontText = "20190952, 윤영준";
                ElevatedButton.styleFrom(
                  backgroundColor: Colors.green
                ); // I want change color to green, but i can't
              });
            },
          ),
        ],
      ),
    );
  }
}

enter image description here

推荐答案

你有几个问题:

style是定义的,但它的值从来没有使用过.此外,您必须使它成为一个字段,而不是构建方法中的属性.

ButtonStyle style = ElevatedButton.styleFrom();

您必须使用样式属性而不是创建新属性.

ElevatedButton(
    style: style,
    ...
),

最后但并非最不重要的是,ElevatedButton.styleFrom没有任何作用,你必须重写style值来应用新的更改.

setState(() {
   ...
   style = ElevatedButton.styleFrom(backgroundColor: Colors.green);
   // or if you want to preserve other configurations
   // style = style.copyWith(...);
});

P.S.我就是这么做的:

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  bool pressed = false;
  
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Text(
            pressed ? "20190952, 윤영준" : "Press the Button to see studuent infomation",
            key: Key("textKey"),
            style: TextStyle(color: Colors.blue, fontSize: 15),
          ),
          const SizedBox(height: 20),
          ElevatedButton(
            style: ElevatedButton.styleFrom( // Setting the ElevatedButton style
                backgroundColor: pressed ? Colors.green : Colors.lightBlue[50],
                foregroundColor: Colors.blue,
                textStyle: TextStyle(fontSize: 20)
            ),
            child: const Text('Push'),
            onPressed: () {
              setState(() {
                pressed = true;
              });
            },
          ),
        ],
      ),
    );
  }
}

Flutter相关问答推荐

在Flutter 中压缩图片返回空

如何在2024年使用Ffltter将用户数据保存在云FireStore中?

'Flutter的无效常数值错误

我怎样才能在Ffltter中显示html布局链接?

如何在抖动中将PNG图像转换为WebP

如何从RealmResults获取Flutter Flutter 流?

Flutter:无法构建iOS应用程序-找不到PigeonParser.h文件

使用 Firebase Realtime 实现高效流式传输

如何在 VS Code 中启用 Flutter 运行/调试工具栏?

要禁用按钮上的通知声音,请点击Flutter

在参数为空时保持命名参数的默认值

允许文本小部件在容器之间溢出

像图像一样Flutter 自定义标签

小部件堆栈未显示顶层

减少 Flutter 中 API 的连接等待时间

如何在 flutter 中创建渐变按钮

无法在 flutter 中更新 void 方法内的变量值

如何获得Flutter 的时差?

如何从用户 Flutter 导航抽屉中隐藏管理菜单

如何在Flutter 的 initState 中初始化 Firestore 查询