flutter - How to change TextButton background color

Flutter - TextButton Background Color
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 set a background color for the TextButton. We will change the TextButton widget’s default background color. Here we will use the TextButton class’s styleFrom() method to change the TextButton’s default background color.

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 change a TextButton’s background color by passing a value to the styleFrom() method’s backgroundColor parameter.

So finally, the flutter app developers can set a background color for the TextButton widget by passing a value to the TextButton class’s styleFrom() method backgroundColor argument.
main.dart

import 'package:flutter/material.dart';

void main() => runApp(const FlutterExample());

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

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Example',
theme: ThemeData(primarySwatch: Colors.red),
home: const StateExample()
);
}
}

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

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

class _StateExampleState extends State<StateExample>{
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFFFEFEFA),
appBar: AppBar(
title: const Text("Flutter - TextButton Background Color")
),
body: bodyContent(),
);
}


bodyContent() {
return Center(
child: TextButton(
child: const Text("Click Me"),
style: TextButton.styleFrom(
padding: const EdgeInsets.all(20),
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(12))
),
backgroundColor: const Color(0xFFE3DAC9)
),
onPressed: (){
// do something here
},
)
);
}
}

flutter - How to create OutlinedButton with icon

Flutter - OutlinedButton Icon
The OutlinedButton class represents a material design outlined button that is essentially a TextButton with an outlined border. The outlined buttons are medium-emphasis buttons. In a flutter application, the OutlinedButtons contain actions that are important, but they are not the primary action in an app. An OutlinedButton widget’s default style is defined by defaultStyleOf. The flutter application developers can override the OutlinedButton’s style with its style parameter. An OutlinedButton has a default ButtonStyle.side which defines the appearance of the outline. The flutter app developers can specify an OutlinedButton's shape and the appearance of its outline by specifying both the ButtonStyle.shape and ButtonStyle.side properties.

The following flutter application development tutorial will demonstrate how to create an OutlinedButton widget with an icon. Here we will use the OutlinedButton.icon() constructor to create an OutlinedButton with an icon and text.

The OutlinedButton.icon({Key? key, required VoidCallback? onPressed, VoidCallback? onLongPress, ButtonStyle? style, FocusNode? focusNode, bool? autofocus, Clip? clipBehavior, MaterialStatesController? statesController, required Widget icon, required Widget label}) constructor creates a text button from a pair of widgets that serve as the button's icon and label.

The icon and label are arranged in a row inside the OutlinedButton widget. The icon and label arguments must not be null. So the flutter developers must have to pass the icon and label parameters value to this specified OutlinedButton constructor.

The icon parameter value is an Icon widget. The Icon class represents a graphical icon widget drawn with a glyph from a font described in an IconData such as material's predefined IconDatas in Icons.

So finally, the flutter application developers can create an OutlinedButton with an icon and text by using the OutlinedButton’s specified constructor. They have to pass the icon widget to this constructor to display it on the OutlinedButton. And also have to pass a value for the label parameter to display the text on OutlinedButton.
main.dart

import 'package:flutter/material.dart';

void main() => runApp(const FlutterExample());

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

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Example',
theme: ThemeData(primarySwatch: Colors.green),
home: const StateExample()
);
}
}

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

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

class _StateExampleState extends State<StateExample>{
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFFFEFEFA),
appBar: AppBar(
title: const Text("Flutter - OutlinedButton Icon")
),
body: bodyContent(),
);
}


bodyContent() {
return Center(
child: OutlinedButton.icon(
icon: const Icon(
Icons.favorite,
color: Colors.pink,
),
label: const Text("Like Us"),
style: OutlinedButton.styleFrom(
padding: const EdgeInsets.all(20),
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(8))
)
),
onPressed: (){
// do something here
},
)
);
}
}

flutter - How to change OutlinedButton border radius

Flutter - OutlinedButton Border Radius
The OutlinedButton class represents a material design outlined button that is essentially a TextButton with an outlined border. The outlined buttons are medium-emphasis buttons. In a flutter application, the OutlinedButtons contain actions that are important, but they are not the primary action in an app. An OutlinedButton widget’s default style is defined by defaultStyleOf. The flutter application developers can override the OutlinedButton’s style with its style parameter. An OutlinedButton has a default ButtonStyle.side which defines the appearance of the outline. The flutter app developers can specify an OutlinedButton's shape and the appearance of its outline by specifying both the ButtonStyle.shape and ButtonStyle.side properties.

The following flutter application development tutorial will demonstrate how to set or change the OutlinedButton widget’s border-radius. That means how we can make rounded corners OutlinedButton. Here we will use the OutlinedButton class’s styleFrom() method to make the OutlinedButton corners rounded.

