简体   繁体   English

在 Flutter 中更改高架按钮 OnPressed 的背景颜色

[英]Change Background Color of Elevated Button OnPressed in Flutter

I need to change background color of elevated button, on click to indicate it as selected.我需要更改提升按钮的背景颜色,单击以将其指示为已选中。 I have tried this.我试过这个。

  class _MyState extends State<MyPage> {
  bool _flag = true;
    @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: ElevatedButton(
              onPressed: () => setState(() => _flag = !_flag),
              child: Text(_flag ? 'Red' : 'Green'),
              style: ElevatedButton.styleFrom(
                backgroundColor: _flag ? Colors.red : Colors.teal, 
              ),
            ),
          ),
        );
      }
    }

Here on onPressed(): The color is not changing在 onPressed() 上:颜色没有改变

On DartPad your code works Link to dartpad .在 DartPad 上,您的代码可以正常工作Link to dartpad
Be sure you implemented correctly the stateful widget:确保您正确实施了有状态小部件:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {

  const MyHomePage({
    Key? key,
  }) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
   bool _flag = true;
    @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: ElevatedButton(
              onPressed: () => setState(() => _flag = !_flag),
              child: Text(_flag ? 'Red' : 'Green'),
              style: ElevatedButton.styleFrom(
                backgroundColor: _flag ? Colors.red : Colors.teal, 
              ),
            ),
          ),
        );
      }
}

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

相关问题 如何更改高架按钮的标题和颜色 - How to change Elevated button's title and color onpressed 如何从 function 更改 Flutter 中提升按钮的背景颜色? - How to change background color of Elevated Button in Flutter from function? 我可以在 flutter 中永久更改提升按钮的背景颜色吗? - Can I change the background color of elevated button permanently in flutter? Flutter - 更改 TextFormField onPressed 的背景颜色(焦点) - Flutter - Change background color of TextFormField onPressed (focus) 更改按钮的背景颜色和文本颜色 onPressed 并在未按下 Flutter 时设置回默认值 - Change Background Color and Text Color of a button onPressed and set back to default when unPressed Flutter 颤动中凸起按钮的 OnPressed 中的颜色变化 - Color Change in the OnPressed of the RaisedButton in flutter 为什么单选按钮不改变颜色 onPressed - Flutter? - why radio button don't change color onPressed - Flutter? 如何在onPressed()中动态更改凸起按钮的背景颜色 - How to change the background color of raised button dynamically in onPressed() Flutter GetX 在 Elevated Button onPressed 中调用更新的变量 - Flutter GetX calling updated variables in Elevated Button onPressed Flutter - 使用 onPressed 更改图像(不在按钮中) - Flutter - image change with onPressed (not in a button)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM