How to navigate to one page to another page using textfield in flutter?

1

I have added 62 pdf pages in ListView.

class MyApp extends StatefulWidget {
  String _pdfPath;
  int currentPage;
  ScrollController Controller = ScrollController();


  set PdfPath(String path) {
    if (path != _pdfPath) {
      _pdfPath = path;
    }
  }

  @override
  _NativePdfState createState() => _NativePdfState(_pdfPath,currentPage);
}

class _NativePdfState extends State<MyApp> {
  String _pdfPath;
  int _currentPage;
  BottomToolbar bottomToolbar = new BottomToolbar();

  _NativePdfState(String pdfPath,int currentPage) {
    _pdfPath = pdfPath;
    _currentPage= currentPage;
  }
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Column(children: [
      Expanded(
        child: FutureBuilder(
            future: Pdfviewerplugin.getImages(_pdfPath),
            builder: (BuildContext context, AsyncSnapshot snapshot) {
              if (snapshot.hasData) {
                int listLength = (snapshot.data as List).length;

                return ScrollBar(
                  currentPage: (offset) {
                    final int currentItem = widget.Controller.hasClients
                        ? (widget.Controller.offset / widget.Controller.position.maxScrollExtent * listLength).floor() : 0;
                    _currentPage = currentItem;
                    print(currentItem);
                    if (currentItem + 1 == 63) {
                      return 62;
                    }
                    return currentItem + 1;
                  },
                  controller: widget.Controller,
                  child: ListView.builder(
                      controller: widget.Controller,
                      itemCount: listLength,
                      itemBuilder: (BuildContext context, int index) {
                        return Center(
                          child: Builder(builder: (context) {
                            if (index == (snapshot.data as List).length - 1) {
                              return Container(
                                //margin:  EdgeInsets.fromLTRB(0, 0, 0, 5),
                                child: (snapshot.data[index]),
                              );
                            } else {
                              return Container(
                                margin: EdgeInsets.fromLTRB(0, 0, 0, 5),
                                child: (snapshot.data[index]),
                              );
                            }
                          }),
                        );
                      }),
                );
              } else {
                return Center(
                  child: CircularProgressIndicator(),
                );
              }
            }),
      ),
      Expanded(
        child: bottomToolbar,
        flex: 0,
      ),
    ]);
  }
}

Then I have added textfield in bottomtoolbar,

class PageEntry extends StatefulWidget {
  final Color foregroundColor;
  final Color backgroundColor;
  final double height;
  final double width;
  final bool isVisible;
  PageEntry({
    this.height = 24,
    this.width = 35,
    this.isVisible = true,
    this.backgroundColor = const Color(0xFFFFFFFF),
    this.foregroundColor,
  });
  @override
  _PageEntryState createState() => _PageEntryState();
}
class _PageEntryState extends State<PageEntry> {
  var currentPage;
    TextEditingController _controller = TextEditingController();

    void initState() {
      super.initState();
      _controller.addListener(() {
                int currentPageNumber = int.parse(_controller.text);         
      });
    }
void onSumbitted(String value){
      setState(() {
        int currentPageNumber = int.parse(_controller.text);
     });
}
    void dispose() {
      _controller.dispose();
      super.dispose();
    }

    @override
    Widget build(BuildContext context) {
      if (widget.isVisible) {
        return Container(
          height: widget.height,
          width: widget.width,
          color: widget.backgroundColor,
          foregroundDecoration: BoxDecoration(
            border: Border.all(color: Colors.grey),
          ),
          child: TextField(
            keyboardType: TextInputType.number,
            textAlign: TextAlign.center,
            controller: _controller,
            onSubmitted: onSumbitted,
          ),
        );
      } else {
        return Container();
      }
    }
  }

If I give any number(within 62) to textfield,page should be scrolled to that given page. For example,if i give 3, page should be scrolled to 3rd page.

Then If i scrolled to any page, that page number should be showed in textfield. For example, If I scrolled to 6th page, 6 should be showed in textfield.

I dont know how to do this.Please help me to solve this.

flutter
dart
scroll
asked on Stack Overflow Jun 29, 2020 by Sarath Kumar

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0