Maryna Azeez
Maryna Azeez

Reputation: 69

Why is not picking a value and printing it (flutter)?

where is my error is it in the function or in the printing? I am facing a problem in picking the value from the picker and printing it.

void _showPickerArray() {

    new Picker(
        adapter: PickerDataAdapter<String>(
            pickerdata: new JsonDecoder().convert(PickerData2), isArray: true),
        hideHeader: true,
        title: new Text("Please Select"),
        onConfirm: (Picker picker, List value) {
          if (picker == null) {
            return;
          }
          setState(() {
            _selectedpicker = picker;
          });
        }).showDialog(context);
  }

   Container(
         height: 55,
            child: Row(
               children: <Widget>[
                  Expanded(
                     child: Text(_selectedpicker == null
                          ? 'No Category Chosen!'
                          : 'Picked Category: $_selectedpicker'),
               ),
                AdapativeFlatButton('Choose Category', _showPickerArray)
              ],
            ),
          ),

Upvotes: 0

Views: 326

Answers (1)

chunhunghan
chunhunghan

Reputation: 54367

You can copy paste run full code below
You can display values with _selectedpicker.getSelectedValues()

code snippet

_selectedpicker == null
                ? Text("No Category Chosen")
                : Expanded(
                    child: ListView.builder(
                        padding: const EdgeInsets.all(8),
                        itemCount: _selectedpicker.getSelectedValues().length,
                        itemBuilder: (BuildContext context, int index) {
                          return Container(
                            height: 20,
                            child: Center(
                                child: Text(
                                    ' ${_selectedpicker.getSelectedValues()[index].toString()}')),
                          );
                        }),
                  ),

working demo

enter image description here

full code

import 'package:flutter/material.dart';
import 'package:flutter_picker/flutter_picker.dart';
import 'dart:convert';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  Picker _selectedpicker;

  showPickerArray(BuildContext context) {
    Picker(
        adapter: PickerDataAdapter<String>(
          pickerdata: JsonDecoder().convert(PickerData2),
          isArray: true,
        ),
        hideHeader: true,
        selecteds: [3, 0, 2],
        title: Text("Please Select"),
        selectedTextStyle: TextStyle(color: Colors.blue),
        cancel: FlatButton(
            onPressed: () {
              Navigator.pop(context);
            },
            child: Icon(Icons.child_care)),
        onConfirm: (Picker picker, List value) {
          print(value.toString());
          print(picker.getSelectedValues());
          setState(() {
            _selectedpicker = picker;
          });
        }).showDialog(context);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            _selectedpicker == null
                ? Text("No Category Chosen")
                : Expanded(
                    child: ListView.builder(
                        padding: const EdgeInsets.all(8),
                        itemCount: _selectedpicker.getSelectedValues().length,
                        itemBuilder: (BuildContext context, int index) {
                          return Container(
                            height: 20,
                            child: Center(
                                child: Text(
                                    ' ${_selectedpicker.getSelectedValues()[index].toString()}')),
                          );
                        }),
                  ),
            RaisedButton(
              child: Text('Picker Show (Array)'),
              onPressed: () {
                showPickerArray(context);
              },
            ),
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

const PickerData = '''
[
    {
        "a": [
            {
                "a1": [
                    1,
                    2,
                    3,
                    4
                ]
            },
            {
                "a2": [
                    5,
                    6,
                    7,
                    8,
                    555,
                    666,
                    999
                ]
            },
            {
                "a3": [
                    9,
                    10,
                    11,
                    12
                ]
            }
        ]
    },
    {
        "b": [
            {
                "b1": [
                    11,
                    22,
                    33,
                    44
                ]
            },
            {
                "b2": [
                    55,
                    66,
                    77,
                    88,
                    99,
                    1010,
                    1111,
                    1212,
                    1313,
                    1414,
                    1515,
                    1616
                ]
            },
            {
                "b3": [
                    1010,
                    1111,
                    1212,
                    1313,
                    1414,
                    1515,
                    1616
                ]
            }
        ]
    },
    {
        "c": [
            {
                "c1": [
                    "a",
                    "b",
                    "c"
                ]
            },
            {
                "c2": [
                    "aa",
                    "bb",
                    "cc"
                ]
            },
            {
                "c3": [
                    "aaa",
                    "bbb",
                    "ccc"
                ]
            },
            {
                "c4": [
                    "a1",
                    "b1",
                    "c1",
                    "d1"
                ]
            }
        ]
    }
]
    ''';

const PickerData2 = '''
[
    [
        1,
        2,
        3,
        4
    ],
    [
        11,
        22,
        33,
        44
    ],
    [
        "aaa",
        "bbb",
        "ccc"
    ]
]
    ''';

Upvotes: 1

Related Questions