【问题标题】:how to create multi colored button in flutter如何在颤动中创建多色按钮
【发布时间】:2020-10-31 12:26:03
【问题描述】:

我想在按钮下方创建一个按钮

我怎样才能创建这种类型的按钮,基本上我想创建一个按钮,该按钮说明颜色就像 colors.red[100] 并且按钮终点就像起点和终点之间的 color.red[900] 我的按钮就像长大的颜色。

例如(起始颜色为红色[100] 和红色[101],然后是红色[102] 等等),例如在单个按钮中创建从浅到深的颜色

【问题讨论】:

    标签: flutter user-interface frontend


    【解决方案1】:

    使用gradient 和boxShadow

    Center(
            child: Container(
              margin: EdgeInsets.symmetric(horizontal: 30.0),
              alignment: Alignment.center,
              width: double.infinity,
              height: 50.0,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(8.0)),
                gradient: LinearGradient(
                  colors: [
                    Colors.red[100],
                    Colors.red[900],
                  ]
                ),
                boxShadow: [
                  BoxShadow(
                    offset: Offset(0, 0),
                    color: Colors.red[100],
                    blurRadius: 16.0,
                  ),
                  BoxShadow(
                    offset: Offset(0, 0),
                    color: Colors.red[200],
                    blurRadius: 16.0,
                  ),
                  BoxShadow(
                    offset: Offset(0, 0),
                    color: Colors.red[300],
                    blurRadius: 16.0,
                  ),
                ]
              ),
              child: Text("Login", style: TextStyle(color: Colors.white, fontWeight: FontWeight.w900, fontSize: 18.0))
            ),
          )
    

    【讨论】:

      【解决方案2】:

      您可以在Container 小部件中使用gradient

      这里是完整的代码:

       import 'package:flutter/material.dart';
      
       class ClassName extends StatelessWidget {
         @override
         Widget build(BuildContext context) {
         return Scaffold(
           backgroundColor: Colors.grey[200],
        
           //You can add Appbar like this
           appBar: AppBar(
             backgroundColor: Colors.white,
             centerTitle: true,
             title: Text('This is appbar', style: TextStyle(color: Colors.black, fontSize: 18.0),),
           ),
      
           body: SingleChildScrollView(
             child: Padding(
               padding: EdgeInsets.all(10.0),
               child: Column(
                 children: [
      
                   //You can add other Widgets here
      
                   Container(
                     child: GestureDetector(
                       onTap: (){
                         //add functions of this button here
                       },
                       child: Container(
                         height: 50.0,
                         decoration: BoxDecoration(
                           gradient: LinearGradient(
                             begin: Alignment.centerLeft,
                             end: Alignment.centerRight,
                             colors: [Colors.red[100], Colors.red[900]],
                           ),
                           borderRadius: BorderRadius.all(Radius.circular(10.0)),
                         ),
                         child: Center(
                           child: Text('Login', style: TextStyle(fontWeight: FontWeight.bold, color: Colors.white, fontSize: 18.0),),
                         ),
                       ),
                     )
                   ),
      
                   //You can add other Widgets here
                 
                 ],
               ),
             ),
           ),
         );
       }
      

      截图如下:

      我想这就是你想要的。

      如果需要任何其他帮助,请告诉我。

      【讨论】:

      • onTap 函数仅在点击Login text 而不是Container 本身时触发。您可以通过将整个 Container 包装在任何手势检测小部件 @Hardik Ku​​mar 中来进行改进
      • 谢谢。我没看到。
      • 现在检查代码。我还添加了带有屏幕截图的完整代码。如果你想添加阴影,告诉我,我会做的。如果需要任何其他帮助,请告诉我。
      【解决方案3】:

      要使您的Button 具有多种Colors,您可以使用Container 小部件并根据您的喜好自定义gradient 属性。

      我在下面添加了一个演示代码来帮助您满足您的要求:

                 // use a container widget
                  Container(
                    // customize the height property
                    height: 50,
                    // customize the width property
                    width: MediaQuery.of(context).size.width,
                    decoration: BoxDecoration(
                      // spice up the button with a radius
                      borderRadius: BorderRadius.all(
                        Radius.circular(10),
                      ),
                      gradient: LinearGradient(
                          // gradient starts from left
                          begin: Alignment.centerLeft,
                          // gradient ends at right
                          end: Alignment.centerRight,
                          // set all your colors
                          colors: [
                            Colors.red[100],
                            Colors.red[200],
                            Colors.red[300],
                            Colors.red[400],
                            Colors.red[500],
                            Colors.red[600],
                            Colors.red[700],
                            Colors.red[800],
                            Colors.red[900],
                          ]),
                    ),
                    // the button text
                    child: Center(
                      child: Text(
                        'Login',
                        style: TextStyle(
                          fontWeight: FontWeight.w600,
                          color: Colors.white,
                        ),
                      ),
                    ),
                  ),
      

      【讨论】:

      • 您只需要在gradient 中添加结束颜色,它也会这样做。您只是增加了代码的大小,没有别的。
      • 添加两组颜色将产生不同的输出@HardikKumar。试试代码,我提供了链接:codepen.io/timilehinjegede/pen/WNryOVr
      • 如果你使用stops属性LinearGradient,那么只有它会有所不同。我试过了,这就是我说的原因。
      • 这偏离了原始问题,问题询问如何将渐变从 Light([100]) 颜色的阴影设置为最暗的阴影@HardikKumar
      • 我建议你试试我的代码,你就会明白我的意思。
      猜你喜欢
      • 1970-01-01
      • 2021-08-18
      • 2023-03-29
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多