Неопределенное имя «контекст». Попробуйте исправить имя на то, которое определено, или определить имя - PullRequest
0 голосов
/ 26 мая 2020

Это мой код, но я никогда не меняю страницу. Ошибка = неопределенное имя 'context' .

Вот некоторые из них:

Undefined name 'context' Undefined name 'context' во флаттере навигация

Try correcting the name to one that is defined, or defining the name.

class EntryItem extends StatelessWidget {
const EntryItem(this.entry);

enter code here

final Entry entry;

Widget _buildTiles(Entry root) {
if (root.children.isEmpty)
return ListTile(
title: Text(root.title, style: TextStyle(color: Colors.black)),
trailing: Icon(
Icons.keyboard_arrow_right,
color: Colors.white,
),
onTap: () {
### Navigator.of(context).pushNamedAndRemoveUntil('/gettingStarted', (route) => false)
},
selected: true,
);
return ExpansionTile(
key: PageStorageKey(root),
trailing: Icon(
Icons.keyboard_arrow_right,
),
title: Text(
root.title,
style: TextStyle(color: Color(0xFF2F6CE6)),
),
children: root.children.map(_buildTiles).toList(),
);
}

@override
Widget build(BuildContext context) {
return _buildTiles(entry);
}
}

Ответы [ 2 ]

0 голосов
/ 28 мая 2020

Вы можете передать контекст из ListView.builder в метод EntryItem при его создании и использовать его в OnClick для перехода к OtherPage. import 'package: flutter / material.dart';

void main() => runApp(new ExpansionTileSample());

class ExpansionTileSample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: const Text('ExpansionTile'),
        ),
        body: new ListView.builder(
          itemBuilder: (BuildContext context, int index) =>
          new EntryItem(data[index], context),
          itemCount: data.length,
        ),
      ),
    );
  }
}

// One entry in the multilevel list displayed by this app.
class Entry {
  Entry(this.title, [this.children = const <Entry>[]]);

  final String title;
  final List<Entry> children;
}

// The entire multilevel list displayed by this app.
final List<Entry> data = <Entry>[
  new Entry(
    'Chapter A',
    <Entry>[
      new Entry(
        'Section A0',
        <Entry>[
          new Entry('Item A0.1'),
          new Entry('Item A0.2'),
          new Entry('Item A0.3'),
        ],
      ),
      new Entry('Section A1'),
      new Entry('Section A2'),
    ],
  ),
  new Entry(
    'Chapter B',
    <Entry>[
      new Entry('Section B0'),
      new Entry('Section B1'),
    ],
  ),
  new Entry(
    'Chapter C',
    <Entry>[
      new Entry('Section C0'),
      new Entry('Section C1'),
      new Entry(
        'Section C2',
        <Entry>[
          new Entry('Item C2.0'),
          new Entry('Item C2.1'),
          new Entry('Item C2.2'),
          new Entry('Item C2.3'),
        ],
      ),
    ],
  ),
];

// Displays one Entry. If the entry has children then it's displayed
// with an ExpansionTile.
class EntryItem extends StatelessWidget {
  const EntryItem(this.entry, this.context);

  final Entry entry;
  final BuildContext context;

  Widget _buildTiles(Entry root) {
    if (root.children.isEmpty) return new ListTile(
        onTap: () { print("I can be tapped but I dont't figure out how to navigate");
        Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
          return new OtherScreen();
        }));
        },
        title: new Text(root.title));
    return new ExpansionTile(
      key: new PageStorageKey<Entry>(root),
      title: new Text(root.title),
      children: root.children.map(_buildTiles).toList(),
    );
  }

  @override
  Widget build(BuildContext context) {
    return _buildTiles(entry);
  }
}
0 голосов
/ 26 мая 2020

Вы должны передать context из своей функции сборки в _buildTiles

Widget _buildTiles(Entry root, BuildContext context) 

и:

return _buildTiles(entry, context);

, а затем использовать контекст для навигации.

...