使用堆栈的自定义按钮

问题描述 投票:0回答:2

如何使用堆栈小部件和没有背景图像的弯曲设计创建按钮。

flutter widget containers flutter-animation
2个回答
0
投票

希望有帮助:)

import 'package:flutter/material.dart';

void main() {
  runApp(
    const MaterialApp(
      home: MyApp(),
    ),
  );
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Container(
          width: 200,
          height: 50,
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(20),
            image: const DecorationImage(
              image: AssetImage(
                'assets/images/button_background.png',
              ),
              fit: BoxFit.cover,
            ),
          ),
          child: RawMaterialButton(
            shape: const RoundedRectangleBorder(
              borderRadius: BorderRadius.all(
                Radius.circular(20),
              ),
            ),
            splashColor: Colors.green,
            onPressed: () {
              print('Hello World');
            },
            child: Container(
              padding: const EdgeInsets.symmetric(horizontal: 20),
              alignment: Alignment.centerLeft,
              child: const Text('Login', style: TextStyle(color: Colors.white)),
            ),
          ),
        ),
      ),
    );
  }
}


0
投票

我建议使用背景图片,因为它更简单,但如果你真的坚持不使用它们,这里有一个没有背景图片的例子:

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: ClipRRect(
          borderRadius: const BorderRadius.all(Radius.circular(20)),
          child: Container(
            height: 60,
            width: 250,
            decoration: const BoxDecoration(
              gradient: LinearGradient(
                colors: [Colors.green, Colors.yellow],
                begin: Alignment.centerLeft,
                end: Alignment.centerRight,
              ),
            ),
            child: RawMaterialButton(
              onPressed: () {},
              splashColor: Colors.green,
              child: Stack(
                children: [
                  Container(
                    alignment: Alignment.centerLeft,
                    padding: const EdgeInsets.only(left: 20),
                    child: const Text(
                      'Login',
                      style: TextStyle(color: Colors.white, fontSize: 18),
                    ),
                  ),
                  Positioned(
                    right: 5,
                    top: -80,
                    height: 100,
                    width: 100,
                    child: Container(
                      decoration: const ShapeDecoration(
                        color: Colors.green,
                        shape: CircleBorder(),
                      ),
                    ),
                  ),
                  Positioned(
                    right: -40,
                    top: -65,
                    height: 100,
                    width: 100,
                    child: Container(
                      decoration: ShapeDecoration(
                        color: Colors.brown.withOpacity(.9),
                        shape: const CircleBorder(),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}

附言随意调整

color
s 和
height/width
值以满足您的需要。

© www.soinside.com 2019 - 2024. All rights reserved.