【问题标题】:Override widget changing child widget in flutter在颤动中覆盖小部件更改子小部件
【发布时间】:2021-06-27 12:17:43
【问题描述】:

基本上我有两个看起来一样的按钮小部件,唯一的区别是一个带有图标,另一个没有。

这是我的带有图标的小部件类

class ButtonElevationWithIcon extends StatelessWidget {
  final String buttonText;
  final Function onPressedButton;
  final Icon buttonIcon;
  final double height;

  ButtonElevationWithIcon(
      {@required this.buttonIcon,
      @required this.buttonText,
      this.height = 60,
      @required this.onPressedButton});

  @override
  Widget build(BuildContext context) {
    return Container(
        margin: EdgeInsets.symmetric(vertical: 5.0, horizontal: 40),
        decoration: ShapeDecoration(
          shape: StadiumBorder(
            side: BorderSide(width: 1.0),
          ),
          color: Colors.redAccent,
          shadows: <BoxShadow>[
            BoxShadow(
              color: Colors.redAccent.withOpacity(0.2),
              blurRadius: this.height / 5,
              offset: Offset(0, this.height / 10),
            ),
          ],
        ),
        child: TextButton.icon(
            label: Center(
              child: Padding(
                padding: const EdgeInsets.all(8.0),
                child: Stack(
                  children: <Widget>[
                    Text(
                      this.buttonText,
                      style: TextStyle(
                        fontFamily: 'Poppins',
                        fontWeight: FontWeight.bold,
                        fontSize: Constants.kfontSizeButton,
                        letterSpacing: Constants.kletterSpacing,
                        color: Colors.yellowAccent,
                      ),
                    ),
                    Text(
                      this.buttonText,
                      style: TextStyle(
                        fontFamily: 'Poppins',
                        fontWeight: FontWeight.bold,
                        fontSize: Constants.kfontSizeButton,
                        letterSpacing: Constants.kletterSpacing,
                        foreground: Paint()
                          ..style = PaintingStyle.stroke
                          ..strokeWidth = 1.5
                          ..color = Colors.black,
                      ),
                    ),
                  ],
                ),
              ),
            ),
            icon: this.buttonIcon,
            style: ButtonStyle(
                foregroundColor: MaterialStateProperty.all<Color>(Colors.white),
                backgroundColor:
                    MaterialStateProperty.all<Color>(Color(0xFFfa0002)),
                shape: MaterialStateProperty.all<RoundedRectangleBorder>(
                    RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(40.0),
                ))),
            onPressed: onPressedButton));
  }
}

没有它

class ButtonElevation extends StatelessWidget {
  final double height;

  final String buttonText;
  final Function onPressedButton;

  ButtonElevation(
      {@required this.buttonText,
      this.height = 100,
      @required this.onPressedButton});

  @override
  Widget build(BuildContext context) {
    return Container(
        margin: EdgeInsets.symmetric(vertical: 5.0, horizontal: 40),
        height: this.height,
        decoration: ShapeDecoration(
          shape: StadiumBorder(
            side: BorderSide(width: 1.0),
          ),
          color: Colors.redAccent,
          shadows: <BoxShadow>[
            BoxShadow(
              color: Colors.redAccent.withOpacity(0.2),
              blurRadius: this.height / 5,
              offset: Offset(0, this.height / 10),
            ),
          ],
        ),
        child: TextButton(
            child: Center(
              child: Stack(
                children: <Widget>[
                  Text(

                    this.buttonText,
                    style: TextStyle(
                      fontFamily: 'Poppins',
                      fontWeight: FontWeight.bold,
                      fontSize: Constants.kfontSizeButton,
                      letterSpacing: Constants.kletterSpacing,
                      color: Colors.yellowAccent,
                    ),
                  ),
                  Text(
                    this.buttonText,
                    style: TextStyle(
                      fontFamily: 'Poppins',
                      fontWeight: FontWeight.bold,
                      fontSize: Constants.kfontSizeButton,
                      letterSpacing: Constants.kletterSpacing,
                      foreground: Paint()
                        ..style = PaintingStyle.stroke
                        ..strokeWidth = 1.5
                        ..color = Colors.black,
                    ),
                  ),
                ],
              ),
            ),
            style: ButtonStyle(
                foregroundColor: MaterialStateProperty.all<Color>(Colors.white),
                backgroundColor:
                    MaterialStateProperty.all<Color>(Color(0xFFfa0002)),
                shape: MaterialStateProperty.all<RoundedRectangleBorder>(
                    RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(40.0),
                ))),
            onPressed: onPressedButton));
  }
}

