【问题标题】:Flutter get text from widgetsFlutter 从小部件中获取文本
【发布时间】:2021-03-24 15:30:59
【问题描述】:

我来自 java,现在开始使用 Flutter。

在我的第一个应用程序中,我正在玩弄布局和按钮,但我在让按钮操作正常工作时遇到了困难,这可能是因为我来自 java。

在我的应用中,我有以下小部件:

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

现在我想打印容器的按钮和 textchild 的文本。我如何做到这一点?

 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? 
 ),

【问题讨论】:

  • 以下两个答案可以解决您的问题,但这种方法并不能反映 Flutter 的模块化
  • @Yadu 能详细解释一下吗?
  • 您是使用按钮文本作为值还是纯粹用于调试目的? (如果是为了调试,那么下面的答案看起来不错),在知道你试图在更广泛的例子中实现什么后,可以给出详细的解释
  • 感谢您的关注。我的目标是发现如何在实际应用程序中使用这样的值。目前我仍在弄清楚 Flutter 是如何处理事情的。

标签: flutter text widget


【解决方案1】:

您可以在下面复制粘贴运行完整代码
您可以使用as 进行类型转换,然后访问属性data
代码sn-p

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

输出

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

完整代码

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

【讨论】:

  • 非常感谢这个详细的回答......,非常有帮助
【解决方案2】:

您可以使用data 属性访问Text 小部件的text

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

以类似的方式,您可以访问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);
              }
           )
        )
    );
 }

【讨论】:

  • 酷,非常感谢您的回复....它工作得很好
猜你喜欢
  • 2019-06-13
  • 2019-06-11
  • 2020-12-11
  • 2015-05-10
  • 2021-01-24
  • 2019-05-25
  • 2021-06-15
  • 1970-01-01
  • 2023-03-07
相关资源
最近更新 更多