nayo
nayo

Reputation: 301

is it possible to add gradient color using one name flutter

I want to define below gradient color in one name to use multiple places in my code.is it posssible in flutter

like this >> const Color backgroundBlue = Color(0xff0F1A2E);

   gradient: LinearGradient(
            colors: [
              Color.fromRGBO(125, 158, 205, 1.0),
              Color.fromRGBO(115, 131, 163, 0.7490196078431373),
            ],
            begin: Alignment.centerLeft,
            end: Alignment.centerRight,
          ),

Upvotes: 2

Views: 1398

Answers (1)

Miguel Vieira Colombo
Miguel Vieira Colombo

Reputation: 89

if the focus is to use only one file, you can define at the beginning of the file what to make the calls, but if you intend to use it in multiple files you can create a new file and inside it create a constant with LinearGradient and import where you need

import 'package:flutter/cupertino.dart';

//colors.dart
const myGradient = LinearGradient(
  colors: [
    Color.fromRGBO(125, 158, 205, 1.0),
    Color.fromRGBO(115, 131, 163, 0.7490196078431373),
  ],
  begin: Alignment.centerLeft,
  end: Alignment.centerRight,
);


import 'colors.dart';

// page1.dart
class Page1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(gradient: myGradient),
    );
  }
}

Upvotes: 2

Related Questions