如何为flutter中的附件创建浮动按钮

时间:2019-01-28 04:42:20

标签: flutter flutter-layout

如何在Flutter中创建一个浮动按钮并为其按下的按钮设置不同的动画效果?我正在尝试进行以下操作:

当我尝试添加多个浮动按钮时,出现“ argument for the name floating button action has already been specified”错误

    class MyBills extends StatelessWidget {
  MyBills({Key key}) : super(key: key);

  //@override
  //MyBillsPageState createState() => MyBillsPageState();
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: ListView.builder(
        itemCount: 20,
        itemBuilder: (context, index) {
          return new Container(height: 100, child: BillItem());
        }
      ),
      floatingActionButton: new FloatingActionButton(
        child: new Icon(Icons.add),
        heroTag: 1,
        onPressed: _addAttachment
      ),
      floatingActionButton: new FloatingActionButton(
        child: new Icon(Icons.camera),
        heroTag: 2,
        onPressed: _cameraAttachment
      ),
    );
  }

enter image description here 或设置浮动选项like this

的动画

1 个答案:

答案 0 :(得分:1)

给每个FloatingActionButton一个唯一的heroTag标签,您应该会很高兴。

floatingActionButton: FloatingActionButton(
  heroTag: 0, // you can use any object here
),

您的解决方案:

@override
Widget build(BuildContext context) {
  return new Scaffold(
    body: Stack(
      alignment: Alignment.bottomRight,
      children: <Widget>[
        ListView.builder(
          itemCount: 20,
            itemBuilder: (context, index) {
              return new Container(height: 100, child: BillItem());
            }
        ),
        Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            new FloatingActionButton(child: new Icon(Icons.add), heroTag: 1, onPressed: _addAttachment),
            new FloatingActionButton(child: new Icon(Icons.camera), heroTag: 2, onPressed: _cameraAttachment),
          ],
        ),
      ],
    ),
  );
}