Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何访问小部件或刚刚单击的按钮的文本?_Flutter_Dart_Widget - Fatal编程技术网

Flutter 如何访问小部件或刚刚单击的按钮的文本?

Flutter 如何访问小部件或刚刚单击的按钮的文本?,flutter,dart,widget,Flutter,Dart,Widget,我想将单击的按钮添加到列表中,但我不知道如何访问单击的按钮本身 这是我的。 通过以下方式创建: 用于(var备用支架)支架创建(支架) 这是我要填写的列表: List SelectedStandList=[] 这就是我创建每个“按钮”的方式: import 'package:flutter/material.dart'; class StandCreation extends StatelessWidget { final String stand; StandCreation(this

我想将单击的按钮添加到列表中,但我不知道如何访问单击的按钮本身

这是我的。 通过以下方式创建:

用于(var备用支架)支架创建(支架)

这是我要填写的列表:

List SelectedStandList=[]

这就是我创建每个“按钮”的方式:

import 'package:flutter/material.dart';
class StandCreation extends StatelessWidget {
  final String stand;
  StandCreation(this.stand);

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {}, // Here i would like to do something like SelectedStandList.add(tappedInkWell)
      child: Container(
        width: double.infinity,
        margin: const EdgeInsets.only(
          top: 5,
          bottom: 5,
        ),
        padding: const EdgeInsets.all(3.0),
        color: Colors.blue,
        child: Text(
          stand,
          textAlign: TextAlign.center,
          style: TextStyle(
            color: Colors.white,
            fontSize: 23,
            fontWeight: FontWeight.bold,
          ),
        ),
      ),
    );
  }
}
然后,我想用这个列表创建一个只包含所选项目的新屏幕。
谢谢你的帮助

对循环使用
ListView.builder
而不是
。将您的
StandCreation
更改为:

final VoidCallback onPressed;
StandCreation({this.stand, this.onPressed});
onTap: onPressed
并将
onTap
更改为:

final VoidCallback onPressed;
StandCreation({this.stand, this.onPressed});
onTap: onPressed
最后将For循环更改为:

ListView.builder(
  itemCount: 6, // how many buttons you want
  itemBuilder: (context, index) {
    return StandCreation(stand: stand,
       onPressed:(){
            // Here is your onclick, do whatever you want with it  
        }
    )
   },
)
如果您想让每个
onPressed
函数都有不同的操作,那么您可以简单地在其中放入
If
语句:

   onPressed:(){
        if(index==0) //do something with first button
    }

使用
ListView.builder
而不是
进行循环。将您的
StandCreation
更改为:

final VoidCallback onPressed;
StandCreation({this.stand, this.onPressed});
onTap: onPressed
并将
onTap
更改为:

final VoidCallback onPressed;
StandCreation({this.stand, this.onPressed});
onTap: onPressed
最后将For循环更改为:

ListView.builder(
  itemCount: 6, // how many buttons you want
  itemBuilder: (context, index) {
    return StandCreation(stand: stand,
       onPressed:(){
            // Here is your onclick, do whatever you want with it  
        }
    )
   },
)
如果您想让每个
onPressed
函数都有不同的操作,那么您可以简单地在其中放入
If
语句:

   onPressed:(){
        if(index==0) //do something with first button
    }