Changes not reflecting on adding onto a list in flutter

Viewed 235
import '../../Data/expansionbutton.dart';
import 'package:flutter/material.dart';
import '../../Data/Globalvariable.dart' as global;
import 'menu.dart';

class Textbtn extends StatefulWidget {
  final Widget menu = Text('Add Pin');
  @override
  _TextbtnState createState() => _TextbtnState(menu: menu);
}

class _TextbtnState extends State<Textbtn> {
  Widget menu;
  bool expn = false;
  _TextbtnState({this.menu});
  String pin;
  void addfun() {
    setState(() {
      global.pincode.add(pin);
      menu = MenuBar();
      expn = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    return ExpnButton(
      expanded: expn,
      wiget: SizedBox(
        height: MediaQuery.of(context).size.height - 400,
        width: MediaQuery.of(context).size.width - 10,
        child: Padding(
          child: menu,
          padding: EdgeInsets.all(19),
        ),
      ),
      upperwig: SizedBox(
        height: 80,
        child: TextField(
          onChanged: (value) {
            pin = value;
          },
          maxLength: 6,
          keyboardType: TextInputType.number,
          decoration: InputDecoration(
              icon: Icon(Icons.pin_drop),
              suffix: TextButton(
                style: TextButton.styleFrom(
                  primary: Colors.blue,
                ),
                onPressed: () {
                  addfun();
                },
                child: Text('Add'),
              ),
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(20.0),
              ),
              hintText: " Enter pin code",
              labelText: "Pin Code"),
        ),
      ),
    );
  }
}

I am trying to take pin from the user and show the selected pin to the user while doing that I created a list globally but when I change list via setstate the changes doesn't seems to work at all

Globalvariable.dart
library project.globals;
List<String> pincode = ['Select Pin',];

here is the file for menubar()

menu.dart

import 'package:flutter/material.dart';
import '../../Data/Globalvariable.dart' as global;
import 'package:flutter/foundation.dart';

class MenuBar extends StatefulWidget {
  @override
  MenuBarState createState() {
    return MenuBarState();
  }
}


class MenuBarState extends State<MenuBar> {
  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: global.pincode.length,
      itemBuilder: (context, index) {
        final item = global.pincode[index];
        return Dismissible(
          key: Key(item),
          onDismissed: (direction) {
            setState(() {
              global.pincode.removeAt(index);
            });
            ScaffoldMessenger.of(context)
                .showSnackBar(SnackBar(content: Text('$item Removed')));
          },
          background: Container(color: Colors.red),
          child: ListTile(
            title: Text(item),
          ),
        );
      },
    );
  }
}

All i want is to get the input from the user and show it on expansion but my code is able to take the input and make changes to global.pincode as i can see in debug console but the changes doesn't seems to change the state or rather the listview of buttonsenter image description here

enter image description here

2 Answers

User Valuelistenablebuilder in your list & pass your array/list as value in Valuelistenablebuilder. It will update the list if there is any change happened.

well I figured out the answer I used changenotifier created a class which extends changenotifier and put all the variable that changes in that class then added all the statement which changes the data in a consumer and the widget which i need to rebuild in provider and it works perfectly

Related