首页 > 解决方案 > 我无法更改 ElevatedButton 的颜色。扑

问题描述

问题是什么?
该按钮位于渐变的背景上并采用其颜色,但我无法更改颜色。
也许问题出在渐变中?虽然我试图去除容器的装饰,但没有帮助。也许它是一个容器?

@override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      body: Container(
        decoration: BoxDecoration(
          gradient: LinearGradient(
              begin: Alignment.topLeft,
              end: Alignment.bottomRight,
              colors: [Colors.black, Colors.blue]),
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Padding(...),
            CircularPercentIndicator(...),
            Padding(...),
            SizedBox(height: 20),
            Padding(
              padding: EdgeInsets.only(bottom: 30.0),
              child: ElevatedButton(
                child: Text("Start destroying"),
                onPressed: null,
                style: ElevatedButton.styleFrom(
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(30.0),
                  ),
                  primary: Colors.black,
                  padding: EdgeInsets.symmetric(horizontal: 60, vertical: 15),
                  textStyle: TextStyle(
                    fontSize: 25,
                    color: Colors.white,
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

标签: flutterdart

解决方案


您需要为以下设置一个非空onPressed函数ElevatedButton

ElevatedButton(
  child: Text("Start destroying"),
  onPressed: () {}, // add this
  style: ElevatedButton.styleFrom(
    shape: RoundedRectangleBorder(
      borderRadius: BorderRadius.circular(30.0),
    ),
    primary: Colors.red,
    padding: EdgeInsets.symmetric(horizontal: 60, vertical: 15),
    textStyle: TextStyle(
      fontSize: 25,
      color: Colors.white,
    ),
  ),
),

推荐阅读