flutterfloating-action-button

Change the shape of Floating action button in Flutter


I am trying to create a Floating action button which uses both with and without extended.

My requirement is when the page is scrolled floating action button should change from extended to with out extended. The issue i am facing is that the shape is not changing into circle. Below i have attached the code and screenshot.

FloatingActionButton.extended(
          label: AnimatedSwitcher(
            duration: Duration(milliseconds: 500),
            transitionBuilder: (Widget child, Animation<double> animation) => FadeTransition(
              opacity: animation.drive(CurveTween(curve: Curves.easeIn)),
              child: SizeTransition(
                child: child,
                sizeFactor: animation,
                axis: Axis.horizontal,
              ),
            ),
            child: isExtended
                ? SvgPicture.asset(
                  floatIcon,
                )
                : Row(
                    children: [
                      SvgPicture.asset(
                        floatIcon,
                      ),
                      const Text(
                        ' New',
                        style: TextStyle(fontFamily: 'Gilroy Medium', fontSize: 20, color: Color(0xffB0B0B0)),
                      ),
                    ],
                  ),
          ),
          backgroundColor: Colors.black,
          elevation: 0,
          onPressed: () {},
        ),

screenshot:

Need to change the shape to circle

Can anyone please help me on this issue.


Solution

  • Try below code,or you can used flutter_scrolling_fab_animated package

    Declare one Boolean variable

    bool isFABExtended = false; 
    

    Create function for button action change:

      void _switchButton() {
        setState(
          () {
            isFABExtended = !isFABExtended;
          },
        );
      }
    

    Declare your Widget:

     floatingActionButton: isFABExtended
          ? FloatingActionButton(
              onPressed: _switchButton,
              child: Icon(Icons.check),
            )
          : FloatingActionButton.extended(
              onPressed: _switchButton,
              label: Row(
                children: [
                  Padding(
                    padding: const EdgeInsets.only(right: 4.0),
                    child: Icon(Icons.add),
                  ),
                  Text("Add Button")
                ],
              ),
            ),
    

    Extended FloatingActionButton Result Screen -> image

    Circular FloatingActionButton result-> image