颤动的抽屉没有正确导航

时间:2018-05-31 06:02:59

标签: navigation flutter

我对Android / Flutter开发很新。

我的应用程序有一个带有2个扩展区块的抽屉 - 当我点击任何一个孩子时,它总是导航到该扩展区块的最后一个孩子。 (也就是说,book1有4个部分。点击任何部分将导航到book1 sectionC。具有3个部分的Book2将始终导航到book2 sectionC)

我无法理解为什么会这样。谢谢你的任何建议。

下面的代码重现了这个问题:

    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,
      );

}

1 个答案:

答案 0 :(得分:0)

更改_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;
  }