Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
311 views
in Technique[技术] by (71.8m points)

Flutter How to get value from a custom widget

I have a container that color can change using a color list. I want to get the selected color of the container from the main page. When the container clicked, a list of colors appears and we can select the color for the container. I want to do is to get the color value from that container when I press a button

  Widget build(BuildContext context) {
    return Container(
      height: 80,
      width: 40,
      padding: EdgeInsets.all(5),
      child: InkWell(
        onTap: () {
          showDialog(
            context: context,
            child: Dialog(
                backgroundColor: Colors.white,
                // insetPadding: EdgeInsets.all(100),
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(20)),
                child: Column(
                  children: [
                    Container(
                      color: Colors.red,
                      height: 50,
                      alignment: Alignment(0, 0),
                      child: Text(
                        'Select the Color',
                        style: TextStyle(
                            fontWeight: FontWeight.bold, fontSize: 20),
                      ),
                    ),
                    ListView.builder(
                        shrinkWrap: true,
                        itemCount: colors.length,
                        itemBuilder: (context, index) {
                          return GestureDetector(
                            child: Container(
                              decoration: BoxDecoration(
                                  //border: Border.all(),
                                  ),
                              padding: EdgeInsets.all(5),
                              child: Row(
                                children: <Widget>[
                                  SizedBox(
                                    width: 20,
                                  ),
                                  Container(
                                    decoration: BoxDecoration(
                                        boxShadow: <BoxShadow>[
                                          BoxShadow(blurRadius: 10)
                                        ],
                                        border: Border.all(),
                                        borderRadius:
                                            BorderRadius.circular(100),
                                        color: color[index]),
                                    padding: EdgeInsets.all(5),
                                    //color: color[index],
                                    height: 45,
                                    width: 45,
                                  ),
                                  Padding(
                                    padding: EdgeInsets.all(10),
                                  ),
                                  Text(
                                    colors[index],
                                    style: TextStyle(
                                      fontFamily:
                                          GoogleFonts.inter().fontFamily,
                                      color: color[index],
                                      fontSize: 20.0,
                                      shadows: [
                                        Shadow(
                                            // bottomLeft
                                            offset: Offset(0, 1.5),
                                            blurRadius: 5,
                                            color: Colors.black),
                                      ],
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            onTap: () {
                              Navigator.pop(context);
                              setState(() {
                                selectedColor = color[index];
                                print(index);
                              });
                            },
                          );
                        })
                  ],
                )),
          );
        },
        child: Container(
          padding: EdgeInsets.all(10),
          width: 20,
          height: 60,
          decoration: BoxDecoration(
            color: selectedColor,
            borderRadius: BorderRadius.circular(10),
          ),
        ),
      ),
    );
  }
question from:https://stackoverflow.com/questions/65911337/flutter-how-to-get-value-from-a-custom-widget

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can pass the value to parent when you call Navigator.pop(). https://flutter.dev/docs/cookbook/navigation/returning-data

onTap: () {
                          Navigator.pop(context, color[index]);
                          setState(() {
                            selectedColor = color[index];
                            print(index);
                          });
                        },

In this case, you need to wait for result after call Navigator push method.

final result = await Navigator.push(
...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...