问题描述
我正在尝试通过Flutter中的页面控制器使用Bloc模式及其抛出的'_positions.isNotEmpty':ScrollController未附加到任何滚动视图"来更改活动页面索引.
I'm trying to change the active page index via a pagecontroller in Flutter, using the Bloc pattern and its throwing "'_positions.isNotEmpty': ScrollController not attached to any scroll views.".
这是我的代码:
WelcomeWizardScreen:
WelcomeWizardScreen:
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:fluttertest/blocs/wizard/bloc/bloc.dart';
import 'package:fluttertest/screens/wizardsteps/joincongregation.dart';
import 'package:fluttertest/screens/wizardsteps/welcometomapman.dart';
class WelcomeWizardScreen extends StatefulWidget {
@override
_WelcomeWizardScreenState createState() => _WelcomeWizardScreenState();
}
class _WelcomeWizardScreenState extends State<WelcomeWizardScreen> {
final WizardBloc wizardBloc = WizardBloc();
@override
Widget build(BuildContext context) {
// TODO: implement build
return BlocProvider(
builder: (BuildContext context) => WizardBloc(),
child: PageView(
children: <Widget>[WelcomeToMapMan(), JoinCongregation()],
controller: wizardBloc.pageController,
),
);
}
}
WizardBloc:
WizardBloc:
import 'package:bloc/bloc.dart';
import 'package:flutter/widgets.dart';
import 'wizard_state.dart';
import 'wizard_event.dart';
class WizardBloc extends Bloc<WizardEvent, WizardState> {
int activeStep = 0;
final PageController pageController = PageController(initialPage: 0, keepPage: false, viewportFraction: 0.4);
@override
WizardState get initialState => WelcomeToMapManState();
@override
Stream<WizardState> mapEventToState(
WizardEvent event,
) async* {
if (event is ChangePage)
{
pageController.jumpToPage(event.pageIndex);
}
// TODO: Add Logic
}
Stream<WizardState> _mapJoinCongregationToState() async* {
}
}
PageView中的屏幕之一:
One of the screens in the PageView:
class JoinCongregation extends StatelessWidget {
@override
Widget build(BuildContext context) {
final WizardBloc _wizardBloc = BlocProvider.of<WizardBloc>(context);
// TODO: implement build
return Column(
children: <Widget>[
Center(
child: Text("this is step 2"),
),
RaisedButton(
child: Text("back to step 1"),
onPressed: () => {_wizardBloc.dispatch(ChangePage(0))},
)
],
);
}
}
当它被调用来更改页面时,PageViewController似乎没有附加"到PageView,但是它正确地初始化了(在正确的页面索引上).
It seems like the PageViewController isn't "attached" to the PageView when it is called on to change pages, but it initalises correctly (on the correct page index).
我该如何解决?我对扑扑还挺陌生的.
How can I solve this? I'm fairly new to flutter.
推荐答案
您不应该在bloc中创建PageController,因为bloc不应与UI耦合(从理论上讲,您应该能够在Flutter和Bluster之间重用您的blocAngularDart).请参考 https://github.com/felangel/bloc/issues/18 有关如何完成此操作的示例.
You shouldn't create a PageController in a bloc because a bloc should not be coupled with the UI (theoretically you should be able to reuse your bloc between Flutter and AngularDart). Please refer to https://github.com/felangel/bloc/issues/18 for an example of how you can accomplish this.
这篇关于PageView抛出"_positions.isNotEmpty":ScrollController未附加到任何滚动视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!