How to add a border to a widget in Flutter?

Do you want to add a border to a widget like a container, card e.t.c in flutter? If yes then you can add it using the BoxDecoration(). Inside the BoxDecoration() you have to use the border attribute.

Here you will learn how to add a border to container and text using the BoxDecoration().

Step 1: Create a Container Widget

The first step is to create a Container that allows you to add the BoxDecoration() in it. Add the below lines of code.

Center(
          child: Container(
            width: 100,
            height: 100,
            decoration: BoxDecoration(color: Colors.amber),
          ),
        ),

Output

Container at the center
Container at the center

Step 2: Add some text

Add some text inside the container.

Center(
          child: Container(
            alignment: Alignment.center,
            width: 100,
            height: 100,
            child: Text(
              "Okay",
              style: TextStyle(color: Colors.white, fontSize: 20),
            ),
            decoration: BoxDecoration(color: Colors.amber),
          ),
        ),
Text at the center of the Container
Text at the center of the Container

Step 3: Add border

Here I will add a border for the Container using the border attribute.

Center(
          child: Container(
            alignment: Alignment.center,
            width: 100,
            height: 100,
            child: Text(
              "Okay",
              style: TextStyle(color: Colors.white, fontSize: 20),
            ),
            decoration: BoxDecoration(
                color: Colors.amber,
                border: Border.all(color: Colors.red, width: 3)),
          ),
        ),
Border outside the container
Border outside the container

Full Code

import 'package:flutter/material.dart';

class MyApp extends StatefulWidget {
  // This widget is the root of your application.
  const MyApp({Key? key}) : super(key: key);

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

class _MyAppState extends State {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // Application name
      title: 'My Flutter App',
      debugShowCheckedModeBanner: false, // Remove debug banner
      home: Scaffold(
        appBar: AppBar(
          // The title text which will be shown on the action bar
          title: Text("My Flutter App"),
        ),
        body: Center(
          child: Container(
              alignment: Alignment.center,
              width: 100,
              height: 100,
              child: Text(
                "Okay",
                style: TextStyle(color: Colors.white, fontSize: 20),
              ),
              decoration: BoxDecoration(
                  color: Colors.amber,
                  border: Border.all(color: Colors.red, width: 3))),
        ),
      ),
    );
  }
}

void main() {
  runApp(MyApp());
}

Output

Border outside the container
Border outside the container widget

In the same way, you can add a border for any widget. But make sure you can use the border inside the Container() widget only. So always wrap other widget with the Container() widget.

Container(
  child: AnyOtherWidget()
)

Source:
Flutter Container

Hi, I am CodeTheBest. Here you will learn the best coding tutorials on the latest technologies like a flutter, react js, python, Julia, and many more in a single place.

SPECIAL OFFER!

This Offer is Limited! Grab your Discount!
15000 ChatGPT Prompts
Offer Expires In:
close-link