在flutter中创建可重复使用的小部件的简单方法是什么?

时间:2020-07-31 06:44:10

标签: flutter

implementation a.jar:1.0
implementation (b.jar:2.0) {
   rename 'com.example.b' to 'com.example.standalone.b'
}

1 个答案:

答案 0 :(得分:2)

我不知道这是否简单。但是对于简单的可重用的小部件,您可以将其放置在StatelessWidgetStatefulWidget内。

这里是例子:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());


class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Column(
          children: <Widget>[
            MyReusableWidget('Nikola Tesla', 'Owner'), //Input the name and role variable when you call the widget
            MyReusableWidget('Albert Einstein', 'Developer'),
            MyReusableWidget('Isaac Newton', 'Technician'),
          ],
        ),
      ),
    );
  }
}


class MyReusableWidget extends StatelessWidget {
  final String name; // provide a place for the input's data
  final String role;

  MyReusableWidget(this.name, this.role);

  @override
  Widget build(BuildContext context) {
    return Container(
          child: Column(
            children: [
              Container(
                alignment: Alignment.topLeft,
                padding: EdgeInsets.only(left: 10.0),
                child: Text(
                  name, // This is where you place your 'name' data
                  style: TextStyle(
                      fontWeight: FontWeight.bold, color: Colors.black),
                ),
              ),
              Container(
                alignment: Alignment.topLeft,
                padding: EdgeInsets.all(10.0),
                child: Text(
                  role, // This is where you place your 'role' data
                  style: TextStyle(
                      fontWeight: FontWeight.bold, color: Colors.grey),
                ),
              ),
            ],
          ),
        );
  }
}

我正在创建一个名为MyReusableWidget的小部件。我将在我的MyApp内将该小部件调用3次。然后,每个小部件都应提供不同的名称和角色。

因此,在我的MyReusableWidget内,我提供了两个名为namerole的String数据类型,以在调用小部件时存储我的数据。

  final String name; // provide a place for the input's data
  final String role;

  MyReusableWidget(this.name, this.role);

然后,我想将namerole变量放在Text小部件内:

                child: Text(
                  name, // This is where you place your 'name' data
                  style: TextStyle(
                      fontWeight: FontWeight.bold, color: Colors.black),
                ),

和:

               child: Text(
                  role, // This is where you place your 'role' data
                  style: TextStyle(
                      fontWeight: FontWeight.bold, color: Colors.grey),
                ),

然后,在我的MyApp小部件中,我可以随意调用MyReusableWidget,并在每个小部件上提供不同的namerole值。

Column(
          children: <Widget>[
            MyReusableWidget('Nikola Tesla', 'Owner'), //Input the name and role variable when you call the widget
            MyReusableWidget('Albert Einstein', 'Developer'),
            MyReusableWidget('Isaac Newton', 'Technician'),
          ],
        ),

结果:

Result

就是这样。 您可以在上面存储任何类型的数据类型(String,int,double等)。

我希望这会有所帮助。