简体   繁体   English

如何在有状态小部件 flutter 中使用 static 构造函数?

[英]How to use static constructor in stateful widget flutter?

I want to make a clickable iconButton with a different boolean value init.我想用不同的 boolean 值初始化来制作一个可点击的iconButton

get the IconButton code:获取IconButton代码:

Row(
 children: [
  CheckIcon(isCheck: true,),
  CheckIcon(isCheck: false,),
  CheckIcon(isCheck: true,),
 ],
),

IconButton Code: IconButton代码:

import 'package:flutter/material.dart';

class CheckIcon extends StatefulWidget {
  static bool? isCheck;

  //the error is hire
  const CheckIcon({
    Key? key,
    // I can't initialize the static boolean in constructor
  }) : super(key: key);

  @override
  State<CheckIcon> createState() => _CheckIconState();
}

class _CheckIconState extends State<CheckIcon> {
  @override
  Widget build(BuildContext context) {
    return IconButton(
      splashRadius: 18.0,
      icon: Icon(
        CheckIcon.isCheck!
            ? Icons.check_circle_rounded
            : Icons.check_circle_outline_rounded,
        color: Colors.lightBlue,
        size: 20,
      ),
      onPressed: () {
        setState(() {
          CheckIcon.isCheck = !CheckIcon.isCheck!;
        });
      },
    );
  }
}

How to initialize the static boolean in the constructor stateful widget in flutter?如何在 flutter 的构造函数状态小部件中初始化 static boolean?

I hope you have the idea to share, cheers... thanks我希望你有想法分享,干杯...谢谢

Why use static ?为什么使用static Static field is shared across all instances, you won't have different states. Static 字段在所有实例之间共享,您不会有不同的状态。

Instead use State field and change it in setState method:而是使用State字段并在setState方法中更改它:

class CheckIcon extends StatefulWidget {
  final bool isCheck;

  const CheckIcon({
    Key? key,
    required this.isCheck, // here we set first value
  }) : super(key: key);

  @override
  State<CheckIcon> createState() => _CheckIconState();
}

class _CheckIconState extends State<CheckIcon> {
  late bool checked = widget.isCheck; // here we set first value and then change it in setState

  @override
  Widget build(BuildContext context) {
    return IconButton(
      splashRadius: 18.0,
      icon: Icon(
        checked
            ? Icons.check_circle_rounded
            : Icons.check_circle_outline_rounded,
        color: Colors.lightBlue,
        size: 20,
      ),
      onPressed: () {
        setState(() {
          checked = !checked;
        });
      },
    );
  }
}

Youu can't pass the static in construction please refer to the below doc.您在施工中无法通过 static 请参阅以下文档。

https://dart.dev/tools/diagnostic-messages?utm_source=dartdev&utm_medium=redir&utm_id=diagcode&utm_content=initializer_for_static_field#initializer_for_static_field https://dart.dev/tools/diagnostic-messages?utm_source=dartdev&utm_medium=redir&utm_id=diagcode&utm_content=initializer_for_static_field#initializer_for_static_field

instead you can use this if it's okay in your case.相反,如果您的情况没问题,您可以使用它。

 // ignore: must_be_immutable
 class CheckIcon extends StatefulWidget {
   bool isCheck;
 
   CheckIcon({
     Key? key,
     required this.isCheck,
   }) : super(key: key);
 
   @override
   State<CheckIcon> createState() => _CheckIconState();
 }
 
 class _CheckIconState extends State<CheckIcon> {
   @override
   Widget build(BuildContext context) {
     return IconButton(
       splashRadius: 18.0,
       icon: Icon(
         widget.isCheck ? Icons.check_circle_rounded : Icons.check_circle_outline_rounded,
         color: Colors.lightBlue,
         size: 20,
       ),
       onPressed: () {
         setState(() {
           widget.isCheck = !widget.isCheck;
         });
       },
     );
   }
 }

You cant use a static variable in constructor.您不能在构造函数中使用 static 变量。 You can do it like the following你可以这样做

class CheckIcon extends StatefulWidget {
  final bool? isCheck;


  const CheckIcon({
    Key? key,
required this.isCheck
  }) : super(key: key);

  @override
  State<CheckIcon> createState() => _CheckIconState();
}

class _CheckIconState extends State<CheckIcon> {
  @override
  Widget build(BuildContext context) {
    return IconButton(
      splashRadius: 18.0,
      icon: Icon(
        CheckIcon.isCheck!
            ? Icons.check_circle_rounded
            : Icons.check_circle_outline_rounded,
        color: Colors.lightBlue,
        size: 20,
      ),
      onPressed: () {
        setState(() {
          widget.isCheck = !widget.isCheck!;
        });
      },
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM