Flutter get text from widgets

Viewed 1417

I am coming from java and now making my very initial steps with flutter.

In my first application I am playing around with the layouts and the buttons and I am facing difficulties getting the button actions to work, probably due to my coming from from java.

In my app I have, among others, the following widgets:

  Widget _buttonOne = RaisedButton(
    onPressed: () {},
    child: Text('Button One', style: TextStyle(fontSize: 20)),
  );

  final _textContainer =
      const Text('Container One', textAlign: TextAlign.center);

 Container(
    padding: const EdgeInsets.all(8),
    child: _textContainer,
    color: Colors.teal[200],
 ),

Now I want to print the text of the button and textchild of the container. How do I achieve that?

 CupertinoButton.filled(
     child: Text('Button Two'),
     onPressed: () {
         print(_textContainer);   // how do I print the text of the text widget here? 
         print (_buttonOne. ....); // on java there is a getText() method .... how does this work in flutter? 
 ),
2 Answers

You can access the text of a Text widget by using the data property:

final _textContainer =
      const Text('Container One', textAlign: TextAlign.center);

@override
Widget build(BuildContext context) {
   return Scaffold(body: 
      Center(
         child: CupertinoButton.filled(
           child: Text('Button Two'),
           onPressed: () =>
                  {print(_textContainer.data)},
           )
        )
    );
 }

In a similar fashion, you could access the content of the RaisedButton child:

final _raisedButtonText = const Text('Button One', style: TextStyle(fontSize: 20));

Widget _buttonOne = RaisedButton(onPressed: () {}, child: _raisedButtonText);

final _textContainer =
      const Text('Container One', textAlign: TextAlign.center);

@override
Widget build(BuildContext context) {
   return Scaffold(body: 
      Center(
         child: CupertinoButton.filled(
           child: Text('Button Two'),
           onPressed: () {
                  print(_textContainer.data);
                  print(_raisedButtonText.data);
              }
           )
        )
    );
 }

You can copy paste run full code below
You can use as to do type casting then access attribute data
code snippet

CupertinoButton.filled(
                child: Text('Button Two'),
                onPressed: () {
                  print(_textContainer.data);
                  print(((_buttonOne as RaisedButton).child as Text).data);
                }),

output

I/flutter (30756): Container One
I/flutter (30756): Button One

full code

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  Widget _buttonOne = RaisedButton(
    onPressed: () {},
    child: Text('Button One', style: TextStyle(fontSize: 20)),
  );

  final _textContainer =
      const Text('Container One', textAlign: TextAlign.center);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            CupertinoButton.filled(
                child: Text('Button Two'),
                onPressed: () {
                  print(_textContainer
                      .data); // how do I print the text of the text widget here?
                  print(((_buttonOne as RaisedButton).child as Text).data);
                }),
          ],
        ),
      ),
    );
  }
}
Related