Skip to main content

Flutter Create Clickable Text

In flutter we can set onPressed function on Text with TextButton widget. TextButton allow us to create simple text button with all the text styling enabled. TextButton is also an alternative of FlatButton. Since FlatButton is deprecated.

Flutter Create Clickable Text

1. Creating TextButton widget and define onPressed{} event.
TextButton(
        onPressed: test,
        child: const Text(
          'Text Button',
          style: TextStyle(fontWeight: FontWeight.bold, fontSize: 22),
          textAlign: TextAlign.center,
        ),
      )
Screenshot:
Flutter Create Clickable Text
Source code for main.dart file:
import 'package:flutter/material.dart';

void main() => runApp(App());

class App extends StatelessWidget {
  void test() {
    print('Text Clicked...');
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            body: Center(
      child: TextButton(
        onPressed: test,
        child: const Text(
          'Text Button',
          style: TextStyle(fontWeight: FontWeight.bold, fontSize: 22),
          textAlign: TextAlign.center,
        ),
      ),
    )));
  }
}

Comments