implementation a.jar:1.0
implementation (b.jar:2.0) {
rename 'com.example.b' to 'com.example.standalone.b'
}
答案 0 :(得分:2)
我不知道这是否简单。但是对于简单的可重用的小部件,您可以将其放置在StatelessWidget
或StatefulWidget
内。
这里是例子:
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
内,我提供了两个名为name
和role
的String数据类型,以在调用小部件时存储我的数据。>
final String name; // provide a place for the input's data
final String role;
MyReusableWidget(this.name, this.role);
然后,我想将name
和role
变量放在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
,并在每个小部件上提供不同的name
和role
值。
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'),
],
),
结果:
就是这样。 您可以在上面存储任何类型的数据类型(String,int,double等)。
我希望这会有所帮助。