Code Optimization

Interesting things about software development and code optimization

Legion Y540-15IRH-PG0 Laptop Lenovo - Type 81SY - Camera Issue

Hi friends,


I have updated BIOS as it was recommended by Lenovo support tools/website and after that I started experiencing an issue with integrated web camera. Windows 10 x64 started to show it as: 


Unknown USB Device (Configuration Descriptior Request Failed)
Windows has stopped this device because it has reported problems. (Code 43)
A request for the USB configuration descriptor failed.

Port_#0006.Hub_#0001
(Standard USB Host Controller)
Universal Serial Bus controllers
Device USB\VID_0000&PID_0003\5&1f456139&0&6 was configured
Class Guid: {36fc9e60-c465-11cf-8056-444553540000}


Re-installing all official drivers did not help me so I entered into BIOS and found such an option for USB like LEGACY support.

Disabling the LEGACY USB option in BIOS and re-installing official Lenovo Intel Chipset Device Software (Run as Administrator) did solve my issue.


Thank you and see you :)





1vqHSTrq1GEoEF7QsL8dhmJfRMDVxhv2y



Flutter - Updating TextField Selection/Caret Problem and comma for thousand separator

Hello friends,


I'm going to show you how I resolved the selection/caret updating issue for Flutter TextField when cursor goes to the very beginning instead of the position you specify.


I was creating a money formating text field and faced this issue as many of us and here is how I resolved it:

  
String odlValue="";

  @override
  void initState() {
    tbPurchasePrice.addListener((){
      var cursor = tbPurchasePrice.selection;
      String val = tbPurchasePrice.text;
      if((odlValue.length < val.length && val.length > 3) || 
          (odlValue.length > val.length && val.length > 4)){
        val = val.replaceAll(",""");
        val = val.substring(0, val.length - 3) + "," + val.substring(val.length - 3);
        int d = val.length - tbPurchasePrice.text.length;
        odlValue = val;
        tbPurchasePrice.text = val;
        Future.delayed(
        Duration(milliseconds: 5),
         () {
              tbPurchasePrice.selection = new TextSelection(
baseOffset: cursor.baseOffset + d, 
               extentOffset: cursor.extentOffset + d, 
               affinity: cursor.affinity, isDirectional: cursor.isDirectional);  
          });
      }
      else if(odlValue.length > val.length && val.length <= 4 && val.indexOf(",") >= 0){
        tbPurchasePrice.text = val.replaceAll(",""");
        odlValue = val;
        Future.delayed(
        Duration(milliseconds: 5),
         () {
              tbPurchasePrice.selection = new TextSelection(
baseOffset: cursor.baseOffset - 1
               extentOffset: cursor.extentOffset - 1
               affinity: cursor.affinity, isDirectional: cursor.isDirectional);  
          });
      }
      odlValue = val;
    });

    super.initState();
  }

.......

child: TextFormField(
       decoration: InputDecoration( prefix: Text(r"$")),
       controller: tbPurchasePrice, 
keyboardType: TextInputType.numberWithOptions(decimal: true),
       style: Theme.of(context).textTheme.headline,
   )
.......


so it puts $ sign on the beginning, then it puts comma in the first thousand place and it use delayed function call to set/update the selection property so it would not go to the beginning of the textfield but to the same place where it has been before text updating or where you need it to be.



Thank you and see you ;)




1vqHSTrq1GEoEF7QsL8dhmJfRMDVxhv2y



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