我需要在抽屉中使用可重新排序的列表。为此,我创建了一个小部件。当我在主屏幕/脚手架/构建中使用小部件时:一切正常。
但是当我在抽屉中使用相同的窗口小部件时,会出现应用异常,建议填写错误报告。
在抽屉中使用小部件时,我还需要做其他事情吗?或者这真的是一个错误吗?
我的程序的简化版:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: _myDrawer(),
drawer: Container(child: _myDrawer()),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
class _myDrawer extends StatefulWidget {
_myDrawer({
Key key,
}) : super(key: key);
@override
__myDrawerState createState() => __myDrawerState();
}
class __myDrawerState extends State<_myDrawer> {
List<String> _sortList = ['Cat1', 'Cat2', 'Date'];
@override
Widget build(BuildContext context) {
return Container(
child: ReorderableListView(onReorder: (int oldIndex, int newIndex) {},
//scrollDirection: Axis.vertical,
//padding: const EdgeInsets.symmetric(vertical: 8.0),
children: <Widget>[
Card(
key: ValueKey(_sortList[0]),
child: ListTile(
title: Text(_sortList[0]),
),
),
Card(
key: ValueKey(_sortList[1]),
child: ListTile(
title: Text(_sortList[1]),
),
),
Card(
key: ValueKey(_sortList[2]),
child: ListTile(
title: Text(_sortList[2]),
),
),
]),
);
}
}
崩溃报告是这样的:
════════ Exception caught by gesture ═══════════════════════════════════════════════════════════════
The following assertion was thrown while handling a gesture:
ScrollController attached to multiple scroll views.
'package:flutter/src/widgets/scroll_controller.dart':
Failed assertion: line 111 pos 12: '_positions.length == 1'
Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
https://github.com/flutter/flutter/issues/new?template=BUG.md
When the exception was thrown, this was the stack:
#2 ScrollController.position (package:flutter/src/widgets/scroll_controller.dart:111:12)
#3 ScrollController.offset (package:flutter/src/widgets/scroll_controller.dart:118:24)
#4 _ReorderableListContentState._scrollTo (package:flutter/src/material/reorderable_list.dart:322:51)
#5 _ReorderableListContentState._wrap.<anonymous closure>.<anonymous closure> (package:flutter/src/material/reorderable_list.dart:543:11)
#6 _DragTargetState.didEnter (package:flutter/src/widgets/drag_target.dart:522:88)
...
Handler: "onStart"
Recognizer: DelayedMultiDragGestureRecognizer#1c0a1
════════════════════════════════════════════════════════════════════════════════════════════════════
感谢您的帮助!
答案 0 :(得分:0)
我无法复制您提到的问题。当我点击抽屉时,我可以正确看到它:
这是完整的代码:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
// body: _myDrawer(),
drawer: Container(child: _MyDrawer())
);
}
}
class _MyDrawer extends StatefulWidget {
_MyDrawer({
Key key,
}) : super(key: key);
@override
__MyDrawerState createState() => __MyDrawerState();
}
class __MyDrawerState extends State<_MyDrawer> {
List<String> _sortList = ['Cat1', 'Cat2', 'Date'];
@override
Widget build(BuildContext context) {
return Container(
child: ReorderableListView(onReorder: (int oldIndex, int newIndex) {},
scrollDirection: Axis.vertical,
padding: const EdgeInsets.symmetric(vertical: 8.0),
children: <Widget>[
Card(
key: ValueKey(_sortList[0]),
child: ListTile(
title: Text(_sortList[0]),
),
),
Card(
key: ValueKey(_sortList[1]),
child: ListTile(
title: Text(_sortList[1]),
),
),
Card(
key: ValueKey(_sortList[2]),
child: ListTile(
title: Text(_sortList[2]),
),
),
]),
);
}
}
注意:我是最新的稳定Flutter版本。