English 中文(简体)
• 在其自己的类别中捕获一只get虫?
原标题:Encapsulating a widget in its own class in flutter?
  • 时间:2018-05-27 16:51:25
  •  标签:
  • dart
  • flutter

I m 试图为我<代码>查询创建<0>。 虽然在列入清单档案时,该片人正在工作,但我不禁要问我如何把<条码><<>>> /条码>的植被纳入自己的类别档案。

Specifically, if the tile object does not take an argument, I can simply extend a stateless widget and call upon the build method to return a new tile object. But if the tile object is to be created with arguments (i.e. custom text), how do I pass this information along? Or would it be better to leave the widget in the listview class itself?

例:

class Tile extends StatelessWidget {

  @override
  Widget build(BuildContext context){
    return _tile(); //Error, How do i pass the arguments?
  }
  Widget _tile(String text, String time) {
    return new Align(
      child: new Container(
        // padding: EdgeInsets.all(5.0),
        ...
最佳回答

我认为,你可以简单地创建一种构造并使用它。

import  package:flutter/material.dart ;

class Tile extends StatelessWidget {
  final String text;
  final String time;

  /// Here is your constructor
  Tile({Key key, this.text, this.time});

  @override
  Widget build(BuildContext context) {
    return _buildTitle(this.text, this.time);
  }

  Widget _buildTitle(String text, String time) {
    return new Align(
        child: new Container(
          // padding: EdgeInsets.all(5.0),
        ));
  }
}
问题回答

Generally when creating a widget constructor you also add a Key and call super. Variables should also be marked final since widgets are immutable.

class Tile extends StatelessWidget {

  // make these final
  final String text;
  final String time;

  // constructor
  const Tile({Key key, this.text, this.time}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListTile(
      // ...
    );
  }
}

并称之为:

Tile(text:  hello , time:  5:30 );

创建习惯建筑者非常常见,在安乐施室里甚至会发生捷径。

  1. Write the names of your final variables.
  2. Put your cursor on them and press Option+Retern (or Alt+Enter).

enter image description here

图像来源here





相关问题
Flutter App cannot be installed on Android TV

I m building a Flutter app that should support Android TV and Mobile devices. Despite Google Play shows that it is supported, I cannot install app on my MiBox device. While trying to install it, both ...

Moving the icon button to the right causes rendering issues

I am trying to align the icon button to the right of the Text field, I tried the row which made the icon button not centered vertically and now I am trying out the Stack widget. Here is my code - ...

热门标签