how to implement a gradient borders in flutter ?

Spread the love

how to implement a gradient borders in flutter
how to implement a gradient borders in flutter

how to implement a gradient borders in flutter ?

Introduction:

Implementing gradient borders in Flutter allows for the creation of visually appealing UI elements with colorful borders. The flutter_gradient_border package provides a straightforward way to achieve this effect. This tutorial will guide you through implementing gradient borders in Flutter using the flutter_gradient_border package.

Content:

Step 1: Add the dependency:

Add the flutter_gradient_border package to your pubspec.yaml file:

Save the file and run flutter pub get to install the package.

Step 2: Import the package:

Import the flutter_gradient_border package in your Dart file:


import 'package:flutter/material.dart';
import 'package:flutter_gradient_border/flutter_gradient_border.dart';

Step 3: Create a widget with gradient border:

Create a widget with a gradient border using the GradientBorder widget. Customize the gradient colors, border width, and other properties:


Container(
  width: 200,
  height: 200,
  decoration: BoxDecoration(
    gradient: LinearGradient(
      colors: [Colors.red, Colors.blue],
      begin: Alignment.topLeft,
      end: Alignment.bottomRight,
    ),
    borderRadius: BorderRadius.circular(20),
    border: GradientBorder.all(
      colors: [Colors.purple, Colors.green],
      begin: Alignment.topLeft,
      end: Alignment.bottomRight,
      width: 4,
    ),
  ),
  child: Center(
    child: Text(
      'Gradient Border',
      style: TextStyle(color: Colors.white, fontSize: 20),
    ),
  ),
),

In this example, the Container widget has a gradient border with purple and green colors, a width of 4, and a circular BorderRadius.

Step 4: Run the app:

Run your Flutter app to see the widget with a gradient border in action.

Sample Code:


// ignore_for_file: prefer_const_constructors

import 'package:flutter/material.dart';
import 'package:gradient_borders/gradient_borders.dart';

class GradiantBorder extends StatefulWidget {
  const GradiantBorder({Key? key}) : super(key: key);

  @override
  State<GradiantBorder> createState() => _GradiantBorderState();
}

class _GradiantBorderState extends State<GradiantBorder> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        elevation: 2,
        title: Text(
          "Gradient borders",
          style: TextStyle(color: Colors.black, fontWeight: FontWeight.w400),
        ),
        backgroundColor: Colors.blue[100],
      ),
      // appBar: AppBar(
      //   title: const Text("Gradient borders"),
      // ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
              width: 100,
              height: 100,
              decoration: BoxDecoration(
                  border: const GradientBoxBorder(
                    gradient: LinearGradient(colors: [Colors.blue, Colors.red]),
                    width: 4,
                  ),
                  borderRadius: BorderRadius.circular(16)),
            ),
            const SizedBox(height: 16),
            Container(
              width: 100,
              height: 100,
              decoration: const BoxDecoration(
                border: GradientBoxBorder(
                  gradient: LinearGradient(colors: [Colors.green, Colors.yellow]),
                  width: 4,
                ),
              ),
            ),
            const SizedBox(height: 16),
            Container(
              width: 100,
              height: 100,
              decoration: const BoxDecoration(
                shape: BoxShape.circle,
                border: GradientBoxBorder(
                  gradient: LinearGradient(colors: [Colors.pink, Colors.orange]),
                  width: 4,
                ),
              ),
            ),
            const SizedBox(height: 16),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: const TextField(
                decoration: InputDecoration(
                  contentPadding: EdgeInsets.symmetric(vertical: 12, horizontal: 12),
                  border: GradientOutlineInputBorder(
                    gradient: LinearGradient(colors: [Colors.red, Colors.blue]),
                    width: 2,
                  ),
                  focusedBorder: GradientOutlineInputBorder(gradient: LinearGradient(colors: [Colors.yellow, Colors.green]), width: 2),
                  label: Text("textfild example"),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

Output:

how to implement a gradient borders in flutter
how to implement a gradient borders in flutter

Conclusion:

By following these steps, you can easily implement gradient borders in Flutter using the flutter_gradient_border package. This allows you to create visually appealing UI elements with colorful and customizable borders.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *