flutter - How to make a raised button that has a gradient background?

后端 未结 10 1914
野性不改
野性不改 2021-01-31 14:52

Is there a way to change the raised button background color to a gradient? if not, how can I achieve something like this?

10条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-31 15:48

    All the solution above are not really working without some minor artifacts or issues (e.g. missing ripple effect, unwanted borders, not respecting the theme's minWidth for buttons).

    The solution below has none of the above issues (the critical part is to use the Ink widget to retain the ripple capabilities over the gradient):

    RaisedButton(
      onPressed: () { },
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(80.0)),
      padding: const EdgeInsets.all(0.0),
      child: Ink(
        decoration: const BoxDecoration(
          gradient: myGradient,
          borderRadius: BorderRadius.all(Radius.circular(80.0)),
        ),
        child: Container(
          constraints: const BoxConstraints(minWidth: 88.0, minHeight: 36.0), // min sizes for Material buttons
          alignment: Alignment.center,
          child: const Text(
            'OK',
            textAlign: TextAlign.center,
          ),
        ),
      ),
    )
    

提交回复
热议问题