模型类:
Venue with ChangeNotifier{
String id;
update(String venueId) {
id = venueId;
notifyListeners();
}
}
尝试像这样获取id值:
Widget _buildContents(BuildContext context) {
final venue = Provider.of<Venue>(context);
final id = venue.id;
print('@@@@@@@@@@'); //// trying to get the the id here.
}
我的ID应该在dropDownButton的onTap回调中更新:
DropdownButton(
....
items: venues.map((venue) {
return DropdownMenuItem<String>(
onTap: () {
venue.update(venue.id);
},
value: venue.name,
child: Text(
venue.name,
style: TextStyle(fontSize: 28),
),
);
}).toList(),
.....
)
并且Provider ..位于小部件上方
MultiProvider(providers: [
ChangeNotifierProvider<Venue>(create: (_) => Venue()),
], child: HomePage());
出了点问题..帮助!?
答案 0 :(得分:1)
您可以在下面复制粘贴运行完整代码
您可以使用DropdownButton<Venue>
并在venue.update
中调用onChanged
代码段
DropdownButton<Venue>(
value: dropdownValue,
icon: Icon(Icons.arrow_downward),
iconSize: 24,
elevation: 16,
style: TextStyle(color: Colors.deepPurple),
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
onChanged: (Venue newValue) {
setState(() {
dropdownValue = newValue;
venue.update(newValue.id);
});
},
items: venues.map((venue) {
return DropdownMenuItem<Venue>(
value: venue,
child: Text(
venue.name,
style: TextStyle(fontSize: 28),
),
);
}).toList()
工作演示输出
I/flutter (27988): @@@@@@@@@@ 1
I/flutter (27988): @@@@@@@@@@ 2
工作演示
完整代码
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class Venue with ChangeNotifier {
String id;
String name;
update(String venueId) {
id = venueId;
notifyListeners();
}
Venue({this.id, this.name});
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MultiProvider(providers: [
ChangeNotifierProvider<Venue>(create: (_) => Venue()),
], child: HomePage()),
);
}
}
class HomePage extends StatefulWidget {
HomePage({Key key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
Venue dropdownValue;
List<Venue> venues = [
Venue(id: "1", name: "name1"),
Venue(id: "2", name: "name2"),
Venue(id: "3", name: "name3"),
];
@override
Widget build(BuildContext context) {
final venue = Provider.of<Venue>(context);
final id = venue.id;
print('@@@@@@@@@@ $id');
return Scaffold(
appBar: AppBar(
title: Text("demo"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
DropdownButton<Venue>(
value: dropdownValue,
icon: Icon(Icons.arrow_downward),
iconSize: 24,
elevation: 16,
style: TextStyle(color: Colors.deepPurple),
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
onChanged: (Venue newValue) {
setState(() {
dropdownValue = newValue;
venue.update(newValue.id);
});
},
items: venues.map((venue) {
return DropdownMenuItem<Venue>(
value: venue,
child: Text(
venue.name,
style: TextStyle(fontSize: 28),
),
);
}).toList(),
),
],
),
),
);
}
}