The OutlinedButton class’s styleFrom() method is a static convenience method that constructs an outlined button ButtonStyle given simple values. By default, OutlinedButton 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 OutlinedButton by passing a value to the styleFrom() method’s shape parameter. It also changes the OutlinedButton shape.

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

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 specifies the radii for each corner. Using this parameter value we make the OutlinedButton corners rounded.

The BorderRadius class represents an immutable set of radii for each corner of a rectangle. The BorderRadius.all(Radius radius) constructor creates a border radius where all radii are radius. So using this constructor we can make a rounded corners OutlinedButton in which all corner radii are equal.

So finally, the flutter app developers can change an OutlinedButton’s border-radius by passing a RoundedRectangleBorder instance to the OutlinedButton class’s styleFrom() method’s shape parameter.
main.dart

import 'package:flutter/material.dart';

void main() => runApp(const FlutterExample());

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

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Example',
theme: ThemeData(primarySwatch: Colors.green),
home: const StateExample()
);
}
}

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

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

class _StateExampleState extends State<StateExample>{
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFFFEFEFA),
appBar: AppBar(
title: const Text("Flutter - OutlinedButton Border Radius")
),
body: bodyContent(),
);
}


bodyContent() {
return Center(
child: OutlinedButton(
child: const Text("I am a Button"),
style: OutlinedButton.styleFrom(
padding: const EdgeInsets.all(20),
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20))
)
),
onPressed: (){
// do something here
},
)
);
}
}

flutter - How to change OutlinedButton border color

Flutter - OutlinedButton Border Color
The OutlinedButton class represents a material design outlined button that is essentially a TextButton with an outlined border. The outlined buttons are medium-emphasis buttons. In a flutter application, the OutlinedButtons contain actions that are important, but they are not the primary action in an app. An OutlinedButton widget’s default style is defined by defaultStyleOf. The flutter application developers can override the OutlinedButton’s style with its style parameter. An OutlinedButton has a default ButtonStyle.side which defines the appearance of the outline. The flutter app developers can specify an OutlinedButton's shape and the appearance of its outline by specifying both the ButtonStyle.shape and ButtonStyle.side properties.

The following flutter application development tutorial will demonstrate how to set or change the OutlinedButton widget’s border color. Here we will use the OutlinedButton class’s styleFrom() method to change the OutlinedButton’s border color. Here we also add padding to the OutlinedButton widget.

The OutlinedButton class’s styleFrom() method is a static convenience method that constructs an outlined button ButtonStyle given simple values. By default, OutlinedButton 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 change the OutlinedButton’s border color by passing a value to the styleFrom() method’s side parameter.

The styleFrom() method’s side parameter 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. We can pass a value to this color property to change an OutlinedButton’s border color.

So finally, the flutter app developers can change an OutlinedButton widget’s border color by calling its styleFrom() static method. They have to set a BorderSide instance to its side parameter value. They also have to set the color property value for the BorderSide instance which color they want to use for drawing the OutlinedButton border color.
main.dart

import 'package:flutter/material.dart';

void main() => runApp(const FlutterExample());

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

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Example',
theme: ThemeData(primarySwatch: Colors.green),
home: const StateExample()
);
}
}

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

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

class _StateExampleState extends State<StateExample>{
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFFFEFEFA),
appBar: AppBar(
title: const Text("Flutter - OutlinedButton Border Color")
),
body: bodyContent(),
);
}


bodyContent() {
return Center(
child: OutlinedButton(
child: const Text("Click Here"),
style: OutlinedButton.styleFrom(
side: const BorderSide(color: Colors.pink),
padding: const EdgeInsets.all(20)
),
onPressed: (){
// do something here
},
),
);
}
}

flutter - How to use OutlinedButton

Flutter OutlinedButton
The OutlinedButton class represents a material design Outlined Button that is essentially a TextButton with an outlined border.

The Outlined buttons are medium-emphasis buttons. In a flutter application, the OutlinedButtons contain actions that are important, but they are not the primary action in an app.

An OutlinedButton widget’s default style is defined by defaultStyleOf. The flutter application developers can override the OutlinedButton’s style with its style parameter.

An OutlinedButton has a default ButtonStyle.side which defines the appearance of the outline. The flutter app developers can specify an OutlinedButton's shape and the appearance of its outline by specifying both the ButtonStyle.shape and ButtonStyle.side properties.

The OutlinedButton() constructor creates an OutlinedButton. And the OutlinedButton.icon() constructor creates a text button from a pair of widgets that serve as the button's icon and label.

The OutlinedButton class has several useful properties to customize an OtlinedButton widget’s appearance and behaviors such as autofocus, clipBehavior, onFocusChange, onPressed, onLongPress, and style.

The OutlinedButton class styleFrom() static method is a static convenience method that constructs an outlined button ButtonStyle given simple values.

