0
votes

I've created a FlatButton in one of my Widget Screens, whose State i want to be managed by another Stateful widget in the same screen. It is a textfield , which when it starts editing , I want to enable the button. How can I do this? I made a function to toggle the boolean value of the button, but when i try to access this function from my Stateful widget class it gives the error, 'Instance member can't be accessed using static access'. How can I resolve this issue ?

My main Screen code


import 'package:flutter/material.dart';
import 'package:flutter_convertor/ItemBought.dart';



class task extends StatefulWidget{

  @override
  taskState createState() => new taskState();
}

class taskState extends State<task> {

 int current_step = 0;
 bool isButtonDisabled;

 @override void initState() {

    super.initState();
    isButtonDisabled = false;
  }

 formReady(){


   setState(() {
     isButtonDisabled = !isButtonDisabled ;
   });
 }


  @override
  Widget build(BuildContext context) {


    Column taskScreen = Column(
        children: <Widget>[

            ItemBought(), //STATEFUL widget  which contains another Stateful widget addImage()
        //some other implementation

              FlatButton(
              color: Colors.red,
              textColor: Colors.black,
                shape: new RoundedRectangleBorder(borderRadius: new BorderRadius.circular(20.0)),
              disabledColor: Color(0XFFf9c3c1),
              disabledTextColor: Colors.white,
              padding: EdgeInsets.all(8.0),
              splashColor: Colors.red[400],
              onPressed: isButtonDisabled ? null : _completePage
              ,
              child: Text(
              "Completed",
              style: TextStyle(color: Colors.white, fontSize: 15.0, fontWeight: FontWeight.bold),
              ),
              )
              ,
            ],
          )

        ]);


    return taskScreen;
  }

}

Widget from which i am trying to change the state of the FlatButton:


import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:flutter_convertor/task.dart';


class addImage extends StatefulWidget{
  @override
  _addImageState createState() => _addImageState();
}

class _addImageState extends State<addImage> {


  Column itemDetails = Column(
 //some implementation
  );

  Column OPObservations = Column(
    children: <Widget>[
    //otherlayout widgets
      Padding(
        padding: const EdgeInsets.only(left: 8.0, right: 8.0),
        child: TextField(
            onChanged: taskState.formReady(), //GIVES ERROR 
            maxLines: 6 ,
            style: new TextStyle(
              fontSize: 13.0,

            ),
            controller: TextEditingController(),
            decoration: InputDecoration(
                alignLabelWithHint: true,
                labelText: "Enter the details",
                labelStyle: TextStyle(color: Colors.grey[400], fontSize: 13.0),
                contentPadding: EdgeInsets.only(top:8.0, bottom: 8.0,left: 10.0),
                enabledBorder: OutlineInputBorder(
                    borderSide: BorderSide(color: Colors.grey[400]),
                    borderRadius: BorderRadius.all(Radius.circular(8.0))),
                focusedBorder: UnderlineInputBorder(
                  borderSide: BorderSide(color: Colors.red),
                )
            )
        ),

      ),
    ],
  );

  @override
  Widget build(BuildContext context) {

    return Container(
            child: _image == null ? Column (
                //some implementation
                )
                : Column(
                      children: <Widget>[
                      itemDetails,
                      OPObservations,
                      ]
            )
    );
  }

}

1

1 Answers

0
votes

Didn't read your code (sorry) but according to your question there are two possible ways: 1) you could follow the lift-state-up principle and store the state of the button in the parent widget of both other components. 2) You implement a (public) method changing the state of the button and call it from the other widget. you could do this by accessing the method by asigning the button-widget a GlobalKey and then do something like this buttonKey.currentState.changeStateMethod(newState);.

Hope one of the method help :)