I need to pass data from a page class to a widget on the same page:
class PrePerfil extends StatefulWidget {
final String id_persona;
PrePerfil(this.id_persona, {Key key}): super(key: key);
@override
_PrePerfilState createState() => _PrePerfilState();
}
I need to pass the value of person_id to the widget of another class called PageOne on the same page.
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
In the last line (title) I need to use the person_id, but I can't add it.
Please your support, thank you very much!
I add all the page code
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);
}
You can access the value of the del variable
StatefulWidget
usingwidget.id_persona
.First make sure you are receiving a value in your widget
PrePerfil
.In the method
initState
of_PrePerfilState
, add a print to check.If the value you expect is painted, then you only need to pass it to the widget
PageOne
, since I see that you do not pass that parameter, in the same way within theinitState
add the parameter.With that you would already receive it in the widget
PageOne
, usingwidget.id_persona