Flutter – Set Gradient to Container Background
In this article, we will learn how to make the Gradient Background color of the body in the Flutter Application. A sample image is given below to get an idea about what we are going to do in this article.
Step By Step Implementation
Step 1: Create a New Project in Android Studio
To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.
Step 2: Import the material package
Adding material package that gives us the essential functions and calls the runApp method in the main function that will call our application.
import 'package:flutter/material.dart'; void main() { runApp(RunMyApp()); }
Step 3: Creating Stateless Widget
Now we have to make a stateless widget because our application does not go to change its state and then return the MaterialApp widget which allows us the set the title and theme and many more.
class RunMyApp extends StatelessWidget { const RunMyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp(); } }
Step 4: Creating Scaffold Widget
Give the home property and there can be a scaffold widget that has the property of AppBar and body. AppBar allows us to give the title of AppBar, color, leading, and trailing icon.
home: Scaffold( appBar: AppBar( title: Text('Gradient Background'), ), body: ),
Step 5: Now we have to make the body background gradient color. For that, we can use a container with some height and width. And then using Box decoration we can make the color gradient.
body: Container( width: double.maxFinite, height: double.maxFinite, decoration: BoxDecoration( gradient: LinearGradient( colors: [Colors.green, Color.fromARGB(255, 29, 221, 163)], ), ), child: Center(child: Text('w3wiki'))),
Final Code:
Dart
import 'package:flutter/material.dart' ; void main() { runApp(RunMyApp()); } class RunMyApp extends StatelessWidget { const RunMyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false , home: Scaffold( appBar: AppBar( title: Text( 'Gradient Background' ), ), body: Container( width: double .maxFinite, height: double .maxFinite, decoration: BoxDecoration( gradient: LinearGradient( colors: [Colors.green, Color.fromARGB(255, 29, 221, 163)], ), ), child: Center(child: Text( 'w3wiki' ))), ), ); } } |