简体   繁体   English

RadioListTile Flutter 在循环中

[英]RadioListTile Flutter in loop

I have a problem with RadioListTile it doesn't work我对 RadioListTile 有疑问,它不起作用

Im trying to create a list of RadioListTile to fill in with my data from firebase using for loop.我试图创建一个 RadioListTile 列表,以使用 for 循环填充来自 firebase 的数据。 When I click it it do receive the action, but I cannot check the box when click on it.当我单击它时,它会收到操作,但是单击它时我无法选中该框。

I have been trying to solve it for days.几天来我一直在努力解决它。 Anyone can help?任何人都可以帮忙吗?

This is my code:这是我的代码:

import 'package:eatwell/src/helpers/changescreen.dart';
import 'package:eatwell/src/model/itemmodel.dart';
import 'package:eatwell/src/model/platemodel.dart';
import 'package:eatwell/src/pages/cartPage.dart';
import 'package:eatwell/src/provider/customplate.dart';
import 'package:eatwell/src/provider/item.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class CustomDetail extends StatefulWidget {
  final CustomModel custom;

  const CustomDetail({Key key, this.custom}) : super(key: key);

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

class _CustomDetail extends State<CustomDetail> {
  @override
  Widget build(BuildContext context) {
    final carbsProvider = Provider.of<CarbsProvider>(context);
    var mycarb = 1;
    return Scaffold(
        appBar: AppBar(
          title: Text(
            "Preset Meal",
            style: TextStyle(
              fontSize: 30.0,
              fontWeight: FontWeight.bold,
              color: Colors.black,
              fontFamily: 'DancingScript',
            ),
          ),
          centerTitle: true,
          actions: <Widget>[
            Padding(
              padding: const EdgeInsets.only(bottom: 8.0, right: 8.0),
              child: Stack(
                children: <Widget>[
                  IconButton(
                      icon: Icon(
                        Icons.shopping_bag_outlined,
                        size: 40,
                      ),
                      onPressed: () {
                        changeScreen(context, Cart());
                      }),
                  Positioned(
                    right: 3,
                    bottom: 0,
                    child: Container(
                      decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(10),
                          boxShadow: [
                            BoxShadow(
                                color: Colors.grey,
                                offset: Offset(2, 3),
                                blurRadius: 3)
                          ]),
                      child: Padding(
                        padding: const EdgeInsets.only(left: 4, right: 4),
                        child: Text(
                          "2",
                          style: TextStyle(
                              color: Colors.red,
                              fontSize: 16.0,
                              fontWeight: FontWeight.bold),
                        ),
                      ),
                    ),
                  )
                ],
              ),
            )
          ],
        ),
        body: SafeArea(
          child: ListView(
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.only(
                    left: 20.0, right: 20, bottom: 10, top: 30),
                child: Container(
                  decoration: BoxDecoration(
                    color: Colors.cyanAccent,
                    borderRadius: BorderRadius.circular(100),
                  ),
                  height: 300,
                  alignment: Alignment.center,
                  child: Image(
                    image: NetworkImage(widget.custom.image),
                    height: 300,
                    width: 300,
                  ),
                ),
              ),
              Text(
                widget.custom.name,
                style: TextStyle(fontSize: 30.0, fontWeight: FontWeight.bold),
                textAlign: TextAlign.center,
              ),
              Text(
                ("RM ${widget.custom.price}"),
                style: TextStyle(fontSize: 30.0, fontWeight: FontWeight.bold),
                textAlign: TextAlign.center,
              ),

              for (var i = 0; i < widget.custom.carbnum; i++)
                Column(
                  children: <Widget>[
                    ListTile(
                      title: Text("Choose Your Carbohydrates"),
                    ),
                    for (var i = 0; i < carbsProvider.carbs.length; i++)
                      RadioListTile(
                        title: Text(carbsProvider.carbs[i].name),
                        value: i,
                        onChanged: (var v) {
                          print("object");
                          mycarb = v;
                        },
                        groupValue: mycarb,
                      )
                  ],
                ),

              // for (var i = 0; i < widget.custom.fooditems.length; i++)
              //   Text(
              //     widget.custom.fooditems[i],
              //     style: TextStyle(fontSize: 30.0, fontWeight: FontWeight.bold),
              //   )
            ],
          ),
        ));
  }
}

Wrap your onChanged in setState.将您的 onChanged 包装在 setState 中。 just like就像

onChanged: (var v) {
  setState((){
    print("object");
    mycarb = v;
  });
},

According to the docs:根据文档:

Calling setState notifies the framework that the internal state of this object has changed in a way that might impact the user interface in this subtree, which causes the framework to schedule a build for this State object. Calling setState notifies the framework that the internal state of this object has changed in a way that might impact the user interface in this subtree, which causes the framework to schedule a build for this State object.

Use setState method in onChanged method of RadioListTile widget and as answered by Abdul Qadir.在 RadioListTile 小部件的 onChanged 方法中使用 setState 方法,并由 Abdul Qadir 回答。

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

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