我正在使用提供程序包。我正在使用FutureProvider加载json文件。 FutureProvider还具有一个ChangeNotifierProvider作为子级。我不确定这样做时会出什么问题,但是我的孩子没有得到重建。
使用提供程序的底页:
class AddClockBottomSheet extends StatelessWidget {
final LoadTimeZoneRegionProvider _loadTimeZoneRegionProvider = LoadTimeZoneRegionProvider();
@override
Widget build(BuildContext context) {
return FutureProvider<List<TimeZoneRegion>>.value (
initialData: _loadTimeZoneRegionProvider.timeZoneRegionList,
value: _loadTimeZoneRegionProvider.loadTimeZoneRegionList(),
child: Consumer<List<TimeZoneRegion>>(
builder: (context, timeZoneRegionList, child) {
return ChangeNotifierProvider<AddClockProvider>(
builder: (_) => AddClockProvider(timeZoneRegionList),
child: Consumer<AddClockProvider>(
builder: (context, addClockProvider, child) {
return Container(
padding: EdgeInsets.all(20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Text(
"Add Clock",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 30.0,
color: Theme
.of(context)
.appBarTheme
.color
),
),
TextField(
decoration: InputDecoration(
labelText: "Clock label",
),
textAlign: TextAlign.center,
autofocus: true,
),
DropdownButton<TimeZoneRegion>(
value: addClockProvider.timeZoneRegion,
icon: Icon(Icons.arrow_downward),
iconSize: 24,
elevation: 16,
underline: Container(
height: 2,
color: Theme
.of(context)
.appBarTheme
.color,
),
onChanged: addClockProvider.updateTimeZoneRegionDropdown,
items: addClockProvider.timeZoneRegionDropDownMenuItemList,
),
FlatButton(
child: Text(
"Add",
style: TextStyle(
color: Theme
.of(context)
.accentTextTheme
.title
.color,
),
),
color: Theme
.of(context)
.accentColor,
onPressed: () {},
),
],
),
);
},
),
);
}),
);
}
}
要从json文件加载数据的提供程序。
class LoadTimeZoneRegionProvider {
List<TimeZoneRegion> timeZoneRegionList = List();
Future<List<TimeZoneRegion>> loadTimeZoneRegionList() async {
TimeZoneFileHandler handler = TimeZoneFileHandler();
timeZoneRegionList = await handler.parseTimeZones();
print(timeZoneRegionList);
return timeZoneRegionList;
}
}
将与DropDownButton交互的提供程序。
class AddClockProvider with ChangeNotifier {
final List<TimeZoneRegion> timeZoneRegionList;
TimeZoneRegion timeZoneRegion;
TimeZone timeZone;
AddClockProvider(this.timeZoneRegionList);
UnmodifiableListView<
DropdownMenuItem<TimeZoneRegion>> _mapTimeZoneRegionList() {
return UnmodifiableListView<
DropdownMenuItem<TimeZoneRegion>>(
timeZoneRegionList.map<DropdownMenuItem<TimeZoneRegion>>((
TimeZoneRegion timeZoneRegion) {
return DropdownMenuItem<TimeZoneRegion>(
value: timeZoneRegion,
child: Text(timeZoneRegion.name),
);
}
).toList());
}
UnmodifiableListView<
DropdownMenuItem<TimeZoneRegion>> get timeZoneRegionDropDownMenuItemList {
return _mapTimeZoneRegionList();
}
List<DropdownMenuItem<TimeZone>> get timeZoneList {
return timeZoneRegion.timeZones
.map<DropdownMenuItem<TimeZone>>((TimeZone timeZone) {
return DropdownMenuItem<TimeZone>(
value: timeZone,
child: Text("${DurationParser.formatDuration(timeZone.offset)} ${timeZone.name}"),
);
}
).toList();
}
void updateTimeZoneRegionDropdown(TimeZoneRegion newTimeZoneRegion) {
timeZoneRegion = newTimeZoneRegion;
notifyListeners();
}
void updateTimeZoneDropdown(TimeZone newTimeZone) {
timeZone = newTimeZone;
notifyListeners();
}
}