Home Blog Page 48

How to upgrade flutter SDK 2.10 & enable flutter windows desktop support

0
how to upgrade flutter to 2.11 for desktop support

Hi Guys, Welcome to Proto Coders Point. In this Flutter Article will learn how to upgrade flutter to 2.10 & enable flutter windows desktop support.

Video Tutorial to upgrade flutter SDK


Steps to Upgrade Flutter SDK version

First run command prompt at administration

1. flutter doctor:- Check if all the platform required dependencies is setup.

flutter doctor

flutter doctor

Here as you can see all the platform setup required is done, and showing No issue found!


2. flutter upgrade:- This will upgrade your flutter SDK version to latest version available i.e flutter 2.11.

flutter upgrade

flutter upgrade

We have successfully upgraded flutter SDK to 2.10.


Installing Visual Studio with Desktop development with C++

So as we are going to build windows app in flutter, we need visual studio with desktop development C++ package installed.

Therefore, open simple install visual studio installer and install it.

visual studio desktop development with C++

Enable windows desktop and create flutter platforms windows

Once you have successfully upgraded flutter 2.10 and install visual studio with desktop development.

Now, open any existing flutter project or create new and run below 2 commands to enable windows desktop support for your flutter app.

flutter config --enable-windows-desktop

flutter create --platform=windows,macos,linux .


Conclusion

Now, In this tutorial we have successfully upgraded flutter SDK to 2.10 and enabled desktop windows support.

How to add/create desktop windows support for existing flutter project

0
lutter window desktop build support

Hi Guys, Welcome to Proto Coders Point. In this article we add windows support for existing flutter project.

To create windows module i.e. desktop support for your flutter project, simple run the below commands in a terminal beening into root folder of your flutter project.

flutter create --platform=windows/macos/linux

Then above command will simple “create a new folder into your existing flutter project by adding windows support” in your flutter config.

After create desktop support for flutter windows you need to config & enable <platform> desktop

run below respective config –enable command as below:-

flutter config --enable-windows-desktop
 flutter config --enable-macos-desktop
 flutter config --enable-linux-desktop

Once you run the above commands, flutter config file will get changed by adding windows config flutter.

Hence now your flutter project has ability to build flutter windows app.

flutter build windows

Unable to find suitable visual studio toolchain – Flutter Doctor Issue Fixed

0
Unable to find suitable visual studio toolchain

Hi Guys, Welcome to Proto Coders Point. In this article let’s fix an Exception : unable to find suitable visual studio toolchain. please run flutter doctor for more details.

Flutter Windows Build Error

You will face Unable to find suitable Visual Studio toolchain error only when you try to run flutter app in windows Desktop. That’s because of 2 reasons.

  1. Visual Studio is Not Installed
  2. Desktop development with C++ ( Windows SDK ) workload, is not installed with Visual Studio.

Unable to find suitable visual studio toolchain

Please run flutter doctor

Unable to find suitable visual studio toolchain

As you can see here, it’s saying you to install Visual Studio – developer from Windows (Visual Studio Build Tools 2017 15.8.42), Visual Studio 2019 or later is required.

Please install the “Desktop development with C++” workload.

Just install Visual Studio and don’t forget to select “Desktop Development with C++”

Desktop Development with C++

Desktop development with C++ is required to be install in visual studio, if you want to build Flutter Windows Apps.

Now Restart Android Studio or your IDE.

Then run flutter doctor again in terminal

flutter doctor

flutter doctor all issues fixed
flutter doctor all issues fixed

How to show Slider in bottom sheet in flutter

0
Slider in Bottom Sheet
Slider in Bottom Sheet

Hi Guys, Welcome to Proto Coders Point. In this flutter tutorial, will learn how to implement range slider in bottom sheet of flutter app.

Flutter Slider

In fluttter a slider is a widget of material design that is used to set a range value, So basically slider widget in flutter is used as input widget by which user can simply drag the slider point to select the range value.

I have already wrote a complete article on Slider flutter where i have explained it in details



Range Slider in flutter Bottom Sheet

This article is only on “How to use range slider in bottom sheet flutter”.

Video Tutorial

