How To Add Background Gradient With Appbar In Flutter

admin_img Posted By Bajarangi soft , Posted On 23-09-2020

I want to make the background of the AppBar a gradient, How can i do it.

How to add background gradient with aapbar in flutter app

AppBar does not have that feature by default. But you can make an AppBar like widget which will have a gradient background as follow:

Step-1:
You can install gradient_app_bar

 

dependencies:
  flutter:
    sdk: flutter
  gradient_app_bar: ^0.1.3


Step-2:
You can install packages from the command line: with Flutter:

flutter pub get


step-3:
Now in your Dart code, you can use:
import 'package:gradient_app_bar/gradient_app_bar.dart';


You can add this gradiant_app_bar code :
GradientAppBar(
  title: Text('GradientAppBar'),
  leading: Icon(Icons.menu),
  centerTitle: true,
  gradient: LinearGradient(colors: [Colors.orangeAccent, Colors.purple]),
  bottom: TabBar(tabs: <Widget>[
    Tab(
      icon: Icon(Icons.person),
      text: 'Person',
    ),
    Tab(
      icon: Icon(Icons.store),
      text: 'Store',
    ),
  ]),
),

Complete Code  gradient_app_bar in flutter
Main.dart
import 'package:flutter/material.dart';
import 'package:gradient_app_bar/gradient_app_bar.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'App Design',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 2,
      child: Scaffold(
        appBar: GradientAppBar(
          title: Text('GradientAppBar'),
          leading: Icon(Icons.menu),
          centerTitle: true,
          gradient: LinearGradient(colors: [Colors.orangeAccent, Colors.purple]),
          bottom: TabBar(tabs: <Widget>[
            Tab(
              icon: Icon(Icons.person),
              text: 'Person',
            ),
            Tab(
              icon: Icon(Icons.store),
              text: 'Store',
            ),
          ]),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                'You have pushed the button this many times:',
              ),
              Text(
                '$_counter',
                style: Theme.of(context).textTheme.display1,
              ),
            ],
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: _incrementCounter,
          tooltip: 'Increment',
          child: Icon(Icons.add),
          backgroundColor: Colors.red,
        ),
      ),
    );
  }
}

Related Post