0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Flutter】RRectでgradationalなButtonの実装

Posted at

背景

デザイナーさんに
「角っこが丸く、グラデーションがかかったボタンが欲しいな」
と言われた。

ElevatedButtonは角っこが丸くなっている。
Containerはグラデーションできる。

でも、上記2つの組合せは出来なかった。

だけど、デザイナーの意向を汲んでボタンを実装する必要が僕にはあった。

やりたかったこと

image.png

実装

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: const [
              Button1(),
              SizedBox(height: 20),
              Button2(),
            ],
          ),
        ),
      ),
    );
  }
}

class Button1 extends StatelessWidget {
  const Button1({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        onTap: () {/* action */},
        child: ClipRRect(
            borderRadius: BorderRadius.circular(20.0),
            child: Container(
                height: 100,
                width: 100,
                decoration: const BoxDecoration(
                  gradient: LinearGradient(
                      begin: FractionalOffset.topLeft,
                      end: FractionalOffset.bottomRight,
                      // colorsとstopsの項目数を合わせましょう
                      colors: [
                        Colors.deepOrangeAccent,
                        Colors.orangeAccent,
                        Colors.amberAccent,
                        Colors.black
                      ],
                      // 右下隅っこに黒をクッキリと
                      stops: [
                        0.1,
                        0.5,
                        0.9,
                        0.9
                      ]),
                ),
                child: const Center(child: Icon(Icons.favorite, size: 40)))));
  }
}

class Button2 extends StatelessWidget {
  const Button2({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        onTap: () {/* action */},
        child: ClipRRect(
            borderRadius: BorderRadius.circular(100.0),
            child: Container(
                height: 200,
                width: 90,
                decoration: const BoxDecoration(
                  gradient: LinearGradient(
                      begin: FractionalOffset.topCenter,
                      end: FractionalOffset.bottomCenter,
                      colors: [Colors.tealAccent, Colors.lightBlueAccent],
                      stops: [0.1, 0.9]),
                ),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: const [
                    Icon(Icons.psychology, size: 40),
                    Text("考えるのをやめた", style: TextStyle(fontSize: 20)),
                  ],
                ))));
  }
}

ElevatedButtonを使わないで実現する方法しかわからなかったよ。。。

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?