The following flutter application development tutorial will demonstrate how to use an OutlinedButton in a flutter application. Here we show a simple text message on the Snackbar when a user pressed the OutlinedButton.
main.dart

import 'package:flutter/material.dart';

void main() => runApp(const FlutterExample());

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

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Example',
theme: ThemeData(primarySwatch: Colors.green),
home: const StateExample()
);
}
}

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

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

class _StateExampleState extends State<StateExample>{
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFFFEFEFA),
appBar: AppBar(
title: const Text("Flutter - OutlinedButton")
),
body: bodyContent(),
);
}


bodyContent() {
return Center(
child: OutlinedButton(
onPressed: (){
SnackBar snackBar = const SnackBar(
content: Text("You clicked me")
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
},
child: const Text("Click Me"),
style: OutlinedButton.styleFrom(
padding: const EdgeInsets.all(20)
),
)
);
}
}

flutter - How to use TextField

Flutter TextField
The TextField class represents a material design text field. The TextField widget lets the user enter text, either with a hardware keyboard or with an onscreen keyboard.

The TextField calls the onChanged callback whenever the user changes the text in the field. The flutter developers can control the text that is displayed in the TextField using the controller.

The controller allows developers to set an initial value. The controller can also control the selection and composing region and observe changes to the text, selection, and composing region.

The flutter developers can use TextField decoration property to control the decoration, for example by adding a label or an icon.

The flutter developers can read the value from a TextField widget to use the onSubmitted callback. The onSubmitted callback is applied to TextField's current value when the user finishes editing.

The flutter app developers can always read the current string from TextField's TextEditingController using TextEditingController.text.

The TextField class has many useful properties such as autocorrect, autofocus, clipBehavior, controller, cursorColor, decoration, enabled, enableSuggestions, expands, keyboardAppearance, keyboardType, maxLength, maxLines, minLines, onChanged, onEditingComplete, onSubmitted, onTap, onTapOutside, readOnly, scrollController, selectionControls, selectionEnabled, showCursor, style, textAlign, textCapitalization, and textDirection.

The TextField class also has several useful methods for example createElement(), createState(), debugDescribeChildren(), toString(), toStringDeep(), and toStringShallow().

In this flutter example code, we put a Text widget, a TextField widget, and an ElevatedButton widget. TextField allows the user to write text and the ElevatedButton widget’s press event displays the inputted text on the Text widget.
main.dart

import 'package:flutter/material.dart';

void main() => runApp(const FlutterExample());

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Example',
        theme: ThemeData(primarySwatch: Colors.teal),
        home: const StateExample());
  }
}

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

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

class _StateExampleState extends State<StateExample> {
  String inputtedText  = "Input your name.";
  final textEditingController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFFEFEFA),
      appBar: AppBar(
        title: const Text("Flutter - TextField Example"),
      ),
      body: bodyContent(),
    );
  }

  bodyContent() {
    return Center(
        child: SizedBox(
            width: 250,
            child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Text(
                    inputtedText,
                    style: const TextStyle(
                        fontWeight: FontWeight.bold,
                        color: Color(0xFF2243B6),
                        fontSize: 21
                    ),
                  ),
                  const SizedBox(height: 10),
                  TextField(
                    controller: textEditingController,
                  ),
                  const SizedBox(height: 10),
                  ElevatedButton(
                      onPressed: () {
                        setState(() {
                          inputtedText = textEditingController.text;
                        });
                      },
                      child: const Text("Show Inputted Name"),
                      style: ElevatedButton.styleFrom(
                        padding: const EdgeInsets.all(20),
                      )
                  )
                ]
            )
        )
    );
  }
}

flutter - How to use IconButton

Flutter IconButton
The IconButton class represents a material design icon button. IconButton is a picture printed on a material widget that reacts to touches by filling with color.

The flutter developers commonly used the Icon buttons in the AppBar actions field, but IconButtons can be used in many other places as well. The flutter app developers can leave the onPressed callback null to disable an IconButton, then it will not react to touch.

The IconButton class has several useful properties such as alignment, autofocus, color, disabledColor, focusColor, highlightColor, hoverColor, icon, iconSize, isSelected, onPressed, padding, selectedIcon, splashColor, style, and tooltip.

The IconButton class’s alignment property defines how the icon is positioned within the IconButton. The color property defines the color to use for the icon inside the button if the icon is enabled. The icon property defines the icon to display inside the button. The iconSize property defines the size of the icon inside the button.

The IconButton class styleFrom() static method is a static convenience method that constructs an icon button ButtonStyle given simple values. But this method is only used for Material 3.

The following flutter application development tutorial will demonstrate how we can use an IconButton widget inside a flutter app. Here we put an IconButton on the app user interface. We set an icon for the IconButton widget. When a user pressed the IconButton a like counter is going up. We also display the like counter update on a Text widget.
main.dart

