Flutter Material Widget Outlined Button Class (original) (raw)

Last Updated : 3 Aug, 2022

Outlined Widgets are material design components that are used to give outlines to buttons. It is in no way different from text buttons except for the special feature of the border this class provides. These contain nonprimary actions for the apps. It is introduced in version 1.22 of flutter. Outlined buttons have child as their label with text widgets or icons widgets as the child widget to this parent class. You can set the styling of outlined buttons using ButtonStyle. To use this class you need to import the material package i.e. "package/flutter/material.dart".

Constructor

const OutlinedButton({ Key key, @required VoidCallback onPressed, VoidCallback onLongPress, ButtonStyle style, FocusNode focusNode, bool autofocus = false, Clip clipBehavior = Clip.none, @required Widget child, })

Parameters

1. child: This represents the button's label.

OutlinedButton( child: Text('Raised Button'),

  ),

2. onPressed: This represents the action to be executed when the button is tapped

onPressed: () => Navigator.of(context) .push(MaterialPageRoute(builder: (context) => const NewScreen())),

Properties

Methods

Styling a Button

Button is styled by giving OutlinedButton.styleFrom constructor to the style parameter.

OutlinedButton( child: Text('Outlined Button'), style: OutlinedButton.styleFrom( primary: Colors.green, ), onPressed: () => Navigator.of(context) .push(MaterialPageRoute(builder: (context) => const NewScreen())), ),

Styling a Button

Adding Colors to the Button

The coloring requires two parameters,

1. backgroundcolor

OutlinedButton( child: Text('Outlined Button'), style: OutlinedButton.styleFrom( backgroundColor: Colors.green,

      ),
      onPressed: () => Navigator.of(context)
          .push(MaterialPageRoute(builder: (context) => const NewScreen())),
    ),

2. primary

OutlinedButton( child: Text('Outlined Button'), style: OutlinedButton.styleFrom( backgroundColor: Colors.green, primary: Colors.white, ), onPressed: () => Navigator.of(context) .push(MaterialPageRoute(builder: (context) => const NewScreen())), ),

Adding Colors to the Button

Shaping the Button

The shape of the border can be adjusted by the use of OutlinedBorder constructor as a parameter to the style with the border radius of your choice.

OutlinedButton( child: Text('Outlined Button'), style: OutlinedButton.styleFrom(

          primary: Colors.black,
          textStyle: TextStyle(fontSize: 15, fontStyle: FontStyle.italic),
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.all(Radius.circular(10)))),
      onPressed: () => Navigator.of(context)
          .push(MaterialPageRoute(builder: (context) => const NewScreen())),
    ),

Shaping the Button

Adjusting Text and its Styling

This can be done by passing textstyle to the TextStyle constructor of the outlined button

OutlinedButton( child: Text('Outlined Button'), style: OutlinedButton.styleFrom( backgroundColor: Colors.green, primary: Colors.white, textStyle: TextStyle(fontSize: 15, fontStyle: FontStyle.italic), ), onPressed: () => Navigator.of(context) .push(MaterialPageRoute(builder: (context) => const NewScreen())), ),

Adjusting Text and its Styling

Let's understand outlined button and its properties with the help of an example

Implementation

Dart `

import 'package:flutter/material.dart';

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

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

@override State createState() => _HomeAppState(); }

class _HomeAppState extends State { @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, home: Scaffold( appBar: AppBar( backgroundColor: Colors.green, title: const Text('GeeksforGeeks'), ), body: const FirstScreen())); } }

class FirstScreen extends StatelessWidget { const FirstScreen({Key? key}) : super(key: key);

@override Widget build(BuildContext context) { return Container( child: Center( child: OutlinedButton( child: Text('Outlined Button'), style: OutlinedButton.styleFrom( primary: Colors.black, textStyle: TextStyle(fontSize: 15, fontStyle: FontStyle.italic), shape: RoundedRectangleBorder( borderRadius: BorderRadius.all(Radius.circular(10)))), onPressed: () => Navigator.of(context) .push(MaterialPageRoute(builder: (context) => const NewScreen())), ), ), ); } }

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

@override State createState() => _NewScreenState(); }

class _NewScreenState extends State { TextEditingController textEditingController = TextEditingController();

@override @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( backgroundColor: Colors.green, title: const Text('New Screen'), ), body: Center(child: Text('This is your new screen')), ); } }

`

Output