简体   繁体   English

什么意思 ”<Widget> [~]&quot; in dart? 带列表括号的箭头括号(颤动)

[英]What meaning "<Widget>[~]" in dart? arrow brackets with list brackets(flutter)

i am newbie in dart.我是飞镖新手。

i am studying flutter by reading docs.我正在通过阅读文档来学习颤振。

in here这里

i see [blah blah].我明白了[等等等等]。

i know generic (Cpp) but, i dont know meaning it.我知道通用 (Cpp) 但是,我不知道是什么意思。

could you explain it to me?你能给我解释一下吗?

I'm new to dart as well, by playing around on DartPad.我也是 dart 新手,在 DartPad 上玩过。
It seems that putting a type (ie Int, String) before a Set {'hello', 'world'} or看来,把一个类型(ie Int, String)一个集之前{'hello', 'world'}
a List ['hello', 'world'] will make it like "strict" , making it type safe.一个 List ['hello', 'world']将使它像"strict" ,使其类型安全。 (ref: https://dart.dev/guides/language/type-system ) (参考: https : //dart.dev/guides/language/type-system

Like if you do <String>['hello', 'world'] only string will be accepted on the list,就像如果你做<String>['hello', 'world']列表中只接受字符串,
and will throw an error if you do <String>[1,2,3] .如果您执行<String>[1,2,3] ,则会抛出错误。

So in <Widget>[Column(), Rows()] if you put non-widget class or object in the set it will throw an error like if you do <Widget>['hello', 'world'] .因此,在<Widget>[Column(), Rows()]如果您将非小部件类或对象放入集合中,它将抛出一个错误,就像您执行<Widget>['hello', 'world']

Try the code below on DartPad https://dartpad.dev/215ba63265350c02dfbd586dfd30b8c3?null_safety=true .在 DartPad https://dartpad.dev/215ba63265350c02dfbd586dfd30b8c3?null_safety=true上试试下面的代码。 Note that it will throw an error since I put a string on Widget type Set on line 3.请注意,它会抛出错误,因为我在第 3 行的 Widget 类型 Set 上放置了一个字符串。

import 'package:flutter/material.dart';

void main() {
  
  print(<String>['Hello',' ', 'World']);
  
  print(<Widget>[Column(), 'hello']); //error will throw since it will only accept <Widget>
  
  print(<Widget>[Column(),Row()]);
  
}

Also, all those items inside the List are Widgets class and inside those ei "height: 36.0" are widget properties/methods, it's like nested Widgets, Flutter are full of them.此外,List 中的所有项目都是 Widgets 类,而在那些 ei "height: 36.0" 中是小部件属性/方法,就像嵌套的 Widgets,Flutter 中充满了它们。

class MyButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        print('MyButton was tapped!');
      },
      child: Container( // Container is a widget, child is a GestureDetector props.
        height: 36.0, //height is a Container props
        padding: const EdgeInsets.all(8.0), // EdgeInsets also a widget
        margin: const EdgeInsets.symmetric(horizontal: 8.0),
        decoration: BoxDecoration( // BoxDecoration also a widget
          borderRadius: BorderRadius.circular(5.0),
          color: Colors.lightGreen[500],
        ),
        child: Center(
          child: Text('Engage'),
        ),
      ),
    );
  }
}

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

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