import 'package:flutter/material.dart';

void main() => runApp(const FlutterExample());

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Example',
        theme: ThemeData(primarySwatch: Colors.deepOrange),
        home: const StateExample());
  }
}

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

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

class _StateExampleState extends State<StateExample> {
  int liked  = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFFEFEFA),
      appBar: AppBar(
        title: const Text("Flutter - IconButton example"),
      ),
      body: bodyContent(),
    );
  }

  bodyContent() {
    return Center(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            IconButton(
                icon: const Icon(Icons.favorite),
                onPressed: () {
                  setState(() {
                    liked ++;
                  });
                }
            ),
            const SizedBox(width: 12),
            Text(
              "Liked : $liked",
              style: const TextStyle(
                  fontWeight: FontWeight.bold,
                  color: Colors.blueAccent
              ),
            ),
          ],
        )
    );
  }
}

flutter - How to use ElevatedButton

Flutter ElevatedButton
The ElevatedButton class represents a material design elevated button. The flutter developers can use elevated buttons to add dimension to otherwise mostly flat layouts such as in long busy lists of content, or in wide spaces.

The flutter developers should avoid using elevated buttons on already-elevated content such as dialogs or cards. An ElevatedButton widget is a label child displayed on a Material widget whose Material.elevation increases when the button is pressed.

The label's Text and Icon widgets are displayed in style's ButtonStyle.foregroundColor and the button's filled background is the ButtonStyle.backgroundColor.

The ElevatedButton widget's default style is defined by defaultStyleOf. The style of this ElevatedButton can be overridden with its style parameter. The static styleFrom method is a convenient way to create an ElevatedButton ButtonStyle from simple values.

If the ElevatedButton widget’s onPressed and onLongPress callbacks are null then the elevated button will be disabled.

In the following flutter application development tutorial code, we put two ElevatedButton widget instances. Here we create a simple counter. When the first button is pressed the counter is up. And when users press the second button the counter is down. We also change the first button’s background color.
main.dart

import 'package:flutter/material.dart';

void main() => runApp(const FlutterExample());

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Example',
        theme: ThemeData(primarySwatch: Colors.deepOrange),
        home: const StateExample());
  }
}

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

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

class _StateExampleState extends State<StateExample> {
  int counter = 0;

  void _increment() {
    setState(() {
      counter++;
    });
  }

  void _decrement() {
    setState(() {
      counter--;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Flutter - ElevatedButton example"),
      ),
      body: bodyContent(),
    );
  }

  bodyContent() {
    return Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              "Counter : $counter",
              style: const TextStyle(fontSize: 25),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Padding(
                  padding: const EdgeInsets.all(20),
                  child: ElevatedButton(
                    onPressed: _increment,
                    child: const Text("Increment"),
                    style: ElevatedButton.styleFrom(
                      primary: Colors.green,
                    ),
                  ),
                ),
                ElevatedButton(
                    onPressed: _decrement,
                    child: const Text("Decrement")
                )
              ],
            )
          ],
        )
    );
  }
}

flutter - How to underline Text

Flutter - Text Style Underline
The Text widget displays a string of text with a single style in a flutter app. But depending on the layout constraints the string might break across multiple lines or might all be displayed on the same line. The style argument is optional in a Text instance. When the style argument is omitted then the text will use the style from the closest enclosing DefaultTextStyle. By default, the Text is not selectable. But flutter developers can make a Text selectable by wrapping a subtree with a SelectionArea widget.

In the following flutter application development tutorial, we will demonstrate how we can underline text. Here we will use the TextStyle class decoration property to decorate the Text by putting an underline.

The TextStyle class represents an immutable style describing how to format and paint text. The TextStyle class decoration property is used to apply decorations to paint near the text such as an underline. The flutter app developer can apply multiple decorations with TextDecoration.combine.

The TextDecoration class represents a linear decoration to draw near the text. The TextDecoration class’s underline const draws a line underneath each line of text.

So finally, the flutter app developers can put an underline on a Text by using the TextStyle class’s decoration property. And they also have to set the decoration property value to the underline constant as TextDecoration.underline.
main.dart

import 'package:flutter/material.dart';

void main() => runApp(const FlutterExample());

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

  @override
  Widget build(BuildContext context){
    return MaterialApp(
      title: 'Flutter Example',
      theme: ThemeData(primarySwatch: Colors.deepOrange),
      home: Scaffold(
        appBar: AppBar(
          title: const Text("Flutter - Text Style Underline"),
        ),
        body: const BodyContents(),
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    {
      return const Center(
        child: Text(
          'Underlined Text',
          style: TextStyle(
              fontSize: 50,
              decoration: TextDecoration.underline
          ),
        ),
      ) ;
    }
  }
}