如何使用 Flutter 中的键遍历存储在我的共享首选项中的数据
How can I Loop through data that is stored in my sharedpreference with a key in - Flutter
这是帮助我从共享偏好存储中获取数据的函数:
var user;
var userData;
var anchors;
@override
void initState() {
_getUserAnchor();
super.initState();
}
_getUserAnchor() async{
SharedPreferences localStorage = await SharedPreferences.getInstance();
var userJson = localStorage.getString('loginRes');
user = json.decode(userJson);
anchors = user['Anchors'];
print(anchors);
setState(() {
userData = anchors;
});
}
这是应该显示数据的小部件:
@override
Widget build(BuildContext context) {
return Scaffold(
drawer: NavDrawer(),
appBar: AppBar(
title: Text('Dashboard'),
iconTheme: IconThemeData(color: Colors.white),
backgroundColor: Colors.green,
),
backgroundColor: Colors.white,
body: Container(
padding: const EdgeInsets.fromLTRB(10, 30, 10, 10),
child: ListView(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Icon(Icons.card_membership,
size: 35, color: Colors.orange[400]),
Text(
'Assigned Anchors',
style: TextStyle(color: Colors.orange[400], fontSize: 25),
),
Icon(Icons.notifications_none, size: 35, color: Colors.white)
],
),
Column(
children: persons.map((p) {
return personDetailCard(p); //this is where I want to display the data
}).toList())
],
),
),
);
}
这是 json 数组在成功登录后在共享首选项中的查找方式:
{
"Anchors": [
{
"Oid": 11,
"Name": "MAIZE ASSOCIATION OF NIGERIA",
"Acronym": "MAAN",
"DistributionCentres": [
{
"Oid": 11,
"Name": "Logo Centre (Zone A)",
"Address": "Private Warehouse, Ugba, Logo LGA"
},
{
"Oid": 12,
"Name": "Makurdi Centre (Zone B)",
"Address": "Ministry of Agric, Makurdi "
},
{
"Oid": 13,
"Name": "Oturkpo Centre (Zone C)",
"Address": "Private Warehouse, Oturkpo"
},
{
"Oid": 15,
"Name": "Borno MAAN centre",
"Address": "Bolori Store, Flavour Mill, Behind Vita Foam, Maiduguri"
}
]
},
{
"Oid": 2,
"Name": "MAIZE GROWERS, PROCESSORS AND MARKETERS ASSOCIATION OF NIGERIA",
"Acronym": "MAGPAMAN",
"DistributionCentres": [
{
"Oid": 2,
"Name": "Guma Centre",
"Address": "P 32, 2nd Avenue Federal Housing Estate, N/Bank, Makurdi"
},
{
"Oid": 3,
"Name": "Logo Centre",
"Address": "Terhemen Akema Storage Facility, Ugba, Logo LGA"
},
{
"Oid": 5,
"Name": "Oturkpo Centre",
"Address": "Grain Store, Lower Benue Okele Project, Otukpo"
},
{
"Oid": 6,
"Name": "Gboko Centre",
"Address": "K3 New Road, Opposite former coca cola plant. Solar Schools Street, Gboko"
},
{
"Oid": 7,
"Name": "Gwer East Centre",
"Address": "Ahua Shardye's Warehouse, Behind Sylkan Filling Station, Ikpayongo , G/East LGA"
},
{
"Oid": 8,
"Name": "Kwande Centre",
"Address": "KM 3, Adagi Road, Adikpo"
},
{
"Oid": 9,
"Name": "Ohimini Centre",
"Address": "Ajoga Oglewu, Ohimini"
},
{
"Oid": 10,
"Name": "Oju Centre",
"Address": "Behind Town Hall, Ohuhu owo, Oju LGA"
}
]
}
]
}
所以我想显示名称、首字母缩略词并计算 DistributionCentres,但我不知道如何去做。有人可以帮我吗?我正在使用共享首选项,因为有分配给登录用户的数字锚点。最初,我能够通过在文件中对数组进行硬编码来做到这一点,但我无法通过共享首选项实现相同的目的。请问有人可以帮助我吗?请。如果可行,我将摆脱硬编码的 Person Array 对象。
下面是整体页面浏览量:
import 'package:erg_app/StockPage.dart';
import 'package:erg_app/models/users_model.dart';
import 'package:flutter/material.dart';
import 'package:erg_app/Widgets/nav-drawer.dart';
import 'package:erg_app/models/eopmodel.dart';
import 'package:erg_app/StartScan.dart';
import 'dart:convert';
import 'dart:async';
import 'package:shared_preferences/shared_preferences.dart';
void main() => runApp(MaterialApp(
home: EopPage(),
));
class EopPage extends StatefulWidget {
@override
_MyHomeState createState() => _MyHomeState();
}
class _MyHomeState extends State<EopPage> {
List<Person> persons = [
Person(
name: 'RIFAN',
profileImg: 'assets/images/user.png',
allocated_farmers: "300",
validated_farmers: "345",
non_validated_farmers: "120",
daily_inventory_status: "Completed",
distribution_centers: "100"),
Person(
name: 'MAAN',
profileImg: 'assets/images/user.png',
allocated_farmers: "230",
validated_farmers: "195",
non_validated_farmers: "110",
daily_inventory_status: "Incompleted",
distribution_centers: "70"),
Person(
name: 'COPMAN',
profileImg: 'assets/images/user.png',
allocated_farmers: "560",
validated_farmers: "45",
non_validated_farmers: "780",
daily_inventory_status: "Completed",
distribution_centers: "40"),
];
Widget personDetailCard(Person) {
return Container(
padding: const EdgeInsets.all(10.0),
////////////// 1st card///////////
child: Card(
elevation: 4.0,
color: Colors.grey[100],
margin: EdgeInsets.only(left: 10, right: 10, top: 20, bottom: 10),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
child: Container(
padding: EdgeInsets.only(left: 15, top: 20, bottom: 10),
width: MediaQuery.of(context).size.width,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: 50.0,
height: 50.0,
decoration: new BoxDecoration(
shape: BoxShape.circle,
image: new DecorationImage(
fit: BoxFit.cover,
image: AssetImage(Person.profileImg)))),
),
SizedBox(
width: 20,
),
Text(
Person.name,
textAlign: TextAlign.center,
style: TextStyle(
color: Color(0xFF9b9b9b),
fontSize: 20,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
),
),
],
),
Container(width: 10),
Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 10, top: 10),
child: Text(
'Allocated Farmers:',
textAlign: TextAlign.left,
style: TextStyle(
color: Color(0xFF9b9b9b),
fontSize: 14.0,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
),
),
),
Padding(
padding: const EdgeInsets.only(left: 70, top: 12),
child: Text(
Person.allocated_farmers,
textAlign: TextAlign.left,
style: TextStyle(
color: Colors.grey[700],
fontSize: 14.0,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
),
),
),
],
),
Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 10, top: 10),
child: Text(
'Validated Farmers:',
textAlign: TextAlign.left,
style: TextStyle(
color: Color(0xFF9b9b9b),
fontSize: 14.0,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
),
),
),
Padding(
padding: const EdgeInsets.only(left: 70, top: 12),
child: Text(
Person.validated_farmers,
textAlign: TextAlign.left,
style: TextStyle(
color: Colors.grey[700],
fontSize: 14.0,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
),
),
),
],
),
Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 10, top: 10),
child: Text(
'Non Validated Farmers:',
textAlign: TextAlign.left,
style: TextStyle(
color: Color(0xFF9b9b9b),
fontSize: 14.0,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
),
),
),
Padding(
padding: const EdgeInsets.only(left: 40, top: 12),
child: Text(
Person.non_validated_farmers,
textAlign: TextAlign.left,
style: TextStyle(
color: Colors.grey[700],
fontSize: 14.0,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
),
),
),
],
),
Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 10, top: 10),
child: Text(
'Distribution Centers:',
textAlign: TextAlign.left,
style: TextStyle(
color: Color(0xFF9b9b9b),
fontSize: 14.0,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
),
),
),
Padding(
padding: const EdgeInsets.only(left: 60, top: 12),
child: Text(
Person.distribution_centers,
textAlign: TextAlign.left,
style: TextStyle(
color: Colors.grey[700],
fontSize: 14.0,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
),
),
),
],
),
Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 10, top: 10),
child: Text(
'Daily Inventory Status:',
textAlign: TextAlign.left,
style: TextStyle(
color: Color(0xFF9b9b9b),
fontSize: 14.0,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
),
),
),
Padding(
padding: const EdgeInsets.only(left: 50, top: 12),
child: Text(
Person.daily_inventory_status,
textAlign: TextAlign.left,
style: TextStyle(
color: Person.daily_inventory_status == 'Completed'
? Colors.green
: Colors.red,
fontSize: 14.0,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
),
),
),
],
),
Container(
height: 20,
),
Row(
children: <Widget>[
/////////// Buttons /////////////
Padding(
padding: const EdgeInsets.all(10.0),
child: Person.daily_inventory_status == 'Completed'
? FlatButton(
child: Padding(
padding: EdgeInsets.only(
top: 8, bottom: 8, left: 10, right: 10),
child: Text(
'Validate Farmer',
textDirection: TextDirection.ltr,
style: TextStyle(
color: Colors.white,
fontSize: 15.0,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
),
),
),
color: Colors.green,
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(20.0)),
onPressed: () {
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => StartScanPage()));
// Edit()was here
},
)
: FlatButton(
child: Padding(
padding: EdgeInsets.only(
top: 8, bottom: 8, left: 10, right: 8),
child: Text(
'Take Inventory',
textDirection: TextDirection.ltr,
style: TextStyle(
color: Colors.white,
fontSize: 15.0,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
),
),
),
color: Colors.blueGrey,
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(20.0)),
onPressed: () {
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => StockPage()));
},
),
),
/////////// End of Buttons /////////////
],
),
],
),
),
),
);
}
var user;
var userData;
var anchors;
@override
void initState() {
_getUserAnchor();
super.initState();
}
_getUserAnchor() async{
SharedPreferences localStorage = await SharedPreferences.getInstance();
var userJson = localStorage.getString('loginRes');
user = json.decode(userJson);
anchors = user['Anchors'];
print(anchors);
setState(() {
userData = anchors;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
drawer: NavDrawer(),
appBar: AppBar(
title: Text('Dashboard'),
iconTheme: IconThemeData(color: Colors.white),
backgroundColor: Colors.green,
),
backgroundColor: Colors.white,
body: Container(
padding: const EdgeInsets.fromLTRB(10, 30, 10, 10),
child: ListView(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Icon(Icons.card_membership,
size: 35, color: Colors.orange[400]),
Text(
'Assigned Anchors',
style: TextStyle(color: Colors.orange[400], fontSize: 25),
),
Icon(Icons.notifications_none, size: 35, color: Colors.white)
],
),
Column(
children: persons.map((p) {
return personDetailCard(p);
}).toList())
],
),
),
);
}
}
这里有一个例子可以让你实现你的场景..
var user;
var userData;
List anchors = [];
_getUserAnchor() async{
SharedPreferences localStorage = await SharedPreferences.getInstance();
user = userJson;
setState(() {
anchors = user['Anchors'];
});
print(anchors);
setState(() {
userData = anchors;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('test'),),
body: Column(
children: <Widget>[
ListView.builder(
shrinkWrap: true,
itemCount: anchors.length,
itemBuilder: (BuildContext context, int i){
return Padding(
padding: const EdgeInsets.all(8.0),
child: Card(
child: Column(
children: <Widget>[
Text(anchors[i]['Name']),
Text(anchors[i]['Oid'].toString()),
],
),
),
);
})
],
),
);
}
这里我没有从共享偏好中获取数据我有等于你的 json 到 userJson,你可以使用你相同的步骤从共享偏好中获取数据并跟进,所以你可以建立卡片列表..希望这对您有所帮助...
这是帮助我从共享偏好存储中获取数据的函数:
var user;
var userData;
var anchors;
@override
void initState() {
_getUserAnchor();
super.initState();
}
_getUserAnchor() async{
SharedPreferences localStorage = await SharedPreferences.getInstance();
var userJson = localStorage.getString('loginRes');
user = json.decode(userJson);
anchors = user['Anchors'];
print(anchors);
setState(() {
userData = anchors;
});
}
这是应该显示数据的小部件:
@override
Widget build(BuildContext context) {
return Scaffold(
drawer: NavDrawer(),
appBar: AppBar(
title: Text('Dashboard'),
iconTheme: IconThemeData(color: Colors.white),
backgroundColor: Colors.green,
),
backgroundColor: Colors.white,
body: Container(
padding: const EdgeInsets.fromLTRB(10, 30, 10, 10),
child: ListView(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Icon(Icons.card_membership,
size: 35, color: Colors.orange[400]),
Text(
'Assigned Anchors',
style: TextStyle(color: Colors.orange[400], fontSize: 25),
),
Icon(Icons.notifications_none, size: 35, color: Colors.white)
],
),
Column(
children: persons.map((p) {
return personDetailCard(p); //this is where I want to display the data
}).toList())
],
),
),
);
}
这是 json 数组在成功登录后在共享首选项中的查找方式:
{
"Anchors": [
{
"Oid": 11,
"Name": "MAIZE ASSOCIATION OF NIGERIA",
"Acronym": "MAAN",
"DistributionCentres": [
{
"Oid": 11,
"Name": "Logo Centre (Zone A)",
"Address": "Private Warehouse, Ugba, Logo LGA"
},
{
"Oid": 12,
"Name": "Makurdi Centre (Zone B)",
"Address": "Ministry of Agric, Makurdi "
},
{
"Oid": 13,
"Name": "Oturkpo Centre (Zone C)",
"Address": "Private Warehouse, Oturkpo"
},
{
"Oid": 15,
"Name": "Borno MAAN centre",
"Address": "Bolori Store, Flavour Mill, Behind Vita Foam, Maiduguri"
}
]
},
{
"Oid": 2,
"Name": "MAIZE GROWERS, PROCESSORS AND MARKETERS ASSOCIATION OF NIGERIA",
"Acronym": "MAGPAMAN",
"DistributionCentres": [
{
"Oid": 2,
"Name": "Guma Centre",
"Address": "P 32, 2nd Avenue Federal Housing Estate, N/Bank, Makurdi"
},
{
"Oid": 3,
"Name": "Logo Centre",
"Address": "Terhemen Akema Storage Facility, Ugba, Logo LGA"
},
{
"Oid": 5,
"Name": "Oturkpo Centre",
"Address": "Grain Store, Lower Benue Okele Project, Otukpo"
},
{
"Oid": 6,
"Name": "Gboko Centre",
"Address": "K3 New Road, Opposite former coca cola plant. Solar Schools Street, Gboko"
},
{
"Oid": 7,
"Name": "Gwer East Centre",
"Address": "Ahua Shardye's Warehouse, Behind Sylkan Filling Station, Ikpayongo , G/East LGA"
},
{
"Oid": 8,
"Name": "Kwande Centre",
"Address": "KM 3, Adagi Road, Adikpo"
},
{
"Oid": 9,
"Name": "Ohimini Centre",
"Address": "Ajoga Oglewu, Ohimini"
},
{
"Oid": 10,
"Name": "Oju Centre",
"Address": "Behind Town Hall, Ohuhu owo, Oju LGA"
}
]
}
]
}
所以我想显示名称、首字母缩略词并计算 DistributionCentres,但我不知道如何去做。有人可以帮我吗?我正在使用共享首选项,因为有分配给登录用户的数字锚点。最初,我能够通过在文件中对数组进行硬编码来做到这一点,但我无法通过共享首选项实现相同的目的。请问有人可以帮助我吗?请。如果可行,我将摆脱硬编码的 Person Array 对象。
下面是整体页面浏览量:
import 'package:erg_app/StockPage.dart';
import 'package:erg_app/models/users_model.dart';
import 'package:flutter/material.dart';
import 'package:erg_app/Widgets/nav-drawer.dart';
import 'package:erg_app/models/eopmodel.dart';
import 'package:erg_app/StartScan.dart';
import 'dart:convert';
import 'dart:async';
import 'package:shared_preferences/shared_preferences.dart';
void main() => runApp(MaterialApp(
home: EopPage(),
));
class EopPage extends StatefulWidget {
@override
_MyHomeState createState() => _MyHomeState();
}
class _MyHomeState extends State<EopPage> {
List<Person> persons = [
Person(
name: 'RIFAN',
profileImg: 'assets/images/user.png',
allocated_farmers: "300",
validated_farmers: "345",
non_validated_farmers: "120",
daily_inventory_status: "Completed",
distribution_centers: "100"),
Person(
name: 'MAAN',
profileImg: 'assets/images/user.png',
allocated_farmers: "230",
validated_farmers: "195",
non_validated_farmers: "110",
daily_inventory_status: "Incompleted",
distribution_centers: "70"),
Person(
name: 'COPMAN',
profileImg: 'assets/images/user.png',
allocated_farmers: "560",
validated_farmers: "45",
non_validated_farmers: "780",
daily_inventory_status: "Completed",
distribution_centers: "40"),
];
Widget personDetailCard(Person) {
return Container(
padding: const EdgeInsets.all(10.0),
////////////// 1st card///////////
child: Card(
elevation: 4.0,
color: Colors.grey[100],
margin: EdgeInsets.only(left: 10, right: 10, top: 20, bottom: 10),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
child: Container(
padding: EdgeInsets.only(left: 15, top: 20, bottom: 10),
width: MediaQuery.of(context).size.width,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: 50.0,
height: 50.0,
decoration: new BoxDecoration(
shape: BoxShape.circle,
image: new DecorationImage(
fit: BoxFit.cover,
image: AssetImage(Person.profileImg)))),
),
SizedBox(
width: 20,
),
Text(
Person.name,
textAlign: TextAlign.center,
style: TextStyle(
color: Color(0xFF9b9b9b),
fontSize: 20,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
),
),
],
),
Container(width: 10),
Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 10, top: 10),
child: Text(
'Allocated Farmers:',
textAlign: TextAlign.left,
style: TextStyle(
color: Color(0xFF9b9b9b),
fontSize: 14.0,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
),
),
),
Padding(
padding: const EdgeInsets.only(left: 70, top: 12),
child: Text(
Person.allocated_farmers,
textAlign: TextAlign.left,
style: TextStyle(
color: Colors.grey[700],
fontSize: 14.0,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
),
),
),
],
),
Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 10, top: 10),
child: Text(
'Validated Farmers:',
textAlign: TextAlign.left,
style: TextStyle(
color: Color(0xFF9b9b9b),
fontSize: 14.0,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
),
),
),
Padding(
padding: const EdgeInsets.only(left: 70, top: 12),
child: Text(
Person.validated_farmers,
textAlign: TextAlign.left,
style: TextStyle(
color: Colors.grey[700],
fontSize: 14.0,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
),
),
),
],
),
Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 10, top: 10),
child: Text(
'Non Validated Farmers:',
textAlign: TextAlign.left,
style: TextStyle(
color: Color(0xFF9b9b9b),
fontSize: 14.0,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
),
),
),
Padding(
padding: const EdgeInsets.only(left: 40, top: 12),
child: Text(
Person.non_validated_farmers,
textAlign: TextAlign.left,
style: TextStyle(
color: Colors.grey[700],
fontSize: 14.0,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
),
),
),
],
),
Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 10, top: 10),
child: Text(
'Distribution Centers:',
textAlign: TextAlign.left,
style: TextStyle(
color: Color(0xFF9b9b9b),
fontSize: 14.0,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
),
),
),
Padding(
padding: const EdgeInsets.only(left: 60, top: 12),
child: Text(
Person.distribution_centers,
textAlign: TextAlign.left,
style: TextStyle(
color: Colors.grey[700],
fontSize: 14.0,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
),
),
),
],
),
Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 10, top: 10),
child: Text(
'Daily Inventory Status:',
textAlign: TextAlign.left,
style: TextStyle(
color: Color(0xFF9b9b9b),
fontSize: 14.0,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
),
),
),
Padding(
padding: const EdgeInsets.only(left: 50, top: 12),
child: Text(
Person.daily_inventory_status,
textAlign: TextAlign.left,
style: TextStyle(
color: Person.daily_inventory_status == 'Completed'
? Colors.green
: Colors.red,
fontSize: 14.0,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
),
),
),
],
),
Container(
height: 20,
),
Row(
children: <Widget>[
/////////// Buttons /////////////
Padding(
padding: const EdgeInsets.all(10.0),
child: Person.daily_inventory_status == 'Completed'
? FlatButton(
child: Padding(
padding: EdgeInsets.only(
top: 8, bottom: 8, left: 10, right: 10),
child: Text(
'Validate Farmer',
textDirection: TextDirection.ltr,
style: TextStyle(
color: Colors.white,
fontSize: 15.0,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
),
),
),
color: Colors.green,
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(20.0)),
onPressed: () {
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => StartScanPage()));
// Edit()was here
},
)
: FlatButton(
child: Padding(
padding: EdgeInsets.only(
top: 8, bottom: 8, left: 10, right: 8),
child: Text(
'Take Inventory',
textDirection: TextDirection.ltr,
style: TextStyle(
color: Colors.white,
fontSize: 15.0,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
),
),
),
color: Colors.blueGrey,
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(20.0)),
onPressed: () {
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => StockPage()));
},
),
),
/////////// End of Buttons /////////////
],
),
],
),
),
),
);
}
var user;
var userData;
var anchors;
@override
void initState() {
_getUserAnchor();
super.initState();
}
_getUserAnchor() async{
SharedPreferences localStorage = await SharedPreferences.getInstance();
var userJson = localStorage.getString('loginRes');
user = json.decode(userJson);
anchors = user['Anchors'];
print(anchors);
setState(() {
userData = anchors;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
drawer: NavDrawer(),
appBar: AppBar(
title: Text('Dashboard'),
iconTheme: IconThemeData(color: Colors.white),
backgroundColor: Colors.green,
),
backgroundColor: Colors.white,
body: Container(
padding: const EdgeInsets.fromLTRB(10, 30, 10, 10),
child: ListView(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Icon(Icons.card_membership,
size: 35, color: Colors.orange[400]),
Text(
'Assigned Anchors',
style: TextStyle(color: Colors.orange[400], fontSize: 25),
),
Icon(Icons.notifications_none, size: 35, color: Colors.white)
],
),
Column(
children: persons.map((p) {
return personDetailCard(p);
}).toList())
],
),
),
);
}
}
这里有一个例子可以让你实现你的场景..
var user;
var userData;
List anchors = [];
_getUserAnchor() async{
SharedPreferences localStorage = await SharedPreferences.getInstance();
user = userJson;
setState(() {
anchors = user['Anchors'];
});
print(anchors);
setState(() {
userData = anchors;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('test'),),
body: Column(
children: <Widget>[
ListView.builder(
shrinkWrap: true,
itemCount: anchors.length,
itemBuilder: (BuildContext context, int i){
return Padding(
padding: const EdgeInsets.all(8.0),
child: Card(
child: Column(
children: <Widget>[
Text(anchors[i]['Name']),
Text(anchors[i]['Oid'].toString()),
],
),
),
);
})
],
),
);
}
这里我没有从共享偏好中获取数据我有等于你的 json 到 userJson,你可以使用你相同的步骤从共享偏好中获取数据并跟进,所以你可以建立卡片列表..希望这对您有所帮助...