首页 > 解决方案 > 如何从 RaisedButton 创建 ElevatedButton?

问题描述

有很多属性,RaisedButton我在ElevatedButton. 那么,如何将 a RaisedButton(具有所有属性)转换或复制到 new ElevatedButton

喜欢:

RaisedButton(
  onPressed: () {},
  color: Colors.indigoAccent,
  disabledColor: Colors.indigo,
  textColor: Colors.white,
  disabledTextColor: Colors.grey,
  hoverColor: Colors.pinkAccent,
  splashColor: Colors.black,
  elevation: 12.0,
  padding: EdgeInsets.all(20),
  shape: StadiumBorder(),
  child: Text('Button'),
)

标签: flutterdartdart-null-safety

解决方案


您应该改用您的theme属性MaterialApp()和相应的按钮主题,例如:

MaterialApp(
  theme: ThemeData(
    primaryColor: Colors.blue,
    elevatedButtonTheme: ElevatedButtonThemeData(
        style: ElevatedButton.styleFrom(
            primary: Colors.blue,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(8.0),
            ),
            textStyle: TextStyle(fontSize: 20.0)
        )
    ),
    textButtonTheme: TextButtonThemeData(
      style: TextButton.styleFrom(
        primary: Colors.black,
        textStyle: TextStyle(
          fontWeight: FontWeight.bold
        )
      )
   ),
 ),
)

对于单个按钮样式,您可以使用:

ElevatedButton(
  style: ElevatedButton.styleFrom(
    minimumSize: Size(30.0, 36.0),
    primary: Colors.green,
    padding: EdgeInsets.all(15.0),
    shape: RoundedRectangleBorder(
       borderRadius: new BorderRadius.circular(10.0)
    ),
  ),
),

另请参阅新按钮和按钮主题


推荐阅读