Complete Code: main.dart

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(

        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  double height = 180;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Bottom Sheet with slider"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text("Height: ${height}", style: TextStyle(fontSize: 30),),
            ElevatedButton(
              onPressed: (){
                 showModalBottomSheet(context: context,
                     builder: (BuildContext context){
                       return Container(
                         height: 200,
                         child: Center(child:
                         StatefulBuilder(
                           builder: (context,state){
                             return Center(
                               child: Slider(
                                 value: height.toDouble(),
                                 max: 200,
                                 min: 50,
                                 onChanged: (double value) {
                                   state(() {
                                     height = value.round() as double;
                                   });
                                   setState(() {

                                   });
                                 },

                               ),
                             );
                           },
                         ),
                         ),
                       );
                     }
                 );
              },
              child: Text(" Select Height "),
            ),
          ],
        ),
      ),
    );
  }
}

Output

flutter show model bottom sheet

In above code, we have 2 widget Text & Elevated.

Text Widget will simple show there ranger value from range slider.

Elevated Button when pressed, will show model bottom sheet flutter.

Inside showModelBottomSheet popup, we are showing a slider where user can select the range. The same selected range value is show in Text Widget.

Flutter slider not moving / updating

Flutter show model bottom sheet is not a stateful widget that is the reason the widget inside it will not update, In our case slider is not moving even after setState() is called from bottom sheet will not rebuild.

Here is the solution

As you can see in above code, Slider widget is been wrapped with StatefulBuilder Widegt, that will have it own setState() to update/rebuild it child on change.

                         StatefulBuilder(
                           builder: (context,state){
                             return Center(
                               child: Slider(
                                 value: height.toDouble(),
                                 max: 200,
                                 min: 50,
                                 onChanged: (double value) {
                                  // rebuild this statefulbuilder
                                   state(() {
                                     height = value.round() as double;
                                   });
                                   setState(() {
                                    // this will rebuild the main 
                                   });
                                 },

                               ),
                             );
                           },
                         ),

How to create a search bar in flutter

0

Hi Guys, Welcome to Proto Coders Point. In this flutter tutorial, will learn how to create a search bar in fluttter appbar.

A search field i.e. a search bar is the must required feature to be provided to your flutter app user.

If you are android developer you might be knowing about searchview, searchbar in React Native & UISearchBar in iOS.

In flutter we have a searchDelegate class to implement searchfield in flutter.

How to add search bar in appbar flutter

In this tutorial, will add a search bar in flutter appbar, where a user can enter their search query & depending on query entered we can suggest quick search list is search bar.

Will make use of flutter showSearch + searchDelegate class

Let’s get started

Adding search bar in AppBar

After creating or opening flutter project, let’s add a search bar field in flutter appbar.

Scaffold(
      appBar: AppBar(
        title: Text("Search Bar"),
        actions: [
          IconButton(onPressed: (){
            // class search delegate here 
          }, icon: const Icon(Icons.search))
        ],
      ),
    );

Here in AppBar, I make use of actions property with has IconButton to show a search magnifying glass icon in appbar.


Flutter show search in Appbar

Now, When user click on search Icon, we must show a search TextField where user can enter his query to make a quick search.

Will use showSearch Delegate class.

showSearch is a widget that shows full screen search page and returns the search query result to the user

AppBar(
        title: Text("Search Bar"),
        actions: [
          IconButton(onPressed: (){
            showSearch(context: context, delegate: MySearch());
          }, icon: const Icon(Icons.search))
        ],
      ),

Here MySearch is a class name that we will called to perform search operation in flutter.


Create class that extends SearchDelegate

Search Delegate class extends 4 override methods, create then by clicking on warning suggestion.

The override widget methods are:-

1. buildLeading:-

  @override
  Widget? buildLeading(BuildContext context) => IconButton(
    icon: const Icon(Icons.arrow_back),
    onPressed: () { close(context, null) ; },
  );
leading back button in search bar flutter

buildLeading is used to show a backbutton in search bar, so that user can click on it to close search box page, Here arrow_back icon is used as you can see in screenshot above.


2. buildAction:

 @override
  List<Widget>? buildActions(BuildContext context) => [
    IconButton(
        icon: const Icon(Icons.clear,color: Colors.red,),
        onPressed: () {
          if(query.isEmpty){
            close(context, null);
          }else{
            query = '';
          }
        },
  ),
];
cancel close button in search bar flutter

