如何在Flutter中访问小部件的属性?

时间:2019-04-17 08:06:23

标签: dart flutter

例如,小部件“ IgnorePointer”,其属性“ ignoring”可以为true或false。但我想动态更改和访问此属性。

如果忽略是正确的,那么我希望它的子颜色是不同的,否则是正常的。并单击按钮,我要将此属性设置为true / false,请提供帮助。

import 'package:flutter/material.dart';

class HelloWorld extends StatefulWidget {
 @override
_HelloWorldState createState() => _HelloWorldState();
}

class _HelloWorldState extends State<HelloWorld> {
 @override
 Widget build(BuildContext context) {
return Scaffold(
    body: Column(children: <Widget>[

  IgnorePointer(
      ignoring: false, //<= the property we talkin about
      child: FlatButton(
        child: Text("Useless Button"),
        onPressed: () {},
      )
      ),

  FlatButton(
    child: Text("Toggle IgnorePointer"),
    onPressed: () {}, //TODO : toggle IgnorePointer's property true or false.
  )
]));
  }
}

1 个答案:

答案 0 :(得分:1)

您可以根据需要创建一个简单的bool类型变量并将其设置为默认值(真或假),并在setState中将其值更改为FlatButton onPressd方法。

可能看起来像这样。

class HelloWorld extends StatefulWidget {
    @override
    _HelloWorldState createState() => _HelloWorldState();
  }

  class _HelloWorldState extends State<HelloWorld> {

    bool check = false;
    @override
    Widget build(BuildContext context) {
      return Scaffold(
          body: Column(children: <Widget>[

            IgnorePointer(
                ignoring: check, //<= the property we talkin about
                child: FlatButton(
                  child: Text("Useless Button"),
                  onPressed: () {},
                )
            ),

            FlatButton(
              child: Text("Toggle IgnorePointer"),
              onPressed: () {
                setState(() {
                  if(check){
                    check = false;
                  }else{
                    check = true;
                  }
                });
              }, //TODO : toggle IgnorePointer's property true or false.
            )
          ]));
    }
  }