Skip to main content

Advance Flutter Code Snippet | Learn Flutter by Code

 Dropdownlist Example in Flutter:

import 'package:flutter/foundation.dart';

import 'package:flutter/material.dart';

class DropdownDemo extends StatefulWidget {

  const DropdownDemo({Key? key}) : super(key: key);

  @override

  State<DropdownDemo> createState() => _DropdownDemoState();

}

class _DropdownDemoState extends State<DropdownDemo> {

  String dropdownValue = 'Area of circle';

  @override

  Widget build(BuildContext context) {

    return Scaffold(

      appBar: AppBar(title: const Text("DDDD")),

      body: Center(

        child: Column(

          children: [


            SizedBox(

              height: 50,

            ),

            // Step 2.

            DropdownButton<String>(

              // Step 3.

              value: dropdownValue,

              // Step 4.

              items: <String>['Area of circle', 'Area of Rectangle', 'Area of triangle ']

                  .map<DropdownMenuItem<String>>((String value) {

                return DropdownMenuItem<String>(

                  value: value,

                  child: Text(

                    value,

                    style: TextStyle(fontSize: 30),

                  ),

                );

              }).toList(),

              // Step 5.

              onChanged: (String? newValue) {

                setState(() {

                  dropdownValue = newValue!;

                });

              },

            ),

            SizedBox(

              height: 20,

            ),

            Text(

              'Selected Value: $dropdownValue',

              style: TextStyle(fontSize: 30, fontWeight: FontWeight.bold),

            ),


          ],

        ),

      ),

    );

  }

}


Richtext example in flutter:

import 'package:flutter/gestures.dart';

import 'package:flutter/material.dart';

class RitchTextExample extends StatelessWidget

{

  @override

  Widget build(BuildContext context) {

    return Scaffold(

     appBar: AppBar(title: Text("Richtext example")),

      body:Container(

    child: Center(

    child: RichText(

    text: TextSpan(

    text: 'Don\'t have an account',

    style: TextStyle(color: Colors.black, fontSize: 20),

    children: <TextSpan>[

    TextSpan(text: ' Sign up',

    style: TextStyle(color: Colors.blueAccent, fontSize: 20),

    recognizer: TapGestureRecognizer()

    ..onTap = () {}

    )

    ]

    ),

    )

      )

    ));

  }

  }

 Slider Widget in Flutter:

import 'package:flutter/material.dart';


class SliderImage extends StatelessWidget {

  @override

  Widget build(BuildContext context) {

    return Scaffold(

      appBar: AppBar(title: const Text("Image slider")),

      body: const SliderWidget(),

    );

  }

}


class SliderWidget extends StatefulWidget {

  const SliderWidget({super.key});


  @override

  State<SliderWidget> createState() => _SliderWidgetState();

}


class _SliderWidgetState extends State<SliderWidget> {

  double _currentSliderValue = 0;

  double _imageSize = 50; // Initial image size


  @override

  Widget build(BuildContext context) {

    return Center(

      child: Column(

        mainAxisAlignment: MainAxisAlignment.center,

        children: <Widget>[

          Text(

            'Slider Value: $_currentSliderValue',

            style: TextStyle(fontSize: 20),

          ),

          SizedBox(height: 20),

          Container(

            child: Image.asset("assets/images/congress.png"),

            width: _imageSize,

            height: _imageSize, // Change color as needed

          ),

          SizedBox(height: 20),

          Slider(

            value: _currentSliderValue,

            min: 0,

            max: 100,

            divisions: 10,

            label: _currentSliderValue.round().toString(),

            onChanged: (double value) {

              setState(() {

                _currentSliderValue = value;

                // Update image size based on slider value

                _imageSize = 50 + value; // Adjust this formula as needed

              });

            },

          ),

        ],

      ),

    );

  }

}

Snackbar Example in dart:

import 'package:flutter/material.dart';


class SnackBarPage extends StatelessWidget {

  @override

