我对Android/Flutter开发很陌生。
我的应用程序有一个带有 2 个扩展磁贴的抽屉 - 当我单击任何子级时,它总是导航到该扩展磁贴的最后一个子级。 (也就是说,第 1 册有 4 个部分。单击任何部分将导航到book1部分C。包含 3 个部分的 Book2 将始终导航到 book2 部分 C(
我不明白为什么会这样。感谢您的任何建议。
下面的代码重现了该问题:
import 'package:flutter/material.dart';
void main() async {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
final data = buildData();
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'App',
theme: new ThemeData(
primarySwatch: Colors.green,
),
home: ServiceView(currentService: data.prayerBooks[0].services[0]),
);
}
}
class ServiceView extends StatelessWidget {
final currentService;
ServiceView({Key key, @required this.currentService}) : super(key: key);
// ServiceViewState();
@override
Widget build(BuildContext context) {
final allPrayerBooks = buildData();
return new Scaffold (
drawer: _buildDrawer(allPrayerBooks.prayerBooks),
appBar: new AppBar(
title: new Text(currentService.title),
actions: <Widget>[
],
),
// body: _buildService(context, currentService),
);
}
Drawer _buildDrawer(prayerBooks) {
return Drawer(
child: new ListView.builder(
itemBuilder: (BuildContext context, int index) =>
new DrawerPrayerBookEntry(context, prayerBooks[index]),
itemCount: prayerBooks.length,
),
);
}
}
class DrawerPrayerBookEntry extends StatelessWidget {
const DrawerPrayerBookEntry(BuildContext context, this.prayerBook);
final PrayerBook prayerBook;
Widget _buildTiles(BuildContext context, PrayerBook prayerbook) {
if (prayerbook.services.isEmpty)
return new ListTile(title: new Text(prayerbook.title ?? 'No Title'));
return new ExpansionTile(
key: new PageStorageKey<PrayerBook>(prayerbook),
title: new Text(prayerbook.title ?? 'No title'),
children: _buildServicesTiles(context, prayerbook),
);
}
@override
Widget build(BuildContext context) {
return _buildTiles(context, prayerBook);
}
List<Widget> _buildServicesTiles(context, prayerBook) {
List<Widget> servicesList = [];
Service serviceName;
for (serviceName in prayerBook.services) {
servicesList.add(new ListTile(
title: new Text(serviceName.title ?? 'No title',),
onTap: () {
Navigator.pop(context);
Navigator.push(context, new MaterialPageRoute(
builder: (BuildContext context) {
return ServiceView(
currentService: serviceName,
);
},
));
},
));
}
return servicesList;
}
}
PrayerBooksContainer buildData(){
return new PrayerBooksContainer(
[
new PrayerBook('language','book1', 'book1', [
new Service('service', 'b1 sA'),
new Service('service', 'b1 sB'),
new Service('service', 'b1 sC'),
new Service('service', 'b1 sD'),
]),
new PrayerBook('language','book2','book2',[
new Service('service', 'b2 sA'),
new Service('service', 'b2 sB'),
new Service('service', 'b2 sC'),
]),
],
);
}
class PrayerBooksContainer extends Object {
final List<PrayerBook> prayerBooks;
PrayerBooksContainer(
this.prayerBooks,
);
}
class PrayerBook extends Object {
final String language;
final String apiName;
final String title;
final List<Service> services;
PrayerBook(
this.language,
this.apiName,
this.title,
this.services,
);
}
class Service extends Object {
final String apiName;
final String title;
Service(
this.apiName,
this.title,
);
}
更改_buildServicesTiles方法:
List<Widget> _buildServicesTiles(context, prayerBook) {
List<Widget> servicesList = [];
//Service serviceName;
for (var serviceName in prayerBook.services) {
servicesList.add(new ListTile(
title: new Text(serviceName.title ?? 'No title',),
onTap: () {
Navigator.pop(context);
Navigator.push(context, new MaterialPageRoute(
builder: (BuildContext context) {
return ServiceView(
currentService: serviceName,
);
},
));
},
));
}
return servicesList;
}