所以,我有带有CheckBox的Streambuilder中的ListTile项。我想从ListTile中为FOB onPressed操作传递选定项的值。
我已经用CheckBox创建了ListTiles,并且checkbox的setState可以正常工作,并且在传递更新Firebase数据时,ListTile中的onTap函数不起作用。
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Bachha Party Hisab',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
final Record record;
MyHomePage({Record record})
: record = record,
super(key: new ObjectKey(record));
_MyHomePageState createState() {
return _MyHomePageState();
}
}
class _MyHomePageState extends State<MyHomePage> {
Color gradientStart =
Colors.deepPurple[700]; //Change start gradient color here
Color gradientEnd = Colors.purple[500]; //Change end gradient color here
//controller for BottomNavigationBar
PageController _pageController;
int _currentIndex = 0;
void navigationTapped(int page) {
_pageController.animateToPage(page,
duration: const Duration(milliseconds: 200), curve: Curves.ease);
}
@override
void initState() {
super.initState();
_pageController = new PageController();
}
void onPageChanged(int page) {
setState(() {
this._currentIndex = page;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
backgroundColor: Colors.deepPurple[700],
title: Text(
'Bachha Party Hisab',
style: TextStyle(fontFamily: 'Bangers'),
)),
//use Container to apply the background as the gradient
body: new Container(
decoration: new BoxDecoration(
gradient: new LinearGradient(
colors: [gradientEnd, gradientStart],
begin: const FractionalOffset(0.5, 0.0),
end: const FractionalOffset(0.0, 0.5),
stops: [0.0, 1.0],
tileMode: TileMode.clamp),
),
child: new PageView(
//creating the pages For the page view
children: [
buildBody(context),
new Container(color: Colors.redAccent)
],
controller: _pageController,
onPageChanged: onPageChanged,
),
),
//floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.purple,
child: Icon(Icons.save, color: Colors.white),
elevation: 2.0,
onPressed: null,
tooltip: 'Add something',
),
bottomNavigationBar: new Theme(
data: Theme.of(context).copyWith(
// sets the background color of the `BottomNavigationBar`
canvasColor: Colors.deepPurple[700],
// sets the active color of the `BottomNavigationBar` if `Brightness` is light
primaryColor: Colors.white,
textTheme: Theme.of(context)
.textTheme
.copyWith(caption: new TextStyle(color: Colors.white))),
child: BottomNavigationBar(
currentIndex: _currentIndex,
onTap: navigationTapped,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: new Text(
'Home',
style: TextStyle(fontFamily: 'Bangers'),
),
),
BottomNavigationBarItem(
icon: Icon(Icons.insert_drive_file),
title: new Text('Standings',
style: TextStyle(fontFamily: 'Bangers')))
])));
}
}
Widget buildBody(BuildContext context) {
//connecting to the firebase database
return StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('hisab').snapshots(),
builder: (context, snapshots) {
if (!snapshots.hasData) {
return LinearProgressIndicator();
}
return _buildList(context, snapshots.data.documents);
});
}
Widget _buildList(BuildContext context, List<DocumentSnapshot> snapshot) {
return ListView(
padding: EdgeInsets.only(top: 20.0),
children: snapshot.map((data) => _buildListitem(context, data)).toList(),
);
}
Widget _buildListitem(BuildContext context, DocumentSnapshot data) {
return CustomListItemWidget(
record: Record.fromSnapshot(data),
);
}
//If the widget in a class needs stateful widget class then always "create
//new stateful widget class"
//following is the example of the checkbox widget
class CustomListItemWidget extends StatefulWidget {
CustomListItemWidget({Key, key, this.record}) : super(key: key);
final Record record;
@override
State createState() => _CustomListItemWidgetState();
}
class _CustomListItemWidgetState extends State<CustomListItemWidget> {
bool _values = false;
void _onChanged(bool newValue) {
setState(() {
if (_values = true) {
_values = newValue;
}
});
}
@override
Widget build(BuildContext context) {
return Container(
child: new ListTile(
onTap: () {
widget.record.reference.updateData({'quantity': widget.record.quantity + 1});
},
leading: CircleAvatar(child: Text(widget.record.name[0])),
title: new Column(
children: <Widget>[
new CheckboxListTile(
title: Text(
widget.record.name,
style: TextStyle(
fontFamily: 'Playfair',
fontSize: 17,
color: Colors.white,
fontStyle: FontStyle.italic),
),
value: _values,
onChanged: _onChanged,
)
],
),
),
);
}
}
class Record {
final String name;
final int quantity;
final DocumentReference reference;
Record.fromMap(Map<String, dynamic> map, {this.reference})
: assert(map['name'] != null),
assert(map['quantity'] != null),
name = map['name'],
quantity = map['quantity'];
Record.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data, reference: snapshot.reference);
@override
String toString() => "Record<$name:$quantity>";
}