如何将Onpressed函数添加到列表的每个值

时间:2019-04-23 18:09:15

标签: flutter

我在导航列表中的项目到新的.dart文件时遇到困难,这是我导入第二个文件的主页。

import 'package:flutter/material.dart';

class Binary extends StatefulWidget {
  @override
  _BinaryState createState() => _BinaryState();
}

class _BinaryState extends State<Binary> {
  @override
  Widget build(BuildContext context) {
    double cWidth = MediaQuery.of(context).size.width*0.8;
    return new Container (
      padding: const EdgeInsets.all(16.0),
      width: cWidth,
      child: new Column (
        children: <Widget>[
          new Text ("Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 ", textAlign: TextAlign.left),
          new Text ("Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2", textAlign: TextAlign.left),
        ],
      ),
    );
  }
}

我尝试了这种方法,但是我不知道如何在列表中的iten上添加onpress函数,如果我单击列表中的任何值都应该将我带到相应的页面,例如binary.dart

这是我的二进制代码

#include <iostream>
#include <fstream>
#include <vector>

struct transition {
    // rename the variables into something meaningful
    int int1;
    int int2;
    char a_char;

    friend std::istream& operator>>(std::istream&, transition&);
    friend std::ostream& operator<<(std::ostream&, const transition&);
};
// input stream function for reading one transition
std::istream& operator>>(std::istream& is, transition& t) {
    is >> t.int1 >> t.int2 >> t.a_char;
    return is;
}
// output stream function for writing one transition
std::ostream& operator<<(std::ostream& os, const transition& t) {
    os << t.int1 << " " << t.int2 << " " << t.a_char;
    return os;
}
//-----------------------------------------------------------------------------
struct entity {
    int numberOfStates;
    int numberOfSymbols;
    std::vector<int> finalStates;
    std::vector<transition> transitions;

    friend std::istream& operator>>(std::istream&, entity&);
    friend std::ostream& operator<<(std::ostream&, const entity&);
};
// read one entity from a stream
std::istream& operator>>(std::istream& is, entity& e) {
    int numberOfFinalStates, numberOfTransitions;
    int value;

    if(is >> e.numberOfStates >> e.numberOfSymbols >> numberOfFinalStates) {
        // read to value and put it in its vector
        while(numberOfFinalStates-- && is >> value) e.finalStates.push_back(value);

        if(is >> numberOfTransitions) {
            transition ttmp;
            // read to the temporary transition and put it in its vector
            while(numberOfTransitions-- && is >> ttmp) e.transitions.push_back(ttmp);
            // check that we got the number of values we wanted
            // and set the failbit if we didn't (should check size() of the vectors
            // instead)
            if(numberOfFinalStates != -1 || numberOfTransitions != -1)
                is.setstate(std::ios_base::failbit);
        }
    }
    return is;
}
// write one entity to a stream
std::ostream& operator<<(std::ostream& os, const entity& e) {
    os << e.numberOfStates << "\n" << e.numberOfSymbols << "\n" << e.finalStates.size() << "\n";
    for(const int fs : e.finalStates) os << fs << "\n";
    os << e.transitions.size() << "\n";
    for(const transition& t : e.transitions) os << t << "\n";
    return os;
}
//-----------------------------------------------------------------------------
int main() {
    std::ifstream fs("inputfile.txt");
    if(fs) {
        entity e;
        // stream the opened file into the entity 
        if(fs >> e) {
            std::cout << "loaded these values:\n";
            std::cout << e;
        } else {
            std::cerr << "failed loading file\n";
        }
    } else {
        std::cerr << "failed opening file\n";
    }
}

2 个答案:

答案 0 :(得分:1)

您可以使用InkWellGestureDetector包装列表项。

示例:在您的listDataItem类的build方法中,您将返回一个Card

用InkWell包裹它。

InkWell(
  onTap: () {
    print('inkwell');
  },
  child: Card(...

答案 1 :(得分:-1)

我确实在这里说明了代码,希望对您有所帮助

class listDataItem extends StatelessWidget{

      final String itemName;
      listDataItem(this.itemName);

      @override
      Widget build(BuildContext context){
        return InkWell(
        onTap: () {
        },
        child : Card(

          elevation: 7.0,

          child: new Container(

            margin: EdgeInsets.all(5.0),
            padding: EdgeInsets.all(6.0),

            child: new Row(
              children: <Widget> [

                new CircleAvatar(
                  child: new Text(itemName[0]),

                  backgroundColor: Colors.deepPurple,
                  foregroundColor: Colors.white,

                ),
                new Padding(padding: EdgeInsets.all(8.0)),
                new Text(itemName,style: TextStyle(fontSize: 18.0)),

              ],
            ),
        ),),
        ); 
    }

}