flutter - 在颤振中创建可重用小部件的简单方法是什么?
问题描述
Container(
child: Column(
children: <Widget>[
Container(
alignment: Alignment.topLeft,
padding: EdgeInsets.only(left: 10.0),
child: Text("Random Text",
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black)),
),
Container(
alignment: Alignment.topLeft,
padding: EdgeInsets.all(10.0),
child: Text("Owner",
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.grey)),
),
],
),
),
解决方案
我不知道这是否是一个简单的方法。但是对于一个简单的可重用小部件,您可以将小部件放在 aStatelessWidget
或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
,我提供了两种调用的 String 数据类型name
,并在我调用小部件role
时存储我的数据。
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 等)。
我希望它会有所帮助。
推荐阅读
- javascript - Tern 函数参数:如何用键指定对象?
- android - 打开 OneSignal URL 到另一个活动的 WebView
- javascript - 将 html 表格导出到 XLSX 时如何设置单元格子项的样式
- regex - 使用正则表达式将字符串分成两组并仅显示最后一组
- erlang - erlang - 监视器将信息发送到 shell 而不是 gen_server
- c# - 从表单接受属性中排除自动完成文本框?
- javascript - 模仿使用 module.exports 的模块的结构
- android - xamarin android googlemap 方向
- amazon-web-services - AWS 上的 Kubernetes HTTP 到 HTTPS 重定向,ELB 终止 SSL
- c# - XML 命名空间“使用:Microsoft.Toolkit.Uwp.UI.Controls”中的未知类型“UniformGrid”