我正在尝试建立ExpansionPanelList和即时通讯使用提供程序在那里。老实说,我无法完全理解该提供程序,但由于无法找到使之起作用的其他方式,因此我正在使用它。我已经看到提供商在其他地方使用了类似的方式,所以我认为我在这里不是完全错误的吗?
感谢您的帮助!
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class Product {
final String name;
final String price;
Product({this.name, this.price});
}
class DatabaseService {
final CollectionReference productsCollection =
Firestore.instance.collection('products');
List<Product> productListFromSnapshot(QuerySnapshot snapshot) {
// cycle through each document and create product list
return snapshot.documents.map((doc) {
return Product(
name: doc.data['name'] ?? "",
price: doc.data['price'] ?? 0,
);
}).toList();
}
Stream<List<Product>> get products {
return productsCollection.snapshots().map(productListFromSnapshot);
}
}
class ProductList extends StatefulWidget {
ProductList({Key key}) : super(key: key);
@override
_ProductListState createState() => _ProductListState();
}
class _ProductListState extends State<ProductList> {
@override
Widget build(BuildContext context) {
final products = Provider.of<List<Product>>(context) ?? [];
return Container(
child: ExpansionPanelList.radio(
children: products.map<ExpansionPanel>((Product product) {
return ExpansionPanelRadio(
value: product.name,
headerBuilder: (BuildContext context, bool isExpanded) {
return ListTile(
title: Text(product.name),
);
},
body: ListTile(
title: Text(product.name),
subtitle: Text('some text here as subtitle text'),
trailing: Icon(Icons.delete),
onTap: () {
setState(() {
products.removeWhere((currentItem) => product == currentItem);
});
},
),
);
}).toList(),
),
);
}
}
____________________
class Home extends StatelessWidget {
final AuthService _auth = AuthService();
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blue[50],
appBar: AppBar(
title: Text('test'),
backgroundColor: Colors.blue[400],
elevation: 0.0,
actions: <Widget>[
FlatButton.icon(
onPressed: () async {
await _auth.signout();
},
icon: Icon(Icons.person),
label: Text('LogOut'))
],
),
body: HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
HomeScreen({Key key}) : super(key: key);
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
@override
Widget build(BuildContext context) {
return Center(
child: Column(children: <Widget>[
RaisedButton(
color: Colors.blue[600],
child: Text('ProductList'),
onPressed: () {
Navigator.push(context,
MaterialPageRoute(builder: (contex) => ProductList()));
}),
]),
);
}
}