弹奏错误 - 参数类型' motitalStateProperty< roundedRectangleBorder>'可以将分配给参数类型

发布于 2025-02-11 20:46:00 字数 1091 浏览 2 评论 0原文

想要自定义按钮的默认边框半径,但存在以下错误: “参数类型'材料StateProperty'不能分配给参数类型'outlinedBorder'

Padding(
              padding: const EdgeInsets.all(20.0),
              child: ElevatedButton(
                  style: ElevatedButton.styleFrom(
                      shape: MaterialStateProperty.all<RoundedRectangleBorder>(
                          RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(10.0))),
                      elevation: 0,
                      primary: Colors.white,
                      minimumSize: const Size.fromHeight(50)),
                  onPressed: () {
                    Navigator.pushNamed(context, '/home');
                    // Navigator.of(context).pushReplacementNamed('/home');
                  },
                  child: const Text(
                    "Start",
                    style: TextStyle(
                        fontWeight: FontWeight.bold,
                        fontSize: 18,
                        color: Color.fromRGBO(86, 96, 49, 1)),
                  )),
            ),

want to customize the button's default border radius but having the following error:
"The argument type 'MaterialStateProperty' can't be assigned to the parameter type 'OutlinedBorder"

Padding(
              padding: const EdgeInsets.all(20.0),
              child: ElevatedButton(
                  style: ElevatedButton.styleFrom(
                      shape: MaterialStateProperty.all<RoundedRectangleBorder>(
                          RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(10.0))),
                      elevation: 0,
                      primary: Colors.white,
                      minimumSize: const Size.fromHeight(50)),
                  onPressed: () {
                    Navigator.pushNamed(context, '/home');
                    // Navigator.of(context).pushReplacementNamed('/home');
                  },
                  child: const Text(
                    "Start",
                    style: TextStyle(
                        fontWeight: FontWeight.bold,
                        fontSize: 18,
                        color: Color.fromRGBO(86, 96, 49, 1)),
                  )),
            ),

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦在深巷 2025-02-18 20:46:00

只需删除材料StateProperty.All(或用以下代码替换您的代码。

 Padding(
  padding: const EdgeInsets.all(20.0),
  child: ElevatedButton(
      style: ElevatedButton.styleFrom(
          shape:RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(30.0),
          ),
          elevation: 0,
          primary: Colors.white,
          minimumSize: const Size.fromHeight(50)),
      onPressed: () {
        Navigator.pushNamed(context, '/home');
        // Navigator.of(context).pushReplacementNamed('/home');
      },
      child: const Text(
        "Start",
        style: TextStyle(
            fontWeight: FontWeight.bold,
            fontSize: 18,
            color: Color.fromRGBO(86, 96, 49, 1)),
      )),
)

Just remove MaterialStateProperty.all( or replace your code with below.

 Padding(
  padding: const EdgeInsets.all(20.0),
  child: ElevatedButton(
      style: ElevatedButton.styleFrom(
          shape:RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(30.0),
          ),
          elevation: 0,
          primary: Colors.white,
          minimumSize: const Size.fromHeight(50)),
      onPressed: () {
        Navigator.pushNamed(context, '/home');
        // Navigator.of(context).pushReplacementNamed('/home');
      },
      child: const Text(
        "Start",
        style: TextStyle(
            fontWeight: FontWeight.bold,
            fontSize: 18,
            color: Color.fromRGBO(86, 96, 49, 1)),
      )),
)
南汐寒笙箫 2025-02-18 20:46:00

对于类型来说,这很有帮助,如果您想使用,只需添加通用类型即可。 文档

 ElevatedButton(
      style: ButtonStyle(
          backgroundColor: MaterialStateProperty.all<Color>(Colors.red),
          shape: MaterialStateProperty.all<RoundedRectangleBorder>(
            const RoundedRectangleBorder(
              borderRadius: BorderRadius.all(Radius.circular(8)),
            ),
          )),
      child: const Text('Text'),
      onPressed: () {},
    );

That's helpful for the types, if you want to use you only have to add the generic type. Documentation

 ElevatedButton(
      style: ButtonStyle(
          backgroundColor: MaterialStateProperty.all<Color>(Colors.red),
          shape: MaterialStateProperty.all<RoundedRectangleBorder>(
            const RoundedRectangleBorder(
              borderRadius: BorderRadius.all(Radius.circular(8)),
            ),
          )),
      child: const Text('Text'),
      onPressed: () {},
    );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文