buildAction is used to add some Action widgte in search bar so that use can click on it to perform some action, In out case will use cancel icon so that user can click & cancel it and close search field.


3. buildSuggestions:- flutter search bar with listview

 @override
  Widget buildSuggestions(BuildContext context) {

    List<String> suggesstions = data.where((element) {
      final result = element.toLowerCase();
      final input = query.toLowerCase();

      return result.contains(input);

    }).toList();

    return ListView.builder(
       itemCount: suggesstions.length,
        itemBuilder: (context,index){
          final suggestion = suggesstions[index];
          return ListTile(
            title: Text(suggestion),
            onTap: (){
              query = suggestion;

              showResults(context);
            },
          );
        }
    );
  }

buildSuggestion is used to give a quick suggestion for a search, Here as the user goes on typing user will get hints for his search query, so user can directly select from search box suggestion list as you can see in above screenshot.


buildResults:

@override
  Widget buildResults(BuildContext context) => Center(
    child: Text(
      query,   // query will hold user selected search query
    ),
  );

The result that is finalized after the user make his search query final submission & will be shown in search page as search result.


Complete Code of Search Delegate class

class MySearch extends SearchDelegate{

  List<String> data = [
    'A','B','C','D','E','F','G','H','I','J'
  ];

  @override
  List<Widget>? buildActions(BuildContext context) => [
    IconButton(
        icon: const Icon(Icons.clear,color: Colors.red,),
        onPressed: () {
          if(query.isEmpty){
            close(context, null);
          }else{
            query = '';
          }
        },
  ),
  ];

  @override
  Widget? buildLeading(BuildContext context) => IconButton(
    icon: const Icon(Icons.arrow_back),
    onPressed: () { close(context, null) ; },
  );

  @override
  Widget buildResults(BuildContext context) => Center(
    child: Text(
      query,

    ),
  );

  //flutter search bar with listview
  @override
  Widget buildSuggestions(BuildContext context) {

    List<String> suggesstions = data.where((element) {
      final result = element.toLowerCase();
      final input = query.toLowerCase();

      return result.contains(input);

    }).toList();

    return ListView.builder(
       itemCount: suggesstions.length,
        itemBuilder: (context,index){
          final suggestion = suggesstions[index];
          return ListTile(
            title: Text(suggestion),
            onTap: (){
              query = suggestion;

              showResults(context);
            },
          );
        }
    );
  }

}


Complete source – Flutter add search bar in appbar

main.dart

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home:  MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Search Bar"),
        actions: [
          IconButton(onPressed: (){
            showSearch(context: context, delegate: MySearch());
          }, icon: const Icon(Icons.search))
        ],
      ),
    );
  }
}

class MySearch extends SearchDelegate{
  // this is manual list of query for suggesting user.
  List<String> data = [
    'A','B','C','D','E','F','G','H','I','J'
  ];

  @override
  List<Widget>? buildActions(BuildContext context) => [
    IconButton(
        icon: const Icon(Icons.clear,color: Colors.red,),
        onPressed: () {
          if(query.isEmpty){
            close(context, null);
          }else{
            query = '';
          }
        },
  ),
  ];

  @override
  Widget? buildLeading(BuildContext context) => IconButton(
    icon: const Icon(Icons.arrow_back),
    onPressed: () { close(context, null) ; },
  );

  @override
  Widget buildResults(BuildContext context) => Center(
    child: Text(
      query,
    ),
  );

  @override
  Widget buildSuggestions(BuildContext context) {
     // will show a query hint suggestion "flutter search bar with listview"
    List<String> suggesstions = data.where((element) {

      final result = element.toLowerCase();
      final input = query.toLowerCase();

      return result.contains(input);

    }).toList();

    return ListView.builder(
       itemCount: suggesstions.length,
        itemBuilder: (context,index){
          final suggestion = suggesstions[index];
          return ListTile(
            title: Text(suggestion),
            onTap: (){
              query = suggestion;

              showResults(context);
            },
          );
        }
    );
  }
}


