android – The way to change “itemcount” and “preliminary web page” in PageView Builder in Flutter?

0
135


I’ve 3 recordsdata in my app. 1) important.dart 2) hompage.dart 3) knowledge.dart

There have a singleton class which maintain checklist of string.

Utilizing important.dart file i’m including random variety of strings to the the dataList of the singleton class in each 30 seconds.

in homepage there have pageview.builder, in each web page construct, SinglePage() widgets calls getData, and get a string in accordance a left-right web page swipes.

Notice : Person can solely swipe left at most 2 occasions..

important.dart

import 'dart:async';
import 'dart:convert';
import 'dart:math';
import 'bundle:flutter/materials.dart';
import 'bundle:rdx_test/knowledge.dart';
import 'homepage.dart';

void important() {
  WidgetsFlutterBinding.ensureInitialized();
  Random random = Random();
  DataClass dataClass = DataClass();
  generateStringList(random.nextInt(5)).forEach((noun) {dataClass.addData(noun.toString());});
  Timer(const Length(seconds: 30), () {
    generateStringList(random.nextInt(5)).forEach((noun) {dataClass.addData(noun.toString());});
  });
  runApp(const HomePage());
}


Listing<String> generateStringList(int listSize){
  Listing<String> stringList = [];
  var random = Random.safe();

  for(int i = 0; i < listSize; i ++){
    var values = Listing<int>.generate(5, (i) =>  random.nextInt(255));
    stringList.add(base64UrlEncode(values));
  }
  return stringList;
}

knowledge.dart

class DataClass {
  static closing DataClass _instance = DataClass._internal();
  Listing<String> dataList = [];
  int lastAccessIndex = 0;


  manufacturing facility DataClass() {
    return _instance;
  }
  DataClass._internal();

  void addData(String newData){
    dataList.add(newData);
  }
  String? getData(int index){
    if(index < lastAccessIndex){
      return dataList[lastAccessIndex--];
    }else if(index > lastAccessIndex) {
      return dataList[lastAccessIndex++];
    }else{
      return dataList[0];
    }
  }

}

homepage.dart

import 'bundle:flutter/materials.dart';
import 'bundle:rdx_test/knowledge.dart';

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : tremendous(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget construct(BuildContext context) {
    return MaterialApp(
      residence: Scaffold(
        physique: PageView.builder(
            itemBuilder: (context, index){
              return SinglePage(pageIndex: index);
            }
        ),
      ),
    );
  }
}

class SinglePage extends StatefulWidget {
  closing int pageIndex;
  const SinglePage({Key? key, required this.pageIndex}) : tremendous(key: key);

  @override
  State<SinglePage> createState() => _SinglePageState();
}

class _SinglePageState extends State<SinglePage> {
  late String phrase;
  @override
  void initState() {
    DataClass dataClass = DataClass();
    phrase =  dataClass.getData(widget.pageIndex)!;
    tremendous.initState();
  }
  @override
  Widget construct(BuildContext context) {
    return Heart(baby: Textual content(phrase));
  }
}

It provides me error as i’ve not talked about itemcount.
So i wish to make it dynamic, that each time new knowledge added to the checklist it might probably in a position to present knowledge or stops the precise swipe..

THANK YOU…
You can provide me suggestion that if this resolution may be completed in different manner… THANK YOU…