我需要将数据从页面类传递到同一页面上的小部件:
class PrePerfil extends StatefulWidget {
final String id_persona;
PrePerfil(this.id_persona, {Key key}): super(key: key);
@override
_PrePerfilState createState() => _PrePerfilState();
}
我需要将 person_id 的值传递给同一页面上另一个名为 PageOne 的类的小部件。
class PageOneState extends State<PageOne> {
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: widget.dataList.length,
itemBuilder: (context, index) {
return ExpansionTile(
key: PageStorageKey('${widget.dataList[index].id}'),
//title: Text(widget.dataList[index].title),
title: Text(id_persona), // Aqui está el error
在最后一行(标题)中,我需要使用 person_id,但我无法添加它。
请多多支持,非常感谢!
我添加了所有页面代码
import 'package:flutter/material.dart';
import 'package:curved_navigation_bar/curved_navigation_bar.dart';
class PrePerfil extends StatefulWidget {
final String id_persona;
PrePerfil(this.id_persona, {Key key}): super(key: key);
@override
_PrePerfilState createState() => _PrePerfilState();
}
class _PrePerfilState extends State<PrePerfil> {
final Key keyOne = PageStorageKey('pageOne');
final Key keyTwo = PageStorageKey('pageTwo');
int currentTab = 0;
PageOne one;
PageTwo two;
List<Widget> pages;
Widget currentPage;
List<Data> dataList;
final PageStorageBucket bucket = PageStorageBucket();
@override
void initState() {
one = PageOne(
key: keyOne,
dataList: dataList,
);
two = PageTwo(
key: keyTwo,
);
pages = [one, two];
currentPage = one;
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Persistance Example"),
),
body: PageStorage(
child: currentPage,
bucket: bucket,
),
bottomNavigationBar: CurvedNavigationBar(
onTap: (int index) {
setState(() {
currentTab = index;
currentPage = pages[index];
});
},
items: <Widget>[
Icon(Icons.account_circle, color: Colors.white,size: 30),
Icon(Icons.comment, color: Colors.white,size: 30),
//Icon(Icons.business_center, color: Colors.white,size: 30),
],
color: Colors.orangeAccent,
backgroundColor: Colors.white,
),
);
}
}
class PageOne extends StatefulWidget {
final List<Data> dataList;
final String id_persona;
PageOne({
Key key,
this.dataList, this.id_persona
}) : super(key: key);
@override
PageOneState createState() => PageOneState();
}
class PageOneState extends State<PageOne> {
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: widget.dataList.length,
itemBuilder: (context, index) {
return ExpansionTile(
key: PageStorageKey('${widget.dataList[index].id}'),
title: Text(widget.id_persona),
children: <Widget>[
Container(
color: index % 2 == 0 ? Colors.orange : Colors.limeAccent,
height: 100.0,
),
],
);
});
}
}
class PageTwo extends StatefulWidget {
PageTwo({Key key}) : super(key: key);
@override
PageTwoState createState() => PageTwoState();
}
class PageTwoState extends State<PageTwo> {
@override
Widget build(BuildContext context) {
return ListView.builder(
itemExtent: 250.0,
itemBuilder: (context, index) => Container(
padding: EdgeInsets.all(10.0),
child: Material(
elevation: 4.0,
borderRadius: BorderRadius.circular(5.0),
color: index % 2 == 0 ? Colors.cyan : Colors.deepOrange,
child: Center(
child: Text(index.toString()),
),
),
),
);
}
}
class Data {
final int id;
bool expanded;
final String title;
Data(this.id, this.expanded, this.title);
}
您可以使用 访问 del 变量的
StatefulWidget
值widget.id_persona
。首先确保您在小部件中接收到一个值
PrePerfil
。在 的方法
initState
中_PrePerfilState
,添加一个打印检查。如果绘制了您期望的值,那么您只需将其传递给 widget
PageOne
,因为我看到您没有将该参数传递给它,与initState
添加参数中的方式相同。有了它,您已经在小部件中收到它
PageOne
,使用widget.id_persona