Flutter Auto Size Text – Make app Text Widget Responsive

0
flutter auto size text
flutter auto size text

Hi Guys, Welcome to Proto Coders Point. In this flutter tutorial will look into how to make text auto size, where our app can auto adjust text size depending of screensize.

An Responsive UI is a must required feature because now a days user uses wide range of devices with different screen size like Tablet, Mobile, PC.

A Developer named “Simon Leier“, has built a flutter package named “auto size text” that can auto adjust text size depending on text length & screen size.

Let’s learn about auto_size_text

A Flutter Widget that we can use to automatically resize texts to get fit into available space and get fit within it boundary defined.

Usage

1. Install:

Add the package in pubspec.yaml file in your flutter project

dependencies:
auto_size_text: ^3.0.0

2. Import it to use

You need to import it, on page where you want to use it eg: main.dart

import 'package:auto_size_text/auto_size_text.dart';

Syntax – Adding simple AutoSizeText Widget example

SizedBox(
          width: 200,
          height: 140,
          child: AutoSizeText(
            'This string will be automatically resized to fit in two lines.',

            style: TextStyle(fontSize: 30),
            maxLines: 2,
          ),
        ),

Here I have made use of SizedBox with width 200, height 140, The Auto Text widget in it will auto adjected with maxLines 2 as defined.

AutoSizeText Widget works & respond exactly like text, The different over here is text will auto resize depending on parent widget or screen size.


Properties of Auto Size Text Widget

maxLines: will work exactly like how it work with Text widget.

minFontSize: The possible smallest font size to fit. Text will not go smallest fontsize then defined, By default minFontSize is 12.

maxFontSize: The maximum posible fontsize.

wrapWords: when set to false, The font will not line break it will only decrease fontsize in same line.

overflowReplacement: Support if the text overflows and not fit in boundary, then replacement text or message can be shown to the user. Very useful if text got too small to read.

stepGranularity: define by show many unit the fontsize should re-size when screen size is sink or enlarged.
Eg: stepGranularity : 8, then font size will decreased by 8 or increased by 8.

AutoSizeText(
  'Welcome to Proto Coders Point, This tutorial is on how to make app responsive',
  style: TextStyle(fontSize: 40),
  minFontSize: 8,
  stepGranularity: 8,
  maxLines: 4,
  overflow: TextOverflow.ellipsis,
)

presetFontSize: Use when you have fixe fontsize, I mean you only allow certain specified fontsize Eg: presetFontSize : [30, 20, 10, 5], so here fontSize will be 30 then as screen get sink the fontsize get decreatsed to 20, then 10, 5.

AutoSizeText(
  'This text will get sink as per defined preset font size ',
  presetFontSizes: [30, 20, 10, 5],
  maxLines: 4,
)

Note: If presetFontSizes is been set, then minFontSizemaxFontSize and stepGranularity will be simply get ignored.


group: To apply same rules to multiple AutoSizeText Widget you can use group instance.

Create a group instance using AutoSizeGroup(),

Eg: final group = AutoSizeGroup().

Here I have created Instance of Auto Size Group that I can use further in multiple AutoSizeText.

final myGroup = AutoSizeGroup();

AutoSizeText(
  'Text 1',
  group: myGroup,
);

AutoSizeText(
  'Text 2',
  group: myGroup,
);

Flutter RichText

The Text with custom text style & font from highlighting. To make RichText with Auto Resize ability will be a great feature. Therefore to add richtext with autoSize, we must use addition constructor AutoSizeText.rich() widget.

RichText Example

flutter richtext auto resize
SizedBox(
          width: 200,
          height: 300,
          child: AutoSizeText.rich(
            TextSpan(
              text: "This is ",
              children: <TextSpan>[
                TextSpan(
                  text: "Example",
                  style: TextStyle(fontSize: 20)
                ),
                TextSpan(
                    text: " on Flutter",

                ),
                TextSpan(
                  text: "Auto Size Text Rich ",
                    style: TextStyle(fontSize: 22,color: Colors.redAccent)
                ),
                TextSpan(
                  text: "Widget",

                ),
              ]
            )
          )
        )

In above RichText, it take a TextSpan & will have multiple textSpan children with different fontSize & style.