【问题标题】:parssing data with flutter stateful widget and using it使用颤振有状态小部件解析数据并使用它
【发布时间】:2020-05-09 21:55:58
【问题描述】:

我想在覆盖之前使用用 if 语句解析的数据, 怎么做? 我尝试了很多方法,但没有奏效。 请帮帮我。

import 'package:flutter/material.dart';

class MyButton extends StatefulWidget {
  Function buttonFunction;
  String buttonName;
  MyButton(this.buttonFunction,this.buttonName);

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

class _MyButtonState extends State<MyButton> {

if (widget.buttonName == "ok"){
  int i =1;
}
else int i =2;
  @override
  Widget build(BuildContext context) {
    return Container(
      child: RaisedButton(
        onPressed: () {
          widget.buttonFunction();
        },
        child: Text("${widget.buttonName}",
        ),
      ),
    );
  }
}```

【问题讨论】:

标签: parsing flutter dart widget stateful


【解决方案1】:

您应该在initState 方法中进行大部分初始化。

  int i;

  @override
  void initState() {
    if (widget.buttonName == "ok") {
      i = 1;
    } else
      i = 2;
    super.initState();
  }

【讨论】:

    猜你喜欢
    • 2022-11-12
    • 2023-03-11
    • 2020-05-09
    • 2019-11-29
    • 2020-04-20
    • 2020-03-15
    • 2021-01-19
    • 2021-03-11
    • 2020-07-29
    相关资源
    最近更新 更多