我是新手,不会摆弄它。所以,请对我有耐心。
单击PopupMenuButton的特定菜单项时,将引发以下异常,但始终只能第二次:
'package:flutter/src/widgets/navigator.dart':失败断言:line 1846 pos 12:'!_debuglocked':不是true。
class PopupMenuChoice {
const PopupMenuChoice({this.title, this.pageRoute});
final String title;
final MaterialPageRoute pageRoute;
}
new PopupMenuButton<PopupMenuChoice>(
itemBuilder: (BuildContext context) {
return _popupMenus.map((PopupMenuChoice choice) {
return new PopupMenuItem<PopupMenuChoice>(
value: choice,
child: new Text(choice.title),
);
}).toList();
},
onSelected: _popupMenuSelected,
),
class RandomWordsState extends State<RandomWords> {
final _suggestions = <WordPair>[];
final _saved = new Set<WordPair>();
final _popupMenus = <PopupMenuChoice>[];
...
}
正如您所看到的,有私有变量用于保存WordPair对象,但也用于菜单选择。
_popupMenus列表是在“构建重写”中设置的:
@override
Widget build(BuildContext context) {
// Setup page routes
if (_popupMenus.where((p) => p.title == 'Saved Suggestions').length == 0) {
final _pageRouteSavedSuggestions = new MaterialPageRoute(
builder: (context) {
final tiles = _saved.map(
(pair) {
return new ListTile(
title: new Text(
pair.asPascalCase,
style: _biggerFont,
),
);
},
);
final divided = ListTile
.divideTiles(
context: context,
tiles: tiles,
)
.toList();
return new Scaffold(
appBar: new AppBar(
title: new Text('Saved Suggestions'),
),
body: new ListView(children: divided),
);
},
);
_popupMenus.add(new PopupMenuChoice(
title: 'Saved Suggestions', pageRoute: _pageRouteSavedSuggestions));
}
if (_popupMenus.where((p) => p.title == 'TEST Page').length == 0) {
final _pageRouteTest = new MaterialPageRoute(
builder: (context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('TEST Page'),
),
body: new Text('Some content...'),
);
},
);
_popupMenus.add(
new PopupMenuChoice(title: 'TEST Page', pageRoute: _pageRouteTest));
}
...
在定义的PopupMenuChoice的MaterialPageRoute中,私有变量可能是access(例如_saved)。
void _popupMenuSelected(PopupMenuChoice choice) {
Navigator.of(context).push(choice.pageRoute);
}
void _popupMenuSelected(PopupMenuChoice choice) {
await Future.delayed(const Duration(milliseconds: 100));
Navigator.push(context, choice.pageRoute);
}