Search code examples
flutterflutter-pageview

Flutter - PageView - Add page dynamically - The view not getting updated


I need to create an extendable PageView. The problem is the view is not updated automatically by running the code. It did after hot reload. With below example, it's just showing a white blank single page. If initially there are at least two pages before I add another one, I cannot swipe to the new added page before swiping a little bit to the left first. That's weird. It's not about Provider. Same goes without it.

class PageViewExampleState extends State<View>{

  PageController controller = PageController(initialPage : 0);

  @override
  Widget build(BuildContext context) => PageView(controller : controller, children : context.watch<Provider>().pages);

  @override
  void initState(){
    init();
  }

  Future<void> init() async{
    await Future.delayed(Duration(seconds : 1));
    await next(0);
    await Future.delayed(Duration(seconds : 1));
    await next(1);
    await Future.delayed(Duration(seconds : 1));
    await next(2);
  }

  Future<void> next(int i) async => context.read<Provider>().addpage(await getpage(i));

  Future<Widget> getpage(int i) async => await Container(color : [Colors.red, Colors.blue, Colors.green][i]);
}

Solution

  • Sorry for asking too quickly. PageView.builder is the answer.

    Flutter pageview, add new elements at runtime