Open In App

Flutter Material Widget – Outlined Button Class

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())),
        ),

 

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())),
        ),

 

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())),
        ),

 

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())),
        ),

 

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

Implementation




import 'package:flutter/material.dart';
 
void main() {
  runApp(HomeApp());
}
 
class HomeApp extends StatefulWidget {
  HomeApp({Key? key}) : super(key: key);
 
  @override
  State<HomeApp> createState() => _HomeAppState();
}
 
class _HomeAppState extends State<HomeApp> {
  @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<NewScreen> createState() => _NewScreenState();
}
 
class _NewScreenState extends State<NewScreen> {
  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


Article Tags :