How To Use Stream Builder With Dropdown Using Flutter App

admin_img Posted By Bajarangi soft , Posted On 27-11-2020

Widget rebuilding is scheduled by each interaction, using State.setState, but is otherwise decoupled from the timing of the stream. The builder is called at the discretion of the Flutter pipeline, and will thus receive a timing-dependent sub-sequence of the snapshots that represent the interaction with the stream.

How To Use Stream Builder With Dropdown Using Flutter App

Stream Builder With Dropdown 
Comlpete Code For Stream Builder With Dropdown  In Flutter
main.dart

import 'dart:async';
import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: MyHomePage(),
    );
  }
}
class MyHomePage extends StatefulWidget {
  final String title;
  MyHomePage({this.title});
  @override
  _MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
  Stream<int> timedCounter(Duration interval, [int maxCount]) async* {
    int i = 0;
    while (true) {
      await Future.delayed(interval);
      yield i++;
      if (i == maxCount) break;
    }
  }
  Widget _buildDropwDown(int count) {
    List<DropdownButton> dropdowns = List.generate(count, (i) {
      return DropdownButton<int>(
        items: [
          DropdownMenuItem<int>(
            child: Text("Flutter"),
            value: 1,
          ),
          DropdownMenuItem<int>(
            child: Text("Android"),
            value: 2,
          ),
          DropdownMenuItem<int>(
            child: Text("Dart"),
            value: 3,
          ),
          DropdownMenuItem<int>(
            child: Text("Ios"),
            value: 4,
          ),
        ],
        isExpanded: false,
        onChanged: (int val) {},
        hint: Text('Select Website'),
      );
    });
    return ListView(
      children: dropdowns,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.pink[700],
          title: Text("Stream Builder With Dropdown")),
      body: StreamBuilder<int>(
        stream: timedCounter(Duration(seconds: 2), 10),
        builder: (context, snapshot) {
          if (!snapshot.hasData) {
            return Text("No data");
          }
          return _buildDropwDown(snapshot.data);
        },
      ),
    );
  }
}

 

Related Post