Flutter get text from widgets

1,149

Solution 1

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);
                }),
          ],
        ),
      ),
    );
  }
}

Solution 2

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);
              }
           )
        )
    );
 }
Share:
1,149
rainer
Author by

rainer

By day : Project Planner and Java Programmer .... By night : Father of a 7-year-old girl ....

Updated on December 26, 2022

Comments

  • rainer
    rainer over 1 year

    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? 
     ),
    
    • Yadu
      Yadu over 3 years
      below two answers solves your problem but this approach doesn't reflects the Flutter's modularity
    • rainer
      rainer over 3 years
      @Yadu Can you explain in a little more detail?
    • Yadu
      Yadu over 3 years
      are you using buttons text as a value or it was purely for debugging purposes? (if it is for debugging then below answers looks fine), detailed explanation can be given after knowing what were you trying to achieve in a little more broader example
    • rainer
      rainer over 3 years
      Thanks for your interest. My aim is to discover how to use such a value in a real application. For the moment I am still figuring out how Flutter is handling things.
  • rainer
    rainer over 3 years
    Cool, many thanks for your reply.... it's working perfectly
  • rainer
    rainer over 3 years
    great many thanks for this detailed answer ..., very helpful