我創建了一個可以隨時呼叫的擴展磁貼卡。我已將擴展磁貼創建為一個類,因此我可以隨時呼叫它并在多個頁面上使用它,而不必為每個新頁面重復代碼。
這是下面的代碼
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
class ExpandedTileCard extends StatelessWidget {
final String toptitle;
final String bottomnumber;
const ExpandedTileCard({
Key key,
this.toptitle,
this.bottomnumber,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.symmetric(vertical:5.0),
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Container(
padding: EdgeInsets.only(left: 0.0,bottom: 20.0),
decoration: new BoxDecoration(
border: new Border(
bottom: new BorderSide(
color: Colors.black
)
)
),
child: new Row(
children: [
new Expanded(
flex:1,
child: new SvgPicture.asset(
'assets/icons/phone-call.svg',
height: 30,
),
),
new Expanded(
flex:9,
child: new Container(
padding: EdgeInsets.only(left: 15.0),
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ExpansionTile(
title: Text(
toptitle,
style: new TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black,
),
textAlign: TextAlign.left,
),
children: [],
),
new Text(
(bottomnumber)
),
],
),
),
)
],
),
),
],
),
);
}
}
然后我像這樣在我的頁面中呼叫擴展磁貼。
ExpandedTileCard(
toptitle: "recharge",
bottomnumber: "*129939201#",
),
我的問題是。我如何將子小部件添加到擴展磁貼類并隨時在我的頁面中呼叫它。與我可以編輯頂部標題和底部編號的方式相同。我不想創建固定的子部件,因為某些擴展圖塊的子部件數量與其他部件不同。請幫忙。
uj5u.com熱心網友回復:
你可以這樣做
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
class ExpandedTileCard extends StatelessWidget {
final String toptitle;
final String bottomnumber;
final List<Widget> children;
const ExpandedTileCard({
Key key,
this.toptitle,
this.children,
this.bottomnumber,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.symmetric(vertical:5.0),
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Container(
padding: EdgeInsets.only(left: 0.0,bottom: 20.0),
decoration: new BoxDecoration(
border: new Border(
bottom: new BorderSide(
color: Colors.black
)
)
),
child: new Row(
children: [
new Expanded(
flex:1,
child: new SvgPicture.asset(
'assets/icons/phone-call.svg',
height: 30,
),
),
new Expanded(
flex:9,
child: new Container(
padding: EdgeInsets.only(left: 15.0),
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ExpansionTile(
title: Text(
toptitle,
style: new TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black,
),
textAlign: TextAlign.left,
),
children: children,
),
new Text(
(bottomnumber)
),
],
),
),
)
],
),
),
],
),
);
}
}
并像這樣使用它
ExpandedTileCard(
toptitle: "recharge",
children: [
Container(),
SizedBox(),
],
bottomnumber: "*129939201#",
),
uj5u.com熱心網友回復:
做這樣的事情,
class ExpandedTileCard extends StatelessWidget {
final String toptitle;
final String bottomnumber;
final Widget child; // or final List<Widget> children
const ExpandedTileCard({
Key key,
this.toptitle,
this.bottomnumber,
this.child,
}) : super(key: key);
}
...
children: [child] // or children: children
現在,當您傳遞孩子時,將其作為單個小部件傳遞或作為串列傳遞
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/330999.html
下一篇:'Future<List<Appointment>>'不是型別轉換中型別'List<Appointment>'的子型別
