在步进器小部件中的初始化程序中只能访问静态成员

时间:2019-05-07 10:08:07

标签: android flutter

这是一个Flutter应用程序,它使用步进器小部件来显示其他小部件。我需要将两个复选框磁贴窗口小部件放在一行中,然后将它们按列分开。我已经初始化了复选框磁贴窗口小部件,但是对于onChanged和value参数

,它始终显示“只能在初始化程序中访问静态成员”。

class MyHome extends StatefulWidget {
  @override
  MyHomeState createState() => new MyHomeState();
}

class MyHomeState extends State<MyHome> {

  void onChanged(bool value){
    setState(() {
      _isChecked = value;
    });
  }
static bool _isChecked = false;

  // init the step to 0th position
  int current_step = 0;
  List<Step> my_steps = [
    new Step(
      // Title of the Step
        title: new Text("Residential Data"),
        // Content, it can be any widget here. Using basic Text for this example
        content: Column(
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: TextField(),
            ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: TextField(),
            ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: TextField(),
            ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: Text("Activity"),
            ),
            Row(
              children: <Widget>[
                Column(
                  children: <Widget>[
                    CheckboxListTile(value: _isChecked, onChanged: (bool value){setState(() {
                      _isChecked = value;
                    });})
                  ],
                ),

                Column(
                  children: <Widget>[
                    CheckboxListTile(value: _isChecked, onChanged: (bool value){onChanged(value);},)
                  ],
                )
              ],
            ),

            Row(
              children: <Widget>[
                Column(
                  children: <Widget>[
                    CheckboxListTile(value: _isChecked, onChanged: (bool value){onChanged(value);})
                  ],
                ),

                Column(
                  children: <Widget>[
                    CheckboxListTile(value: _isChecked, onChanged: (bool value){onChanged(value);},)
                  ],
                )
              ],
            )
          ],
        ),//new Text("Hello!"),
        isActive: true),
    new Step(
        title: new Text("Step 2"),
        content: new Text("World!"),
        // You can change the style of the step icon i.e number, editing, etc.
        state: StepState.editing,
        isActive: true),
    new Step(
        title: new Text("Step 3"),
        content: new Text("Hello World!"),
        isActive: true),
  ];

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      // Appbar
      appBar: new AppBar(
        // Title
        title: new Text("Simple Material App"),
      ),
      // Body
      body: new Container(
          child: new Stepper(
            // Using a variable here for handling the currentStep
            currentStep: this.current_step,
            // List the steps you would like to have
            steps: my_steps,
            // Define the type of Stepper style
            // StepperType.horizontal :  Horizontal Style
            // StepperType.vertical   :  Vertical Style
            type: StepperType.vertical,
            // Know the step that is tapped
            onStepTapped: (step) {
              // On hitting step itself, change the state and jump to that step
              setState(() {
                // update the variable handling the current step value
                // jump to the tapped step
                current_step = step;
              });
              // Log function call
              print("onStepTapped : " + step.toString());
            },
            onStepCancel: () {
              // On hitting cancel button, change the state
              setState(() {
                // update the variable handling the current step value
                // going back one step i.e subtracting 1, until its 0
                if (current_step > 0) {
                  current_step = current_step - 1;
                } else {
                  current_step = 0;
                }
              });
              // Log function call
              print("onStepCancel : " + current_step.toString());
            },
            // On hitting continue button, change the state
            onStepContinue: () {
              setState(() {
                // update the variable handling the current step value
                // going back one step i.e adding 1, until its the length of the step
                if (current_step < my_steps.length - 1) {
                  current_step = current_step + 1;
                } else {
                  current_step = 0;
                }
              });
              // Log function call
              print("onStepContinue : " + current_step.toString());
            },


          )),

    );
  }

}

1 个答案:

答案 0 :(得分:0)

您将小部件树创建为类的字段:

class Foo extends StatelessWidget {
  String parameter;

  Widget widget = Text(parameter); // only static members can be accessed in initializers
}

您不应该这样做。您不能使用其他类属性初始化对象的字段。而是在 build 方法内创建该小部件:

class Foo extends StatelessWidget {
  String parameter;

  @override
  Widget build(BuildContext context) {
    Widget widget = Text(parameter);

    // TODO: do something width `widget`
  }  
}