flutter - 重叠两个按钮 Flutter
问题描述
我是新来的。我创建了两个凸起的按钮。两者都被创建为小部件,然后在主 Scaffold 小部件中被调用。我希望它们相互重叠。我正在尝试堆栈小部件,但随后缺少一个按钮。我也尝试过填充和定位,但没有任何效果。我不想使用切换按钮,因为它们只是彼此相邻放置并且没有重叠。
代码
Widget business(context, setState) {
return
Padding(
padding: const EdgeInsets.only(left: 12.0),
child: ClipRRect(
borderRadius: BorderRadius.circular(25.0),
child: SizedBox(
height: 60,
width: 60,
child: RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(25.0),
side: BorderSide(color: button2 ? primaryColor : Colors.white)
),
color: button1 ? primaryColor : Colors.white,
onPressed: () {
setState(() {
button1 = true;
button2 = false;
});
Fluttertoast.showToast(
msg:
"Business",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 4,
backgroundColor: primaryColor,
textColor: Colors.white,
fontSize: 16.0,
);
},
child: Icon(
MyFlutterApp.office,
color: button1 ? Colors.white : Colors.black,
),
),
),
),
);
}
else {
return Container();
}
Widget personal(context, setState) {
return
Padding(
padding: const EdgeInsets.only(left: 12.0),
child: ClipRRect(
borderRadius: BorderRadius.circular(25.0),
child: SizedBox(
height: 60,
width: 60,
child: RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(25.0),
side: BorderSide(color: button1 ? primaryColor : Colors.white)
),
color: button2 ? primaryColor : Colors.white,
onPressed: () {
setState(() {
button1 = false;
button2 = true;
});
Fluttertoast.showToast(
msg:
"Personal",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 4,
backgroundColor: primaryColor,
textColor: Colors.white,
fontSize: 16.0,
);
},
child: Icon(
MyFlutterApp.personal,
color: button2 ? Colors.white : Colors.black,
),
),
),
),
);
}
else {
return Container();
}
解决方案
Based on Flutter Oficial Documentation, you can use the Stack widget to achieve this effect. Here is a quick tutorial about this widget.
Using RaisedButton, always remember to remove the elevation from the upper buttons, to not cause a black overlay impression.
Here is a example:
Stack(
children: <Widget>[
RaisedButton(
onPressed: () => {},
color: Colors.red,
child: Text('First'),
),
RaisedButton(
onPressed: () => {},
color: Colors.transparent,
elevation: 0,
child: Text('Second'),
),
],
)
Here is the result.
推荐阅读
- android - 如何解析json数据Android
- ios - 如何在 iOS 设备上使用 Flutter 访问传感器信息
- swiftui - 详细列表视图 - 顶部对齐问题
- python - 获取最新的免费数据库索引?
- php - 如何避免卡住的进程?
- javascript - 为什么这适用于箭头函数,而不是常规函数?
- php - 设置 Web 驱动程序(带 php 的 selenium)时收到错误消息
- google-photos-api - 如何知道 Google 相册是否已更改
- javascript - 在 Vue JS 项目中不能包含没有 npm 的 JQuery
- mule - 将 mule 集成应用程序从 mule runtime 1.4 升级到 2.0 和 java 8 到 java 11 的最佳方法是什么?