Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the Flutter app bar with gradient background

This question is being posted here as the related question here on stack overflow has only workarounds but no straight to the point approach.

like image 474
Yadu Avatar asked Apr 21 '20 14:04

Yadu


People also ask

How do you use gradient color to app bar in flutter?

One way to add gradient color to the AppBar is by using its flexibleSpace property. The flexibleSpace property of the AppBar accepts a widget as its value, so we can assign a container to it which itself implements a gradient background color.


2 Answers

this can be achieved with the fallowing code

AppBar(
  flexibleSpace: Container(
   decoration: BoxDecoration(
    gradient: LinearGradient(
      colors: [Colors.cyan, Colors.yellow], stops: [0.5, 1.0],
      ),
    ),
  ),
),

See the Pen bGVBVpz by yadunandan (@iamyadunandan) on CodePen.

like image 186
Yadu Avatar answered Oct 03 '22 12:10

Yadu


For a simple appbar with a gradient background and centered title,

AppBar(
      automaticallyImplyLeading: false, // hides default back button
      flexibleSpace: Container(
          decoration: BoxDecoration(
        gradient: LinearGradient(
            begin: Alignment.topLeft,
            end: Alignment.topRight,
            colors: [
              Color(0xffB1097C),
              Color(0xff0947B1),
            ]),
      )),
      title: Text("WishList", style: TextStyle(fontSize: 20.0, color: Colors.white)),
      centerTitle: true,
    ),

enter image description here

For a more complex appbar with a gradient background and action icons

AppBar(
  automaticallyImplyLeading: false, // hides default back button
  flexibleSpace: Container(
      decoration: BoxDecoration(
    gradient: LinearGradient(
        begin: Alignment.topLeft,
        end: Alignment.topRight,
        colors: [
          Color(0xffB1097C),
          Color(0xff0947B1),
        ]),
  )),
  title: Text("Welcome guest", style: TextStyle(fontSize: 20.0, color: Colors.white)),
  actions: [
    IconButton(
        icon: SvgPicture.asset("enter svg path",height: 20.0, width: 20.0),
        onPressed: () {
          print("Icon 1 pressed");
        }),
    IconButton(
      icon: SvgPicture.asset("enter svg path", height: 20.0, width: 20.0),
      onPressed: () {
        print("Icon 2 pressed");
      },
    )
  ],
)

enter image description here

like image 24
zfnadia Avatar answered Oct 03 '22 12:10

zfnadia