我在使用
error
时得到这个AppBar
:
滚动条的 ScrollController 没有附加 ScrollPosition。
这是我的
CustomScrollBar
:
class CustomScrollBar extends StatelessWidget {
final Widget child;
final ScrollController scrollController;
const CustomScrollBar({
required this.scrollController,
required this.child,
});
@override
Widget build(BuildContext context) {
return RawScrollbar(
thumbColor: AppColors.gray,
radius: Radius.circular(8),
thickness: 4,
isAlwaysShown: true,
controller: scrollController,
child: child,
);
}
}
我应该始终可见。这就是我使用它的方式:
child: CustomScrollBar(
scrollController: _scrollControllerForScrollBar,
child: SingleChildScrollView(
controller: _scrollControllerForScrollBar,
child: Padding(
padding: EdgeInsets.all(7.0.scaled),
child: Container(
width: double.infinity,
child: Text(
'any text bla bla bla \n\n\n this is a lot of \n text \n .'
),
),
),
),
),
如您所见,
ScrollBar
和SingleChildScrollView
都使用相同的ScrollController
。我不知道为什么会出现这个错误。知道我在这里缺少什么吗?
我将
ScrollBar
与 ListView
小部件一起使用,并在调试控制台中收到此错误。为了摆脱它,我必须在两个小部件上设置 ScrollController
。
final yourScrollController = ScrollController();
Scrollbar(
isAlwaysShown: true,
thickness: 10,
controller: yourScrollController, // Here
child: ListView.builder(
padding: EdgeInsets.zero,
scrollDirection: Axis.vertical,
controller: yourScrollController, // AND Here
itemCount: yourRecordList?.length
....
)
)
假设您同时使用
Scrollbar
小部件和 ListView
小部件作为滚动条的子项 - 它们都应该具有与 : 相同的控制器
ScrollController _controller = ScrollController();
Scrollbar(
controller: _controller,
child: ListView(
controller: _controller,
....), // ListView
) // Scrollbar
完全删除scrollController,这应该可以解决问题。这就是我修复代码的方法,所以你的代码应该看起来像这样
class CustomScrollBar extends StatelessWidget {
final Widget child;
const CustomScrollBar({
required this.child,
});
@override
Widget build(BuildContext context) {
return RawScrollbar(
thumbColor: AppColors.gray,
radius: Radius.circular(8),
thickness: 4,
isAlwaysShown: true,
child: child,
);
}
}
第二部分应该是这样的
child: CustomScrollBar(
child: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(7.0.scaled),
child: Container(
width: double.infinity,
child: Text(
'any text bla bla bla \n\n\n this is a lot of \n text \n .'
),
),
),
),
),
如果你像这样使用嵌套滚动条:
Expanded(
flex: 2,
child: Center(
child: Scrollbar(
child: SingleChildScrollView(
primary: true,
child: Scrollbar(
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
.....)
您可以通过将此行添加到第一个可滚动视图来解决问题:
primary: true
UI 绘图仪想要知道移动设备上和桌面上低分辨率滚动条的起始位置以及尺寸:
MediaQuery.of(context).size.shortestSide < 600
在调度程序回调期间抛出以下断言: 滚动条的 ScrollController 没有附加 ScrollPosition
如果没有 ScrollPosition,则无法绘制滚动条。
滚动条尝试使用提供的 ScrollController。这 ScrollController 应该与 ScrollView 关联 正在应用滚动条。当提供自己的 ScrollController,确保Scrollbar和Scrollable小部件 使用同一个。
这是flutter的bug,https://github.com/flutter/flutter/issues/82573。
固定在flutter master分支上:
flutter channel master
flutter upgrade
对于此错误,您必须为 ScrollBar 和 SingleSchildScrollView/ListView 定义相同的 1 个scrollController:
例如我的项目:
final ScrollController _scrollController = ScrollController(); //define just 1 controller
final _currencyFormatter = NumberFormat('#,##0.00', 'vi_VN');
final double _safeSpaceToShowToolTip = 1.4;
final double _fixedChartHeight = 350.0;
int _touchedIndex = -1;
@override
Widget build(BuildContext context) {
return Scrollbar(
controller: _scrollController, // and use it both here
isAlwaysShown: true,
trackVisibility: true,
radius: Radius.circular(6.0),
child: SingleChildScrollView(
controller: _scrollController, // AND HERE
scrollDirection: Axis.horizontal,
physics: BouncingScrollPhysics(),
child: Container(
margin: EdgeInsets.only(bottom: R.dimens.mediumSpacing),
height:
R.dimens.getSpecificSize(_fixedChartHeight, isForHeight: true),
width: R.dimens
.getSpecificSize(_fixedChartHeight * 1.9, isForHeight: false),
child: BarChart(
_barChartData(),
),
),
),
);
}
RawScrollbar(
scrollbarOrientation: ScrollbarOrientation.right,
radius: Radius.circular(100.h),
mainAxisMargin: 2.h,
thumbVisibility: true,
controller: Scontroller,
child: ListView.builder(
// primary: true,
controller: Scontroller,
itemCount: _warehouseListedData.value?.map((e) => e["godown"]).toSet().toList().length,
scrollDirection: Axis.vertical,
shrinkWrap: true,
padding: EdgeInsets.all(0.5.h),
itemBuilder: (context, index) {
return Container(
width: double.infinity,
margin: EdgeInsets.all(1.w),
padding: EdgeInsets.symmetric(vertical: 1.h, horizontal: 2.w),
decoration: BoxDecoration(color: Color.fromRGBO(34, 34, 34, 1), borderRadius: BorderRadius.circular(1.h)),
child: Column(
children: [
IntrinsicHeight(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
RichText(
text: TextSpan(
children: [
TextSpan(text: "GODOWN: ", style: TextStyle(fontSize: 18, decoration: TextDecoration.none, color: Colors.red, fontWeight: FontWeight.w500)),
TextSpan(text: "${_warehouseListedData.value?.map((e) => e["godown"]).toSet().toList()[index]}", style: TextStyle(fontSize: 20, decoration: TextDecoration.none, color: Colors.red, fontWeight: FontWeight.w600)),
],
),
),
AspectRatio(
aspectRatio: 1,
child: Container(
decoration: BoxDecoration(borderRadius: BorderRadius.circular(0.5.h), border: Border.all(color: Color.fromARGB(255, 75, 75, 75), width: 2.5), color: Colors.white),
child: Center(
child: Padding(
padding: EdgeInsets.all(1.0.w),
child: Text(
"ALL",
style: TextStyle(fontWeight: FontWeight.w700, decoration: TextDecoration.none),
),
),
),
),
)
],
),
),
SizedBox(
height: 1.h,
),
Column(
children: () {
List<Widget> widgets = [];
var thisGodown = _warehouseListedData.value?.map((e) => e["godown"]).toSet().toList()[index];
var thisGodownCompartments = _warehouseListedData.value
?.map((e) {
if (e["godown"] == thisGodown) {
return e["compartment"];
}
})
.toSet()
.whereType<String>()
.toList();
for (var compartment in thisGodownCompartments!) {
var thisCompartmentLocations = _warehouseListedData.value
?.map((e) {
if (e["godown"] == thisGodown && e["compartment"] == compartment) {
return e["location"];
}
})
.toSet()
.whereType<String>()
.toList();
print(thisCompartmentLocations.toString());
ScrollController sc = ScrollController();
widgets.add(
Container(
height: 6.h,
margin: EdgeInsets.symmetric(horizontal: 0.5.w),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(1.h),
color: Color.fromARGB(255, 69, 69, 69),
),
child: RawScrollbar(
controller: sc,
thumbVisibility: true,
scrollbarOrientation: ScrollbarOrientation.bottom,
child: ListView.builder(
// primary: true,
controller: sc,
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemCount: thisCompartmentLocations?.length,
itemBuilder: (context, index) {
return AspectRatio(
aspectRatio: 1,
child: Container(
margin: EdgeInsets.symmetric(vertical: 2.w, horizontal: 2.w),
decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(1.w)),
),
);
},
),
),
), // TODO
);
widgets.add(SizedBox(
height: 1.h,
));
}
// print(thisGodownCompartments.toString());
return widgets;
}(),
)
],
),
);
},
),
)
抱歉我懒得解构。希望有人觉得它有用
Scrollbar 和 ListView 必须包含相同的 ScrollController。 不应定义 ListView 物理