【发布时间】:2021-09-11 20:50:29
【问题描述】:
我正在使用此链接中的示例: https://medium.com/@jb.padamchopra/implementing-a-bottom-app-bar-with-floating-action-button-flutter-463560e1324 Widget from the link
浮动动作按钮动画由 bool clickedCenterFAB 控制 在我的代码中,动画容器内的小部件定义在一个类中,我希望在用户编写文本并按下按钮后,动画容器折叠,所以逻辑上我想调用 setState 并分配 clickedCenterFAB = false。 我是 Flutter 的新手,我尝试在 uploadPage 小部件中调用 setState 并尝试更改 clickedCenterFAB 但它没有用。知道怎么做吗?我在这里先向您的帮助表示感谢。 My widget
AnimatedContainer(
duration: Duration(milliseconds: 300),
//if clickedCentreFAB == true, the first parameter is used. If it's false, the second.
height: clickedCentreFAB
? MediaQuery.of(context).size.height
: 10.0,
width: clickedCentreFAB
? MediaQuery.of(context).size.height
: 10.0,
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(clickedCentreFAB ? 0.0 : 300.0),
color: Colors.blue,
),
child: clickedCentreFAB ? UploadPage() : null)
编辑:在遵循迈克尔的解决方案并跟上 cmets 之后,这就是我得到的地方
class UploadPage extends StatefulWidget {
final Function collapseContainer;
UploadPage(this.collapseContainer);
@override
_UploadPageState createState() =>
_UploadPageState(collapseContainer);
}
class _UploadPageState extends State<UploadPage> {
final Function collapseContainer;
_UploadPageState(this.collapseContainer);
...
...
CustomButton(
() {
if (_controller.text.trim().isNotEmpty) {
DatabaseServices().uploadPost(
FirebaseAuth.instance.currentUser.uid,
_controller.text,
!_isChecked);
collapseContainer;
}
},
'Post',
Colors.white,
),
CustomButton 显然是我为避免冗余而创建的一个小部件类,并且在我在这里传递的函数中的参数中
【问题讨论】:
-
使用状态管理库,如
flutter_bloc、provider、getX、mobX。