【发布时间】:2020-10-31 12:26:03
【问题描述】:
我想在按钮下方创建一个按钮
我怎样才能创建这种类型的按钮,基本上我想创建一个按钮,该按钮说明颜色就像 colors.red[100] 并且按钮终点就像起点和终点之间的 color.red[900] 我的按钮就像长大的颜色。
例如(起始颜色为红色[100] 和红色[101],然后是红色[102] 等等),例如在单个按钮中创建从浅到深的颜色
【问题讨论】:
标签: flutter user-interface frontend
我想在按钮下方创建一个按钮
我怎样才能创建这种类型的按钮,基本上我想创建一个按钮,该按钮说明颜色就像 colors.red[100] 并且按钮终点就像起点和终点之间的 color.red[900] 我的按钮就像长大的颜色。
例如(起始颜色为红色[100] 和红色[101],然后是红色[102] 等等),例如在单个按钮中创建从浅到深的颜色
【问题讨论】:
标签: flutter user-interface frontend
使用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))
),
)
【讨论】:
您可以在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 Kumar 中来进行改进
要使您的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 中添加结束颜色,它也会这样做。您只是增加了代码的大小,没有别的。
stops属性LinearGradient,那么只有它会有所不同。我试过了,这就是我说的原因。