gpt4 book ai didi

Flutter:如何跳转到在 PageView 上动态创建的最后一页

转载 作者:行者123 更新时间:2023-12-04 16:42:07 27 4
gpt4 key购买 nike

我有一个显示页面列表的 pageView。

应用程序提供了一个 + 按钮,用于在集合的末尾添加一个新页面。

一旦成功创建了这个新页面,我需要 pageView 自动跳转到最后一页。

如果我尝试重新显示提供初始位置设置为最后一个 pageView 索引的提供程序的 View ,则它不起作用

PageController(initialPage: 0, keepPage: false);

任何实现想法?
  • 使用监听器(但哪个监听器?)

  • 完整代码:
      @override
    Widget build(BuildContext context) {
    if (_userId == null) {
    return Scaffold(body: Center(child: Text("Loading experiences")));
    }
    print('Rebuilding entire view.');
    return Scaffold(
    appBar: new AppBar(
    title: StreamBuilder<ExperiencesInfo>(
    stream: _experiencesInfoStream,
    builder: (context, snapshot) {
    if (snapshot.hasData) {
    _selectedExperience = snapshot.data.currentExperience;
    return Text(snapshot.data.currentExperience.name);
    } else {
    return Center();
    }
    }),
    ),
    body: new Container(
    padding: new EdgeInsets.only(
    top: 16.0,
    ),
    decoration: new BoxDecoration(color: Colors.yellow),
    child: Column(
    children: <Widget>[
    Expanded(
    child:
    StreamBuilder<List<Experience>>(
    stream: _userExperiencesStream,
    builder: (context, snapshot) {
    if (snapshot.hasData) {
    return _createExperiencesPagesWidget(snapshot.data);
    } else {
    return Center();
    }
    })
    ),
    _buildPageIndicator(),
    Padding(
    padding: EdgeInsets.only(bottom: 20.0),
    )
    ],
    ),
    ),
    floatingActionButton: new FloatingActionButton(
    child: new Icon(Icons.add),
    onPressed: () {
    _displayAddMenu();
    }),
    );
    }



    _createExperiencesPagesWidget(List<Experience> experiences) {
    print('Creating ExperiencesPagesWidget ${experiences}');
    return PageView.builder(
    physics: new AlwaysScrollableScrollPhysics(),
    controller: _pageController,
    itemCount: experiences.length,
    itemBuilder: (BuildContext context, int index) {
    return ConstrainedBox(
    constraints: const BoxConstraints.expand(),
    child: Column(children: <Widget>[
    _buildMoodIndicator(experiences[index]),
    _buildMoodSelector(),
    ]));
    },
    onPageChanged: (index) {
    if (_actionType == ActionType.none) {
    print('page changed to index: ${index}, updating stream.');
    _experiencesViewModel.experienceIndexSink.add(index);
    } else {
    _actionType = ActionType.none;
    }

    },
    );

    页面 Controller 被定义为类属性
    PageController _pageController = PageController(initialPage: 0, keepPage: false);

    最佳答案

    PageController包含可用于在页面之间动态切换的方法。

    // Create the page controller in your widget
    PageController _controller = PageController(initialPage: 0, keepPage: false);


    // Use it in your page view
    @override
    Widget build(BuildContext context) {
    ...
    PageView(controller: _controller, ...);
    ...
    }


    void onAddButtonTapped() {
    // add the new page
    ...

    // use this to animate to the page
    _controller.animateToPage(lastIdx);

    // or this to jump to it without animating
    _controller.jumpToPage(lastIdx);
    }

    关于Flutter:如何跳转到在 PageView 上动态创建的最后一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52506196/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com