  Widget build(BuildContext context) {

    return

      Scaffold(

        appBar: AppBar(

          title: Text('Flutter SnackBar Demo'),

        ),

        body: Center(

      child: TextButton(

        child: Text(

          'Show SnackBar',

          style: TextStyle(fontSize: 25.0),

        ),

        onPressed: () {

          final snackBar = SnackBar(

            content: Text('Hey! This is a SnackBar message.'),

            duration: Duration(seconds: 5),

            action: SnackBarAction(

              label: 'Undo',

              onPressed: () {

                // Some code to undo the change.

              },

            ),

          );

          ScaffoldMessenger.of(context).showSnackBar(snackBar);

        },

      ),

    )

      );

  }

}

Tabbar Example in dart:

import 'package:flutter/foundation.dart';

import 'package:flutter/material.dart';

class TabbarExample extends StatefulWidget {

  const TabbarExample({Key? key}) : super(key: key);

  @override

  State<TabbarExample > createState() => _MyCardExampleState();

}

class FirstScreen extends StatelessWidget {

  @override

  Widget build(BuildContext context) {

    return Container(

      child: Center(

          child: Text('It is a contact tab, which is responsible for displaying the contacts stored in your mobile',

            style: TextStyle(fontSize: 32.0),

          )

      ),

    );

  }

}


class SecondScreen extends StatelessWidget {

  @override

  Widget build(BuildContext context) {

    return Container(

      child: Center(

        child: Text('It is a second layout tab, which is responsible for taking pictures from your mobile.',

          style: TextStyle(fontSize: 35.0),

        ),

      ),

    );

  }

}

class _MyCardExampleState extends State<TabbarExample> {

  String dropdownValue = 'Mango';

  @override

  Widget build(BuildContext context) {

    return DefaultTabController(

      length: 3,

      child: Scaffold(

        appBar: AppBar(

          title: Text('Flutter Tabs Demo'),

          bottom: TabBar(

            tabs: [

              Tab(icon: Icon(Icons.contacts), text: "Tab 1"),

              Tab(icon: Icon(Icons.camera_alt), text: "Tab 2"),

              Tab(icon: Icon(Icons.camera_alt), text: "Tab 3")

            ],

          ),

        ),

        body: TabBarView(

          children: [

            FirstScreen(),

            SecondScreen(),

            SecondScreen()

          ],

        ),

      ),

    );

  }

}


Tooltip Example in Flutter:

import 'package:flutter/material.dart';


class ToolTipExample extends StatelessWidget {

  @override

  Widget build(BuildContext context) {

    return

      Scaffold(

          appBar: AppBar(

            title: Text('Flutter SnackBar Demo'),

          ),

          body: Center(

            child: Tooltip(

              message: 'This is a tooltip',

              child: ElevatedButton(

                onPressed: () {

                  // Button action

                },

                child: Text('Hover over me'),

              ),

            ),

          )

      );

  }

}

Progress bar example in flutter:

import 'dart:async';


import 'package:flutter/material.dart';

class ProgressBarDemo extends StatefulWidget {

  @override

  _ProgressBarDemoState createState() => _ProgressBarDemoState();

}

class _ProgressBarDemoState extends State<ProgressBarDemo> {

  double _progressValue = 0.0;

  bool _loading = false;


  void _startProgress() {

    setState(() {

      _loading = true;

    });


    // Simulate some async operation

    Future.delayed(Duration(seconds: 10), () {

      setState(() {

        _loading = false;

        _progressValue = 0.0; // Reset progress when done

      });

    });


    // Update progress every 100 milliseconds

    const progressIncrement = 0.01;

    Timer.periodic(Duration(milliseconds: 100), (timer) {

      setState(() {

        if (_progressValue < 1.0) {

          _progressValue += progressIncrement;

        } else {

          timer.cancel();

        }

      });

    });

  }


  @override

  Widget build(BuildContext context) {

    return Scaffold(

      appBar: AppBar(

        title: Text('Progress Bar Demo'),

      ),

      body: Center(

        child: _loading

            ? LinearProgressIndicator(value: _progressValue)

            : ElevatedButton(

          onPressed: _startProgress,

          child: Text('Start Progress'),

        ),

      ),

    );

  }

}


Linear Progress bar example in Flutter:

import 'package:flutter/material.dart';

class ProgressbarExampleWidget extends StatefulWidget {

  @override

  _ProgressbarExampleWidgetState createState() => _ProgressbarExampleWidgetState();

}


class _ProgressbarExampleWidgetState extends State<ProgressbarExampleWidget> {

