【问题标题】:Incorrect use of ParentDataWidget - How fix this error?ParentDataWidget 使用不正确 - 如何修复此错误?
【发布时间】:2021-04-14 15:56:56
【问题描述】:

1/ 我对这段代码有疑问,但 3 天后我仍然不明白为什么。 请参阅图片以更好地了解软件给我的错误(不正确使用 ParentDataWidget 错误)。 Error code in Android Studio program

2/ 然后我有另一个关于初始化的问题:在 void Initstate 中,变量visualizedText 不能用函数getTranslated 翻译(它不接受上下文)。出于什么原因?

class WarmUpPage extends StatefulWidget {
  WarmUpPage({Key key}) : super(key: key);
  @override
  _WarmUpPageState createState() => _WarmUpPageState();
}

class _WarmUpPageState extends State<WarmUpPage> {
  int counter = 8;
  dynamic visualizedText; //text to be visualized above button
  Image imgButton;
  Image imgButtonUp = Image.asset("lib/images/unpressed.png");
  Image imgButtonDown = Image.asset("lib/images/pressed.png");

  //Inizialize the state of the picture into imgButtonUp
  void initState() {
    super.initState();
    imgButton = imgButtonUp;
    visualizedText ='Warm Up'; //initial text but I cannot translate it because context gives error
  }

  //Function to increment the counter
  void _decrementCounter() {
    setState(() {
      counter--;
      if (counter == 0) {
        randompagegenerator(context);
      } else if (counter == 99) {
        visualizedText = getTranslated(context, 'warmup1');
      } else if (counter == 70) {
        visualizedText = getTranslated(context, 'warmup2');
      } else if (counter == 40) {
        visualizedText = getTranslated(context, 'warmup3');
      } else if (counter == 20) {
        visualizedText = getTranslated(context, 'warmup4');
      } else if (counter == 10) {
        visualizedText = getTranslated(context, 'warmup5');
      } else if (counter == 5) {
        visualizedText = getTranslated(context, 'warmup6');
      } else if (counter == 1) {
        visualizedText = getTranslated(context, 'warmup7');
      } else {}
    });
  }

  @override
  Widget build(BuildContext context) {
    TextStyle _textStyleText = TextStyle(
      color: Colors.white,
      fontSize: 40,
    );
    TextStyle _textStyleNumber = TextStyle(
      color: Colors.white,
      fontSize: 70,
    );
    return Scaffold(
      //backgroundColor: Colors.white,
       appBar: ReusableBar(),
      body: SafeArea(
        child: Stack(
          alignment: Alignment.topCenter,
          children: [
            Container(
              width: MediaQuery.of(context).size.width / 1,
              height: MediaQuery.of(context).size.height / 1,
              padding: EdgeInsets.all(100),
              alignment: Alignment.center,
              //color: Colors.grey[850],
              //color: Theme.of(context).colorScheme.background,
              child: Center(
                child: Positioned(
                  width: MediaQuery.of(context).size.width / 1,
                  child: GestureDetector(
                    child: imgButton,
                    onTap:() {
                      _decrementCounter();
                      incrementAppBarCounter();
                  },
                    onTapDown: (tap) {
                      setState(() {
                        imgButton = imgButtonDown;
                      });
                    },
                    onTapUp: (tap) {
                      setState(() {
                        imgButton = imgButtonUp;
                      });
                    },
                  ),
                ),
              ),
            ),
            Positioned(
              top: 0,
              width: MediaQuery.of(context).size.width * 0.7,
              height: MediaQuery.of(context).size.height * 0.28,
              child: Container(
                //width: MediaQuery.of(context).size.width / 1,
                //height: MediaQuery.of(context).size.height * 0.25,
                padding: EdgeInsets.all(8),
                alignment: Alignment.bottomCenter,
                //color:Colors.grey[500],
                child: Text(
                  visualizedText,
                  style: _textStyleText,
                  textAlign: TextAlign.center,
                ),
              ),
            ),
            Positioned(
              width: MediaQuery.of(context).size.width / 1,
              top: MediaQuery.of(context).size.height * 0.58,
              child: Container(
                width: MediaQuery.of(context).size.width / 1,
                //height: MediaQuery.of(context).size.height *0.2,
                padding: EdgeInsets.all(1),
                alignment: Alignment.topCenter,
                //color: Colors.grey[200],
                child: Text('$counter', style: _textStyleNumber),
              ),
            ),
            Positioned(
              bottom: 0,
              width: MediaQuery.of(context).size.width / 1,
              child: Container(
                height: MediaQuery.of(context).size.height / 15,
                padding: EdgeInsets.all(1),
                alignment: Alignment.center,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

【问题讨论】:

  • - 对于 [2],如果您能详细说明 getTranslated 实际在做什么以及您遇到什么上下文错误,那就太好了。

标签: flutter dart syntax-error


【解决方案1】:
  • 对于 [1],

定位小部件必须是堆栈的后代,并且从定位小部件到其封闭的Stack 的路径必须仅包含StatelessWidgetsStatefulWidgets,显然您正在用@ 包装Positioned() 987654325@ 和Center()

【讨论】:

    猜你喜欢
    • 2020-11-18
    • 1970-01-01
    • 2021-07-27
    • 2021-01-22
    • 2021-01-20
    • 2021-10-04
    • 1970-01-01
    • 2021-06-08
    • 2021-07-10
    相关资源
    最近更新 更多