简体   繁体   English

“对话框”内的列表视图中的复选框在颤动中不起作用

[英]checkbox in a listview inside a 'Dialog' not working in flutter

I'm triggering a dialog box on a button click in which I want to print some rows containing data fetched from firestore .我在单击按钮时触发一个对话框,我想在其中打印一些包含从firestore获取的数据的行。 The 'Dialog' box has a Listview widget where I'm looping through my document snapshot to print rows along with checkboxes in front of them. “对话框”框有一个Listview小部件,我在其中循环浏览我的文档快照以打印行以及它们前面的复选框。 Basically I want to perform an action on all the items selected using the checkboxes .基本上我想对使用复选框选择的所有项目执行操作。 But when I tap, the checkboxes are not working.但是当我点击时,复选框不起作用。

Please help!请帮忙! I'm new to flutter.我是扑的新手。 Here's my complete code of the dialog box.这是我的对话框的完整代码。

  final snapshot = await Firestore.instance
        .collection('students')
        .where('vehicle_code', isEqualTo: _userId)
        .getDocuments(); 
 await showDialog<void>(
      context: context,
      builder: (BuildContext context) {
        bool isChecked = false;
      return Dialog(

      shape:
        RoundedRectangleBorder(borderRadius: new BorderRadius.circular(15)),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[

            Container(
              padding: EdgeInsets.all(12),
              alignment: Alignment.center,
              decoration: BoxDecoration(
                  color: Colors.grey[300],
                  borderRadius: new BorderRadius.only(
                  topLeft: const Radius.circular(15),
                  topRight: const Radius.circular(15)),
              ),
              child: Text(
                "Select Students",
                style: TextStyle(
                    color: Colors.black,
                    fontSize: 18,
                    fontWeight: FontWeight.w600),
              ),
            ),
              Container(
                width: 300,
                height: 400,
                child: ListView(
                  //mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[

                    for(var item in snapshot.documents )
                      //Text(item['first_name'])
                      Row(
                        children: [
                          SizedBox(
                            width: 100.0,
                            height: 100.0,
                            child: Image.asset('assets/newlogo.png'),
                          ),
                          Text(item['first_name']),
                          Checkbox(
                            value: isChecked,
                            onChanged: (bool value) {
                              print(isChecked);
                              setState(() {
                                isChecked = value;
                              });
                            },
                          ),

                        ],
                      ),

                  ],
                ),

              ),


      Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.end,
              children: <Widget>[
                RaisedButton(
                  color: Colors.blue,
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                  child: Text(
                    'Okay',
                    style: TextStyle(fontSize: 18.0, color: Colors.white),
                  ),
                ),
                SizedBox(
                  width: 20,
                ),
                RaisedButton(
                  color: Colors.red,
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                  child: Text(
                    'Cancel!',
                    style: TextStyle(fontSize: 18.0, color: Colors.white),
                  ),
                )
              ],
            ),

      ],
    ),
    );
    },
    );

You can move out isChecked and wrap with StatefulBuilder您可以移出isChecked并使用StatefulBuilder包装
You still need to use List<bool> isCheckList to control your checkboxs您仍然需要使用List<bool> isCheckList来控制您的复选框
But it depends on your design但这取决于你的设计

code snippet代码片段

bool isChecked = false;

      void _incrementCounter() async {
        await showDialog<void>(
            context: context,
            builder: (BuildContext context) {
              return StatefulBuilder(
                builder: (BuildContext context, StateSetter setState) {

                  return Dialog(

working demo工作演示

在此处输入图片说明

full test code完整的测试代码

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  List<Map> snapshot = [
    {"first_name": "abc"},
    {"first_name": "def"}
  ];
  bool isChecked = false;

  void _incrementCounter() async {
    await showDialog<void>(
        context: context,
        builder: (BuildContext context) {
          return StatefulBuilder(
            builder: (BuildContext context, StateSetter setState) {

              return Dialog(
                shape: RoundedRectangleBorder(
                    borderRadius: new BorderRadius.circular(15)),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Container(
                      padding: EdgeInsets.all(12),
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        color: Colors.grey[300],
                        borderRadius: new BorderRadius.only(
                            topLeft: const Radius.circular(15),
                            topRight: const Radius.circular(15)),
                      ),
                      child: Text(
                        "Select Students",
                        style: TextStyle(
                            color: Colors.black,
                            fontSize: 18,
                            fontWeight: FontWeight.w600),
                      ),
                    ),
                    Container(
                      width: 300,
                      height: 400,
                      child: ListView(
                        //mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: <Widget>[

                          for (var item in snapshot)
                            //Text(item['first_name'])
                            Row(
                              children: [
                                /*SizedBox(
                                  width: 100.0,
                                  height: 100.0,
                                  child: Image.asset('assets/newlogo.png'),
                                ),*/
                                Text(item['first_name']),
                                Checkbox(
                                  value: isChecked,
                                  onChanged: (bool value) {
                                    print(isChecked);
                                    setState(() {
                                      isChecked = value;
                                    });
                                  },
                                ),
                              ],
                            ),
                        ],
                      ),
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: <Widget>[
                        RaisedButton(
                          color: Colors.blue,
                          onPressed: () {
                            Navigator.of(context).pop();
                          },
                          child: Text(
                            'Okay',
                            style:
                                TextStyle(fontSize: 18.0, color: Colors.white),
                          ),
                        ),
                        SizedBox(
                          width: 20,
                        ),
                        RaisedButton(
                          color: Colors.red,
                          onPressed: () {
                            Navigator.of(context).pop();
                          },
                          child: Text(
                            'Cancel!',
                            style:
                                TextStyle(fontSize: 18.0, color: Colors.white),
                          ),
                        )
                      ],
                    ),
                  ],
                ),
              );
            },
          );
        });
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

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

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