  bool _loading = false;


  @override

  Widget build(BuildContext context) {

    return Scaffold(

      appBar: AppBar(

        title: Text('Progress Bar Example'),

      ),

      body: Center(

        child: Container(

          padding: EdgeInsets.all(12.0),

          child: _loading

              ? LinearProgressIndicator()

              : Text("Press button for downloading", style: TextStyle(fontSize: 25)),

        ),

      ),

      floatingActionButton: FloatingActionButton(

        onPressed: () {

          setState(() {

            _loading = !_loading;

          });

        },

        tooltip: 'Download',

        child: Icon(Icons.cloud_download),

      ),

    );

  }

}

Range slider example in flutter:

import 'package:flutter/material.dart';

class RangeSliderDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("Sum Program")),
      body: const RangeSliderWidget(),

    );
  }
}
class RangeSliderWidget extends StatefulWidget {
  const RangeSliderWidget({super.key});

  @override
  State<RangeSliderWidget> createState() => _RangeSliderWidgetState();
}

class _RangeSliderWidgetState extends State<RangeSliderWidget> {
  RangeValues _currentRangeValues = const RangeValues(20, 60);
  @override
  Widget build(BuildContext context) {
    return Center(

          child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              mainAxisSize: MainAxisSize.max,
              children: [
                Icon(
                  Icons.volume_up,
                  size: 40,
                ),
                new Expanded(
                    child: RangeSlider(
                      values: _currentRangeValues,
                      min: 0,
                      max: 100,
                      divisions: 10,
                      labels: RangeLabels(
                        _currentRangeValues.start.round().toString(),
                        _currentRangeValues.end.round().toString(),
                      ),
                      onChanged: (RangeValues values) {
                        setState(() {
                          _currentRangeValues = values;
                        });
                      },
                    )
                ),
              ]
          )
      );


  }
}

Comments

Popular posts from this blog

DSA in C# | Data Structure and Algorithm using C#

  DSA in C# |  Data Structure and Algorithm using C#: Lecture 1: Introduction to Data Structures and Algorithms (1 Hour) 1.1 What are Data Structures? Data Structures are ways to store and organize data so it can be used efficiently. Think of data structures as containers that hold data in a specific format. Types of Data Structures: Primitive Data Structures : These are basic structures built into the language. Example: int , float , char , bool in C#. Example : csharp int age = 25;  // 'age' stores an integer value. bool isStudent = true;  // 'isStudent' stores a boolean value. Non-Primitive Data Structures : These are more complex and are built using primitive types. They are divided into: Linear : Arrays, Lists, Queues, Stacks (data is arranged in a sequence). Non-Linear : Trees, Graphs (data is connected in more complex ways). Example : // Array is a simple linear data structure int[] number...

JSP Page design using Internal CSS

  JSP is used to design the user interface of an application, CSS is used to provide set of properties. Jsp provide proper page template to create user interface of dynamic web application. We can write CSS using three different ways 1)  inline CSS:-   we will write CSS tag under HTML elements <div style="width:200px; height:100px; background-color:green;"></div> 2)  Internal CSS:-  we will write CSS under <style> block. <style type="text/css"> #abc { width:200px;  height:100px;  background-color:green; } </style> <div id="abc"></div> 3) External CSS:-  we will write CSS to create a separate file and link it into HTML Web pages. create a separate file and named it style.css #abc { width:200px;  height:100px;  background-color:green; } go into Jsp page and link style.css <link href="style.css"  type="text/css" rel="stylesheet"   /> <div id="abc"> </div> Exam...

Uncontrolled form input in React-JS

  Uncontrolled form input in React-JS? If we want to take input from users without any separate event handling then we can uncontrolled the data binding technique. The uncontrolled input is similar to the traditional HTML form inputs. The DOM itself handles the form data. Here, the HTML elements maintain their own state that will be updated when the input value changes. To write an uncontrolled component, you need to use a ref to get form values from the DOM. In other words, there is no need to write an event handler for every state update. You can use a ref to access the input field value of the form from the DOM. Example of Uncontrolled Form Input:- import React from "react" ; export class Info extends React . Component {     constructor ( props )     {         super ( props );         this . fun = this . fun . bind ( this ); //event method binding         this . input = React . createRef ();...