0

How do create a floating button and animate different options on its button press in Flutter? I am trying to make something like below:

When I try to add multiple floating buttons, I get 'argument for the name floating button action has already been specified' error

    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 or animate floating options like this

JJJ
  • 32,902
  • 20
  • 89
  • 102
Deepak Thakur
  • 3,453
  • 2
  • 37
  • 65
  • 1
    Is a [fab_dialer](https://pub.dartlang.org/packages/flutter_fab_dialer) what you're after? Here's also [a version](https://stackoverflow.com/questions/46480221/flutter-floating-action-button-with-speed-dial) which doesn't use a package. – soupjake Jan 28 '19 at 09:10
  • yes.. got it .. ! – Deepak Thakur Jan 28 '19 at 09:14

1 Answers1

3

Give every FloatingActionButton a unique heroTag tag, and you should be good to go.

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

Your solution:

@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),
          ],
        ),
      ],
    ),
  );
}
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440