Flutter TextButton Widget – Usage and Examples

In this tutorial, we will explore the Button widgets available in the Flutter. Compared to the previous version Flutter2, Flutter3 has updated the button widget. We will see how to use the TextButton widget and customize it. If you want to learn how to create your first flutter project, head over to this tutorial on making your first flutter project.

TextButtons, as the name suggests, are widgets with simple text that can be interacted with a click. It uses the theme “TextButtonTheme”. These buttons are useful for action in dialogs, snackbars and cards. The material design 3 specifications for the TextButton can be found in here.

TextButton without Icon

Constructor of the TextButton given below. The onPressed and child properties are mandatory.

TextButton({
    Key? key,
    required VoidCallback? onPressed, //onPressed is mandatory
    VoidCallback? onLongPress,
    ValueChanged<bool>? onHover,
    ValueChanged<bool>? onFocusChange,
    ButtonStyle? style,
    FocusNode? focusNode,
    bool autofocus = false,
    Clip clipBehavior = Clip.none,
    required Widget child,            //child is mandatory
})

Let’s see how we can use the button on a sample application.

Flutter TextButton without icon
Flutter TextButton with Snackbar message on press action
import 'package:flutter/material.dart';

void main() {
  runApp(const TextButtonExample());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(title: const Text('TextButton Example')),
          body: const TextButtonWidget()),
    );
  }
}

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

  //Button click handler: Show snackbar
  handleButtonClick(BuildContext context) {
    const snackBar = SnackBar(
      content: Text("Text Button Clicked!"),
    );
    ScaffoldMessenger.of(context).showSnackBar(snackBar);
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      //Create Text Button
      child: TextButton(
        //Handle button press event
        onPressed: () {
          handleButtonClick(context);
        },
        //Contents of the button
        child: const Text("Click Text Button!")
      ),
    );
  }
}

Customize the TextButton

TextButton uses the theme TextButtonTheme. Let’s see how we apply the following style updates for the Button.

  1. Change the font size to 20: Use textStyle
  2. Make the button font bold: Use textStyle
  3. Change the text color to RED: Use primary
  4. Add padding of 20px on all sides: Use padding
@override
Widget build(BuildContext context) {
  return Center(
    //Create Text Button
    child: TextButton(
      //Handle button press event
        onPressed: () {
          handleButtonClick(context);
        },
        style: TextButton.styleFrom(
            //Change font size and weight
            textStyle: const TextStyle(
              fontSize: 20,
              fontWeight: FontWeight.bold,
            ),
            //Set the foreground color
            primary: Colors.red,
            //Set the padding on all sides to 20px
            padding: const EdgeInsets.all(20.0),
        ),
        //Contents of the button
        child: const Text("Click Text Button!")),
  );
}
Customized Flutter 3 TextButton
Customized Flutter TextButton

TextButton with Icon

Now, let’s see how we can create the TextButton with an icon. There is a special factory constructor available for creating TextButton with an Icon. Let’s have a look into the factory constructor.

factory TextButton.icon({
    Key? key,
    required VoidCallback? onPressed,  //onPressed is mandatory
    VoidCallback? onLongPress,
    ValueChanged<bool>? onHover,
    ValueChanged<bool>? onFocusChange,
    ButtonStyle? style,
    FocusNode? focusNode,
    bool? autofocus,
    Clip? clipBehavior,
    required Widget icon,             //Icon is mandatory
    required Widget label,            //Label is mandatory
})

As it can be seen from the above constructor, we need to provide an icon, label and onPressed values.

Let’s see how we can do this with an example code.

@override
Widget build(BuildContext context) {
  return Center(
    //Create Text Button
    child: TextButton.icon(
        onPressed: () {
          handleButtonClick(context);
        },
        style: TextButton.styleFrom(
          //Change font size and weight
          textStyle: const TextStyle(
            fontSize: 20,
            fontWeight: FontWeight.bold,
          ),
          //Set the foreground color
          primary: Colors.deepOrange,
          //Set the padding on all sides to 20px
          padding: const EdgeInsets.all(20.0),
        ),
        //Set the icon
        icon: const Icon(Icons.send_rounded),
        //Set the label
        label: const Text("Click Text Button!")),
  );
}

