简体   繁体   English

(颤抖)我怎样才能在课堂上争论?

[英](flutter) How can I get argument in class?

I'm new to flutter, and I heard that creating widgets as classes is better.我是 flutter 的新手,我听说将小部件创建为类更好。 So I'm trying to change functions to classes.所以我试图将函数更改为类。

Widget Head(Size size){
  return Container(
    height: size.height*0.3 + 100,
    decoration: BoxDecoration(
        gradient: LinearGradient(
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
            colors: [
              Color(0xff3D1472),
              Color(0xff771887),
            ]
        )
    ),
  );
}

the code above is one of the functions I want to change to stateless class.上面的代码是我想更改为无状态类的功能之一。 And I tried like this,我像这样试过,

class Head extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    
    return Container(
      height: size.height*0.3 + 100,
      decoration: BoxDecoration(
          gradient: LinearGradient(
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
              colors: [
                Color(0xff3D1472),
                Color(0xff771887),
              ]
          )
      ),
    );
  }
}

But I still don't know where I should get that 'size'.但我仍然不知道我应该从哪里得到那个“大小”。 How can I do that?我怎样才能做到这一点? Thank you for reading, and I'll wait for your advice!感谢您的阅读,我会等待您的建议!

You need to pass the arguments in the constructor, for example:您需要在构造函数中传递参数,例如:

class Head extends StatelessWidget {
  Head({this.size});

  final int size;

  @override
  Widget build(BuildContext context) {
    
    return Container(
      height: size.height*0.3 + 100,
      decoration: BoxDecoration(
          gradient: LinearGradient(
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
              colors: [
                Color(0xff3D1472),
                Color(0xff771887),
              ]
          )
      ),
    );
  }
}

And then when you call it, you can do:然后当你调用它时,你可以这样做:

Head(size : 1)

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

相关问题 我怎样才能在flutter中获得android drawable - How can I get android drawables in flutter 如何在颤振 3 的同一个构造函数中同时使用必需参数和简单参数? - How can I use both required and simple argument in same constructor in flutter 3? 如何从 firebase 获取值并将其放入 Flutter 中的文本中 - How can I get a value from firebase and and put it in a text in Flutter 我怎样才能获得任何唯一的 id flutter android 和 ios - How can I get any unique id flutter android and ios 如何在 flutter 中获得 android 蜂窝数据使用情况? - How can i get android cellular data usage in flutter? 如何使用 Flutter 从 Firebase 获取当前活跃用户? - How I can get current active users from Firebase with Flutter? Flutter:如果我想导航到详细信息页面,我会在 flutter 中收到无效参数错误 - Flutter: i get invalid argument(s) Error in flutter if i want to navigate to detail page 我如何从另一个班级获得价值 - How can i get a value to class from another class Flutter:如何在 Flutter 2.5.3 中获取 device_id 和 device_token? - Flutter: how can i get device_id and device_token in flutter 2.5.3? 如何编写以片段类作为参数的方法,使我可以创建该片段的新实例? - How can I write a method with a fragment class as an argument that will allow me to create a new instance of that fragment?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM