【问题标题】:Flutter: Using AbsorbPointer and change the look of the WidgetsFlutter:使用 AbsorbPointer 并更改 Widgets 的外观
【发布时间】:2022-01-19 10:10:01
【问题描述】:

如何更改 AbsorbPointer 中多个小部件的外观?我知道使用它绘制的 AbsorbPointer 像往常一样是儿童。但是,根据某些条件,小部件是否有可能看起来像它们被禁用一样。 提前感谢您的帮助。

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  bool isDisabled = false;
  bool isSwitched = false;
  double _value = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
          child: Column(
        children: [
          Switch(
              value: isDisabled,
              onChanged: (check) {
                setState(() {
                  isDisabled = check;
                });
              }),
          const SizedBox(
            height: 40,
          ),
          AbsorbPointer(
            absorbing: !isDisabled,
            child: Column(
              children: [
                Switch(
                    onChanged: (value) {
                      setState(() {
                        isSwitched = value;
                      });
                    },
                    value: isSwitched),
                Slider(
                  min: 0.0,
                  max: 100.0,
                  value: _value,
                  onChanged: (value) {
                    setState(() {
                      _value = value;
                    });
                  },
                ),
                ElevatedButton(
                  onPressed: () {},
                  child: const Text('BUTTON'),
                )
              ],
            ),
          ),
        ],
      )),
    );
  }
}



【问题讨论】:

  • 你可以使用final absorber = context.findAncestorRenderObjectOfType&lt;RenderAbsorbPointer&gt;(); print(absorber?.absorbing);
  • @pskink 感谢您的快速回复。我不明白我怎么能用这个?!?!
  • 在子小部件中使用该代码来查看是否有父小部件AbsorbPointer 小部件

标签: flutter


【解决方案1】:

我这样改变了外观。我的走近寻​​找我,就像很多代码一样。有没有更好的方法来做这个?

class _MyHomePageState extends State<MyHomePage> {
  bool isDisabled = false;
  bool isSwitched = false;
  double _value = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
          child: Column(
        children: [
          Switch(
              value: isDisabled,
              onChanged: (check) {
                setState(() {
                  isDisabled = check;
                });
              }),
          const SizedBox(
            height: 40,
          ),
          AbsorbPointer(
            absorbing: !isDisabled,
            child: Column(
              children: [
                Switch(
                  value: isSwitched,
                  activeColor: isDisabled
                      ? const Color.fromARGB(255, 102, 187, 106)
                      : Colors.grey.shade600,
                  activeTrackColor: isDisabled
                      ? const Color.fromARGB(255, 190, 243, 193)
                      : Colors.grey.shade400,
                  inactiveThumbColor: isDisabled
                      ? const Color.fromARGB(255, 218, 18, 18)
                      : Colors.grey.shade600,
                  inactiveTrackColor: isDisabled
                      ? const Color.fromARGB(255, 250, 142, 142)
                      : Colors.grey.shade400,
                  onChanged: (value) {
                    setState(() {
                      isSwitched = value;
                    });
                  },
                ),
                Slider(
                  activeColor: isDisabled
                      ? const Color(0xFFEF6C00)
                      : Colors.grey.shade600,
                  inactiveColor: isDisabled
                      ? const Color(0xFFEF6C00)
                      : Colors.grey.shade600,
                  thumbColor: isDisabled
                      ? const Color(0xFFEF6C00)
                      : Colors.grey.shade600,
                  min: 0.0,
                  max: 100.0,
                  value: _value,
                  onChanged: (value) {
                    setState(() {
                      _value = value;
                    });
                  },
                ),
                ElevatedButton(
                  style: ButtonStyle(
                    backgroundColor: isDisabled
                        ? MaterialStateProperty.all(Colors.red)
                        : MaterialStateProperty.all(
                            Colors.grey.shade600,
                          ),
                  ),
                  onPressed: () {},
                  child: const Text('BUTTON'),
                )
              ],
            ),
          ),
        ],
      )),
    );
  }
}


【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    • 1970-01-01
    • 2011-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    相关资源
    最近更新 更多