我的字符串已初始化,但仍显示为null(颤振-飞镖)

时间:2020-05-12 01:44:41

标签: string flutter dart text

希望您一切顺利!

我正在研究一个带有颤振的简单字符串教程,并且遇到了有关字符串和Text小部件的问题。我正在尝试在_MyHomePageState类中创建一个字符串变量,但是即使我为其设置了一个值,我的displayTxt仍显示为空。我使用插值法来查看其包含的值,并且可以肯定的是,它为空。我对飞镖和打靶还不陌生,因此,感谢您的帮助。

祝你有美好的一天!

  • 列表项
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      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> {

  **String displayTxt = "Hello";**

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Row(
              children: <Widget>[
                Text(
                  "Some text here",
                  style: TextStyle(
                    color: Colors.blue,
                    fontSize: 45,
                    fontWeight: FontWeight.bold,
                    fontStyle: FontStyle.italic,
                    letterSpacing: 2,
                    shadows: [
                      Shadow(
                        color: Colors.grey,
                        offset: Offset(-5,-5),
                      ),
                    ],
                    decoration: TextDecoration.underline,
                  ),
                ),
              ],
            ),

            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                MaterialButton(
                  onPressed: () => {},
                  child: Row(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      Icon(
                        Icons.airplanemode_active,
                        color: Colors.white,
                      ),
                      Text('press me'),
                    ],
                  ),
                  color: Colors.black,
                  textColor: Colors.white,
                  highlightColor: Colors.white10,
                  elevation: 10,
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  **child: Text("${displayTxt}"),**
                ),
              ],
            )
          ],
        ),
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:0)

我刚刚解决了我的问题,结果是当函数中没有使用字符串时,它显示为null。那是我的简单解释,但是如果有更多经验的人可以详细说说,那将是有帮助的。

我添加了此内容,然后没有更多错误

 onPressed: () => setState(() {
                this.displayTxt = 'Some text to display';
              }),