Code Optimization

Interesting things about software development and code optimization

Flutter - SingleChildScrollView with continuous scrolling pages

Hi friends,


I was recently developing some mobile app in flutter and was requested to create auto-paging on scrolling.

So when user scrolls to the bottom it would go to/append the next page of content and this is inside of the simple 

SingleChildScrollView (of course as I was already using the SingleChildScrollView as main container :) ).


We have the scroll controller property but what it gives us? Offset and scroll direction in general but how we will use it to make paging?

We need to detect the bottom of the current page list and we should allow user to interact with last item in the current list before going to or appending a new page.


So here is my solution for it:


  ScrollController _controller;
  int bottomOutOfRange = 0;

  _scrollListener() {
    if (_controller.offset > 0.0 &&
        _controller.position.maxScrollExtent > 0.0) {
      if (_controller.offset >= _controller.position.maxScrollExtent &&
          !_controller.position.outOfRange) {
        if (++bottomOutOfRange >= 2) {
          bottomOutOfRange = 0;

          setState(() {
            if (curPage < (totalPages - 1)) {
              curPage++;
              _pbVisible = true;
              SearchCars();
            } else {
              curPage = 0;
              _pbVisible = true;
              SearchCars();
            }
          });
        } else {
          _controller.animateTo(_controller.offset - 5,
              duration: Duration(milliseconds: 500), curve: Curves.easeIn);
        }
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _context,
      appBar: CustomAppBar(),
      body: Container(
          margin: EdgeInsets.only(bottom: 82.0),
          child: SingleChildScrollView(
              controller: _controller,
....


First time user srolls to the very bottom of a page we bounce a little bit back and increment our counter, now if user really whishes to scroll down and see the next page our counter will be triggered one more time and we will know that it will be time to go to/append the next page.


Thank you and see you :)

 


1vqHSTrq1GEoEF7QsL8dhmJfRMDVxhv2y



Android Xml Layout To Flutter Scaffold Converter - Completely free

Simple but may be useful

Android Xml Layout To Flutter Scaffold Converter - Completely free


If you need to quickly convert most of your Android XML layouts into Dart Scaffold files then you can just try this online free converter. It is not perfect and does not support all Android UI elements and containers but in most cases it will make your life easier and create much DART files for you to start migrating your Java UI to Flutter UI.


Android Xml Layout to Flutter Scaffold Converter


1vqHSTrq1GEoEF7QsL8dhmJfRMDVxhv2y



No suitable Android AVD system images are available - Flutter and VS Code

Hello friends,


I was trying to setup and run an android emulator for my Flutter project on Visual Studio Code IDE and got the following error:

No suitable Android AVD system images are available. You may need to install these using sdkmanager, for example: sdkmanager "system-images;android-27;google_apis_playstore;x86"

So steps to solve this issue are the following:

1. Check if there is the path under PATH System Environment Variable: C:\Users\User1\AppData\Local\Android\sdk\tools\bin

      if not then add it pointing to your ANDROID SDK TOOL BIN location

2. Run Windows Powershsll (Admin) and run the command: sdkmanager "system-images;android-27;google_apis_playstore;x86"

3. Wait until it is finished and run the Create Android Emulator in Visual Studio Code


Now everything should work and you should be able to create a new emulator :)


Enjoy!


1vqHSTrq1GEoEF7QsL8dhmJfRMDVxhv2y



Ratee - rate through the World

Ratee - rate everything and everywhere


With the Ratee app you can rate everything and everywhere.

You like a pub - give it 5 starts and let other people and tourists know about it.

You do not like a pub or a service - give it minimal rate and share it with other people and tourists.



Facebook page

Windows phone 10



Android



Official page and download links


Feedbacks and ratings are welcome! :)


Thank you


1vqHSTrq1GEoEF7QsL8dhmJfRMDVxhv2y