【问题标题】:Flutter Error - The argument type 'MaterialStateProperty<RoundedRectangleBorder>' can't be assigned to the parameter type 'OutlinedBorderFlutter 错误 - 参数类型 \'MaterialStateProperty<RoundedRectangleBorder>\' 不能分配给参数类型 \'OutlinedBorder
【发布时间】:2022-12-03 06:43:48
【问题描述】:

想自定义按钮的默认边框半径但是出现如下错误: “不能将参数类型‘MaterialStateProperty’分配给参数类型‘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)),
                  )),
            ),

【问题讨论】:

    标签: flutter error-handling flutter-layout syntax-error


    【解决方案1】:

    只需删除 MaterialStateProperty.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)),
          )),
    )
    

    【讨论】:

      【解决方案2】:

      如果您不想避免只需要添加通用类型,这对输入很有帮助。 Documentation

      高架按钮( 样式:按钮样式( 背景颜色:MaterialStateProperty.all(Colors.red), 形状:MaterialStateProperty.all( const RoundedRectangleBorder( borderRadius: BorderRadius.all(Radius.circular(8)), ), )), child: const Text('Sign in with Google'), onPressed: () {}, );

      【讨论】:

        【解决方案3】:

        这对类型很有帮助,如果你想使用你只需要添加通用类型。 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: () {},
            );
        

        【讨论】:

          猜你喜欢
          • 2021-08-21
          • 2022-09-26
          • 2022-01-23
          • 2021-09-21
          • 2022-10-02
          • 1970-01-01
          • 2020-02-20
          • 2021-07-05
          • 2020-01-23
          相关资源
          最近更新 更多