Flutter Tutorials

Flutter app development tutorials


flutter – TextButton border radius only

Flutter – TextButton border radius for only specific corners

The TextButton class represents a material design text button. The flutter developers can use text buttons on toolbars, in dialogs, or in line with other content. Text buttons do not have visible borders. The flutter developers should avoid using text buttons where they would blend in with other content such as in the middle of lists. The TextButton style can be overridden with its style parameter.

The following flutter application development tutorial will demonstrate how we can add only specified corners rounded-border to the TextButton. That means we will apply border radius for only specified corners, not all corners. In the below example code, we will use the TextButton class’s styleFrom() method to add specified corners rounded border around the TextButton widget.

The TextButton class’s styleFrom() method is a static convenience method that constructs a text button ButtonStyle given simple values. By default, TextButton class styleFrom() method returns a ButtonStyle that doesn’t override anything.

The ButtonStyle class represents the visual properties that most buttons have in common. The ButtonStyle class’s all properties are null by default.

The flutter developers can add a rounded border to the TextButton by passing a value to the styleFrom() method’s shape parameter.

The styleFrom() method’s shape parameter value is an OutlinedBorder instance which adds a border to the TextButton.

The OutlinedBorder class represents a ShapeBorder that draws an outline with the width and color specified by the side. Here we will pass a RoundedRectangleBorder instance for the shape parameter value.

The RoundedRectangleBorder class represents a rectangular border with rounded corners. The RoundedRectangleBorder class borderRadius property value is a BorderRadius object instance that specifies the radii for each corner. By using this parameter value we make the TextButton border’s specified corners rounded.

The BorderRadius class is an immutable set of radii for each corner of a rectangle. The BorderRadius.only({Radius topLeft = Radius.zero, Radius topRight = Radius.zero, Radius bottomLeft = Radius.zero, Radius bottomRight = Radius.zero}) constructor creates a border radius with only the given non-zero values. The other corners will be right angles. So using this BorderRadius constructor the flutter developers can add a rounded corner border to the TextButton where its specified corners are only rounded.

The RoundedRectangleBorder class side property value is a BorderSide instance. The BorderSide class represents a side of a border of a box. The BorderSide class color property value is a Color that is the color of this side of the border. The BorderSide class width property value is a double instance that is the width of this side of the border, in logical pixels.

main.dart


import 'package:flutter/material.dart';

void main(){
  runApp(
    MaterialApp(
      theme: ThemeData(primarySwatch: Colors.blueGrey),
      home: Scaffold(
          appBar: AppBar(
              title: const Text("TextButton Border Radius Only")
          ),
          body: Center(
            child: TextButton(
                onPressed: (){},
                child: const Text("Click Me"),
                style: TextButton.styleFrom(
                    shape: const RoundedRectangleBorder(
                        borderRadius: BorderRadius.only(
                            topLeft: Radius.circular(24),
                            bottomRight: Radius.circular(24)
                        ),
                        side: BorderSide(
                            color: Colors.blueGrey, width: 1
                        )
                    ),
                    padding: const EdgeInsets.symmetric(
                        horizontal: 64, vertical: 24
                    )
                )
            ),
          )
      ),
    ),
  );
}

More flutter tutorials



About Me

Flutter Developer

Design a site like this with WordPress.com
Get started