It generates the following output. As you can see, the icon is added to the button, the label is set and the customized styling is applying.

TextButton With Icon
Flutter TextButton with an icon

Conclusion

In this tutorial, we have learned how to use the TextButtons in flutter. We have seen how to use it, customize it, and add icons to it. If you liked this article, you might want to check out my other Flutter tutorials.

Comments

55 responses to “Flutter TextButton Widget – Usage and Examples”

  1. … [Trackback]

    […] Info on that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  2. … [Trackback]

    […] Read More to that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  3. … [Trackback]

    […] Read More to that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  4. … [Trackback]

    […] Read More Info here on that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  5. … [Trackback]

    […] Info to that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  6. … [Trackback]

    […] Find More to that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  7. … [Trackback]

    […] Read More on to that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  8. … [Trackback]

    […] Find More on to that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  9. … [Trackback]

    […] Info to that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  10. … [Trackback]

    […] Information on that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  11. … [Trackback]

    […] Find More Info here to that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  12. … [Trackback]

    […] Information on that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  13. … [Trackback]

    […] Find More Information here to that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  14. … [Trackback]

    […] Read More on on that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  15. … [Trackback]

    […] Find More on that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  16. … [Trackback]

    […] Read More Information here on that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  17. … [Trackback]

    […] Information on that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  18. … [Trackback]

    […] Find More to that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  19. … [Trackback]

    […] Info to that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  20. … [Trackback]

    […] Read More here to that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  21. … [Trackback]

    […] Information to that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  22. … [Trackback]

    […] Here you will find 21457 additional Info on that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  23. … [Trackback]

    […] There you can find 56685 more Information on that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  24. … [Trackback]

    […] Find More on on that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  25. … [Trackback]

    […] Find More on to that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  26. … [Trackback]

    […] Find More here on that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  27. … [Trackback]

    […] Information to that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  28. … [Trackback]

    […] Info to that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  29. … [Trackback]

    […] Find More Info here to that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  30. … [Trackback]

    […] Find More Information here to that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  31. … [Trackback]

    […] Find More Information here to that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  32. … [Trackback]

    […] Find More to that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  33. … [Trackback]

    […] Find More on on that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  34. … [Trackback]

    […] There you will find 23087 more Info on that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  35. … [Trackback]

    […] Here you will find 69251 more Information on that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  36. … [Trackback]

    […] Find More on to that Topic: genuinecoder.com/flutter-textbutton-tutorial-with-examples/ […]

  37. order androxal canada online order

    ordering androxal generic free shipping

  38. online order enclomiphene generic tablets

    order enclomiphene uk meds

  39. how to order rifaximin lowest price viagra

    get rifaximin buy in australia

  40. online order xifaxan uk generic

    cheap xifaxan cheap united states

  41. how to order staxyn without prescriptions uk

    online order staxyn price from cvs

  42. canadian pharmacy avodart generic

    buying avodart price new zealand

  43. acheter générique kamagra en ligne canada

    commande kamagra livraison livraison le lendemain

  44. flexeril cyclobenzaprine online prescription

    how to buy flexeril cyclobenzaprine uk over the counter

  45. how to buy dutasteride price usa

    purchase dutasteride buy dallas

  46. get gabapentin uk order

    get gabapentin generic london

  47. buying fildena generic uae

    generic fildena over the counter

  48. buy itraconazole price australia

    no prescription next day delivery itraconazole

  49. kamagra bez lテゥkaナ冱kテゥho pナ册dpisu pナ册s noc

    obecný kamagra z kanady

  50. reglan medication

    reglan medication

  51. tetracycline for acne cream

    tetracycline for acne cream

  52. buy antibiotics online

    buy antibiotics online

  53. doxycycline for dogs price

    doxycycline for dogs price

  54. dexlansoprazole 30 mg oral capsule

    dexlansoprazole 30 mg oral capsule

Leave a Reply