我认为应该有一种方法可以不重复代码两次。我只想将子项从 TextButton.icon 更改为 TextButton。

有什么想法吗?

【问题讨论】:

    标签: flutter flutter-widget


    【解决方案1】:

    试试这样的:

    class ButtonElevation extends StatelessWidget {
      final bool hasIcon;
      // Your other properties.
    
      ButtonElevation({
        this.hasIcon = true,
        // Your other properties.
      });
    
      @override
      Widget build(BuildContext context) {
        final child = RestOfYourWidget();
        return hasIcon ? TextButton.icon(label: child): TextButton(child: child);
      }
    }
    

    【讨论】:

      【解决方案2】:

      您可以创建一个单独的类,其中图标字段是可选的。然后如果图标字段不为空,则使用TextButton.icon,否则使用TextButton

      考虑下面的代码

      class ButtonElevationWithIcon extends StatelessWidget {
        final String buttonText;
        final Function onPressedButton;
        final Icon buttonIcon;
        final double height;
      
        ButtonElevationWithIcon(
            {this.buttonIcon,
            @required this.buttonText,
            this.height = 60,
            @required this.onPressedButton});
      
        Widget _label = Center(
                        child: Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: Stack(
                            children: <Widget>[
                              Text(
                                this.buttonText,
                                style: TextStyle(
                                  fontFamily: 'Poppins',
                                  fontWeight: FontWeight.bold,
                                  fontSize: Constants.kfontSizeButton,
                                  letterSpacing: Constants.kletterSpacing,
                                  color: Colors.yellowAccent,
                                ),
                              ),
                              Text(
                                this.buttonText,
                                style: TextStyle(
                                  fontFamily: 'Poppins',
                                  fontWeight: FontWeight.bold,
                                  fontSize: Constants.kfontSizeButton,
                                  letterSpacing: Constants.kletterSpacing,
                                  foreground: Paint()
                                    ..style = PaintingStyle.stroke
                                    ..strokeWidth = 1.5
                                    ..color = Colors.black,
                                ),
                              ),
                            ],
                          ),
                        ),
                      );
      
          ButtonStyle = ButtonStyle(
                          foregroundColor: MaterialStateProperty.all<Color>(Colors.white),
                          backgroundColor:
                              MaterialStateProperty.all<Color>(Color(0xFFfa0002)),
                          shape: MaterialStateProperty.all<RoundedRectangleBorder>(
                              RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(40.0),
                          ),
           ),
          );
      
        @override
        Widget build(BuildContext context) {
          return Container(
              margin: EdgeInsets.symmetric(vertical: 5.0, horizontal: 40),
              decoration: ShapeDecoration(
                shape: StadiumBorder(
                  side: BorderSide(width: 1.0),
                ),
                color: Colors.redAccent,
                shadows: <BoxShadow>[
                  BoxShadow(
                    color: Colors.redAccent.withOpacity(0.2),
                    blurRadius: this.height / 5,
                    offset: Offset(0, this.height / 10),
                  ),
                ],
              ),
              child: this.buttonIcon != null ? TextButton.icon(
                  label: _label,
                  icon: this.buttonIcon,
                  style: _buttonStyle,
                  onPressed: onPressedButton,
                 ) : TextButton(
                  label: _label,
                  style: _buttonStyle,
                  onPressed: onPressedButton,
                 ),
                );
            }
          }
      

      我们可以将labelstyle 保存到单独的变量中以避免重复。 检查icon是否为空,使用TextButton.iconTextButton

      【讨论】:

      • 谢谢!代码工作正常。我只需要将 Widget _label 修改为方法
      猜你喜欢
      • 2021-07-12
      • 2019-10-29
      • 1970-01-01
      • 2019-09-14
      • 1970-01-01
      • 2021-04-01
      • 2010-12-09
      • 2020-03-03
      • 2021-08-13
      相关资源
      最近更新 更多