简体   繁体   English

如何在 flutter 中将 id 从有状态小部件获取到无状态小部件?

[英]how to get the id from statefull widget to stateless widget in flutter?

I need to get the id from quizCatId in the category() class to QuizTile() can someone tell me how to solve the issue...我需要从 category() class 到 QuizTile() 中的 quizCatId 获取 id 有人可以告诉我如何解决这个问题...

my need is to call subcategory data from firestore which has an quizid and quizcatid and there I have my quizdata so I need to pass both the id to the quizdata to get data from the firestore我需要从具有 quizid 和 quizcatid 的 firestore 调用子类别数据,并且我有我的 quizdata,所以我需要将 id 都传递给 quizdata 以从 firestore 获取数据

class Category extends StatefulWidget {
  String quizCatId;
  Category(this.quizCatId);

  @override
  _CategoryState createState() => _CategoryState();
}

class _CategoryState extends State<Category> {
  Stream quizStream;
  DatabaseService databaseService = new DatabaseService();

  Widget quizList() {
    return Container(
      child: Column(
        children: [
          StreamBuilder(
            stream: quizStream,
            builder: (context, snapshot) {
              return snapshot.data == null
                  ? Container()
                  : ListView.builder(
                      shrinkWrap: true,
                      physics: ClampingScrollPhysics(),
                      itemCount: snapshot.data.documents.length,
                      itemBuilder: (context, index) {
                        return QuizTile(
                          noOfQuestions: snapshot.data.documents.length,
                          imageUrl:
                              snapshot.data.documents[index].data['quizImgUrl'],
                          title:
                              snapshot.data.documents[index].data['quizTitle'],
                          desc: snapshot.data.documents[index].data['quizDesc'],
                          quizid: snapshot.data.documents[index].data["quizId"],
                         
                        );
                      });
            },
          ),
        ],
      ),
    );
  }

  @override
  void initState() {
    databaseService.getCatData(widget.quizCatId).then((value) {
      quizStream = value;
      setState(() {});
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        centerTitle: true,
        title: appBar(context),
        brightness: Brightness.light,
        elevation: 0.0,
        backgroundColor: Colors.transparent,
        //brightness: Brightness.li,
      ),
      body: SingleChildScrollView(child: quizList()),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () {
          Navigator.push(context,
              MaterialPageRoute(builder: (context) => CreateCategoryQuiz()));
        },
      ),
    );
  }
}

class QuizTile extends StatelessWidget {
  final String imageUrl, title, quizid, desc, quizcatId;
  final int noOfQuestions;

  QuizTile({
    @required this.title,
    @required this.imageUrl,
    @required this.desc,
    @required this.quizid,
    @required this.noOfQuestions,
    @required this.quizcatId,
  });

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) => PlayQuiz(quizid, quizcatId)));
      },
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 14),
        height: 150,
        margin: EdgeInsets.only(bottom: 8),
        child: ClipRRect(
          borderRadius: BorderRadius.circular(8),
          child: Stack(
            children: [
              Image.network(
                imageUrl,
                fit: BoxFit.cover,
                width: MediaQuery.of(context).size.width,
              ),
              Container(
                color: Colors.black26,
                child: Center(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text(
                        title,
                        style: TextStyle(
                            fontSize: 18,
                            color: Colors.white,
                            fontWeight: FontWeight.w500),
                      ),
                      SizedBox(
                        height: 4,
                      ),
                      Text(
                        desc,
                        style: TextStyle(
                            fontSize: 13,
                            color: Colors.white,
                            fontWeight: FontWeight.w500),
                      )
                    ],
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}

Hey you have acces to statefulWidget properties form state class by windget property.嘿,您可以通过 windget 属性访问来自 state class 的 statefulWidget 属性。

All you have to do is ad new parameter in QuizTile constructor and call them您所要做的就是在 QuizTile 构造函数中添加新参数并调用它们

QuizTile(
                          noOfQuestions: snapshot.data.documents.length,
                          imageUrl:
                              snapshot.data.documents[index].data['quizImgUrl'],
                          title:
                              snapshot.data.documents[index].data['quizTitle'],
                          desc: snapshot.data.documents[index].data['quizDesc'],
                          quizid: snapshot.data.documents[index].data["quizId"],
                         quizCatId: widget.quizCatId
                        );  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM