RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1011755
Accepted
CyberDemon
CyberDemon
Asked:2020-08-09 18:30:53 +0000 UTC2020-08-09 18:30:53 +0000 UTC 2020-08-09 18:30:53 +0000 UTC

单选按钮在颤振的底部工作表中不起作用

  • 772

我有一个模态底部表,它有两个单选按钮。但是当按下时,它们不起作用。我不知道问题是什么,我认为这是因为单选按钮位于底部表中。

这是我的代码

import 'package:flutter/material.dart';

class NutritionScreen extends StatefulWidget {
  @override
  _NutritionScreenState createState() => _NutritionScreenState();
}

class _NutritionScreenState extends State<NutritionScreen> {
  double height = 500.0;
  TextEditingController textFieldControllerAge = TextEditingController();
  TextEditingController textFieldControllerGrowth = TextEditingController();
  TextEditingController textFieldControllerWeight = TextEditingController();
  TextEditingController textFieldControllerWeightGoal = TextEditingController();
  bool _validateAge = false;
  bool _validateGrowth = false;
  bool _validateWeight = false;
  bool _validateWeightGoal = false;
  // Declare this variable
  int selectedRadioTile;

  @override
  void initState() {
    super.initState();
    selectedRadioTile = 0;
  }

  setSelectedRadioTile(int val) {
    setState(() {
      selectedRadioTile = val;
    });
  }

  @override
  void dispose() {
    textFieldControllerAge.dispose();
    textFieldControllerGrowth.dispose();
    textFieldControllerWeight.dispose();
    textFieldControllerWeightGoal.dispose();
    super.dispose();
  }

  void _modalBottomSheetMenu() {
    showModalBottomSheet(
        context: context,
        builder: (builder) {
          return Padding(
            padding: EdgeInsets.only(
                bottom: MediaQuery
                    .of(context)
                    .viewInsets
                    .bottom),
            child: new Container(
                height: height,
                color: Colors
                    .transparent, //could change this to Color(0xFF737373),
                //so you don't have to change MaterialApp canvasColor
                child: new Container(
                  decoration: new BoxDecoration(
                      color: Colors.white,
                      borderRadius: new BorderRadius.only(
                          topLeft: const Radius.circular(10.0),
                          topRight: const Radius.circular(10.0))),
                  child: SingleChildScrollView(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisSize: MainAxisSize.max,
                        mainAxisAlignment: MainAxisAlignment.end,
                        children: <Widget>[
                          Padding(
                              padding: EdgeInsets.all(8.0),
                              child: Text(
                                "Питание",
                                textAlign: TextAlign.center,
                                style:
                                TextStyle(color: Colors.black, fontSize: 26.0),
                              )),
                          Padding(padding: EdgeInsets.all(8.0),
                              child: TextField(
                                maxLines: 1,
                                textDirection: TextDirection.ltr,
                                controller: textFieldControllerAge,
                                style: TextStyle(
                                    color: Colors.lightGreen[400],
                                    fontSize: 18.5),
                                decoration: InputDecoration(
                                  contentPadding: EdgeInsets.only(bottom: 4.0),
                                  labelText: "Возраст",
                                  errorText: _validateAge ? 'Поле должно быть заполненным' : null,
                                  alignLabelWithHint: false,
                                ),
                                keyboardType: TextInputType.phone,
                                textInputAction: TextInputAction.done,
                              )),
                          Padding(padding: EdgeInsets.all(8.0),
                              child: TextField(
                                maxLines: 1,
                                textDirection: TextDirection.ltr,
                                controller: textFieldControllerGrowth,
                                style: TextStyle(
                                    color: Colors.lightGreen[400],
                                    fontSize: 18.5),
                                decoration: InputDecoration(
                                  contentPadding: EdgeInsets.only(bottom: 4.0),
                                  labelText: "Рост",
                                  errorText: _validateGrowth ? 'Поле должно быть заполненным' : null,
                                  alignLabelWithHint: false,
                                ),
                                keyboardType: TextInputType.phone,
                                textInputAction: TextInputAction.done,
                              )),
                          Padding(padding: EdgeInsets.all(8.0),
                              child: TextField(
                                maxLines: 1,
                                textDirection: TextDirection.ltr,
                                controller: textFieldControllerWeight,
                                style: TextStyle(
                                    color: Colors.lightGreen[400],
                                    fontSize: 18.5),
                                decoration: InputDecoration(
                                  contentPadding: EdgeInsets.only(bottom: 4.0),
                                  labelText: "Вес",
                                  errorText: _validateWeight ? 'Поле должно быть заполненным' : null,
                                  alignLabelWithHint: false,
                                ),
                                keyboardType: TextInputType.phone,
                                textInputAction: TextInputAction.done,
                              )),
                          Padding(padding: EdgeInsets.all(8.0),
                              child: TextField(
                                maxLines: 1,
                                textDirection: TextDirection.ltr,
                                controller: textFieldControllerWeightGoal,
                                style: TextStyle(
                                    color: Colors.lightGreen[400],
                                    fontSize: 18.5),
                                decoration: InputDecoration(
                                  contentPadding: EdgeInsets.only(bottom: 4.0),
                                  labelText: "Целевой вес",
                                  errorText: _validateWeightGoal ? 'Поле должно быть заполненным' : null,
                                  alignLabelWithHint: false,
                                ),
                                keyboardType: TextInputType.phone,
                                textInputAction: TextInputAction.done,
                              )),

                          RadioListTile(
                            value: 0,
                            groupValue: selectedRadioTile,
                            title: Text("Мужчина"),
                            onChanged: (val) {
                             setSelectedRadioTile(val);
                            },
                            activeColor: Colors.lightGreen[400],
                            selected: true,
                          ),
                          RadioListTile(
                            value: 1,
                            groupValue: selectedRadioTile,
                            title: Text("Женщина"),
                            onChanged: (val) {
                              setSelectedRadioTile(val);
                            },
                            activeColor: Colors.lightGreen[400],
                            selected: false,
                          ),

                         Container(
                              alignment: Alignment.bottomRight,
                              margin: EdgeInsets.only(top: 30),
                              child: Padding(
                                  padding: EdgeInsets.all(16.0),
                                  child: FloatingActionButton(
                                      heroTag: "tag4",
                                      backgroundColor: Color(0xffFF7070),
                                      child: Icon(
                                          Icons.check, color: Colors.white),
                                      onPressed: () {
                                        setState(() {
                                          textFieldControllerAge.text.isEmpty ?
                                          _validateAge = true : _validateAge = false;

                                          textFieldControllerGrowth.text.isEmpty ?
                                          _validateGrowth = true : _validateGrowth = false;

                                          textFieldControllerWeight.text.isEmpty ?
                                          _validateWeight = true : _validateWeight = false;

                                          textFieldControllerWeightGoal.text.isEmpty ?
                                          _validateWeightGoal = true : _validateWeightGoal = false;
                                        });
                                        if(textFieldControllerAge.text.isNotEmpty &&
                                            textFieldControllerGrowth.text.isNotEmpty &&
                                            textFieldControllerWeight.text.isNotEmpty &&
                                            textFieldControllerWeightGoal.text.isNotEmpty)
                                          _sendDataToSecondScreen(context);

                                      }))),

                        ],
                      )),
                )),
          );
        });
  }

  void _sendDataToSecondScreen(BuildContext context) {
    String textAge = textFieldControllerAge.text;
    String textGrowth = textFieldControllerGrowth.text;
    String textWeight = textFieldControllerWeight.text;
    String textWeightGoal = textFieldControllerWeightGoal.text;
    Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) =>
              MainNutrition(
                  textAge: textAge,
                  textGrowth: textGrowth,
                  textWeight: textWeight,
                  textWeightGoal: textWeightGoal),
                // Этого класса нет
        ));
  }



  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      resizeToAvoidBottomInset: false,
      backgroundColor: Color(0xff2b2b2b),
      appBar: AppBar(
        backgroundColor: Colors.lightGreen[400],
        title: Text(
          'Питание',
          style: new TextStyle(color: Colors.white),
        ),
        leading: IconButton(
          icon: Icon(Icons.arrow_back),
          color: Colors.white,
          onPressed: () => Navigator.of(context).pop(),
        ),
      ),
      body: Container(
        alignment: Alignment.center,
        margin: const EdgeInsets.only(bottom: 45.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisSize: MainAxisSize.max,
          mainAxisAlignment: MainAxisAlignment.end,
          children: <Widget>[
            Padding(
                padding: const EdgeInsets.only(bottom: 200.0),
                child: Text(
                  "Нажми на кнопку, чтобы добавить правильный рацион питания.",
                  textAlign: TextAlign.center,
                  style: TextStyle(color: Colors.white, fontSize: 20.0),
                )),
            FloatingActionButton(
                heroTag: "tag3",
                backgroundColor: Color(0xffFF7070),
                child: Icon(Icons.add, color: Colors.white),
                onPressed: () {
                  _modalBottomSheetMenu();
                }),
          ],
        ),
      ),
    );
  }
}
flutter
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Best Answer
    Spatz
    2020-08-09T22:55:17Z2020-08-09T22:55:17Z

    为了更新模态底部工作表中的单选按钮,您需要使用StatefulBuilder

    showModalBottomSheet(
        context: context,
        builder: (context) => StatefulBuilder(builder: (context, state) {
    

    并在按钮处理程序中使用新方法state(本质上是相同的 setState,只是重命名)

                            onChanged: (val) {
                              state(() {
                                selectedRadioTile = val;
                              });
                            },
    

    所有代码都在这里:

    class NutritionScreen extends StatefulWidget {
      @override
      _NutritionScreenState createState() => _NutritionScreenState();
    }
    
    class _NutritionScreenState extends State<NutritionScreen> {
      double height = 500.0;
      TextEditingController textFieldControllerAge = TextEditingController();
      TextEditingController textFieldControllerGrowth = TextEditingController();
      TextEditingController textFieldControllerWeight = TextEditingController();
      TextEditingController textFieldControllerWeightGoal = TextEditingController();
      bool _validateAge = false;
      bool _validateGrowth = false;
      bool _validateWeight = false;
      bool _validateWeightGoal = false;
      // Declare this variable
      int selectedRadioTile;
    
      @override
      void initState() {
        super.initState();
        selectedRadioTile = 0;
      }
    
      setSelectedRadioTile(int val) {
        setState(() {
          selectedRadioTile = val;
        });
      }
    
      @override
      void dispose() {
        textFieldControllerAge.dispose();
        textFieldControllerGrowth.dispose();
        textFieldControllerWeight.dispose();
        textFieldControllerWeightGoal.dispose();
        super.dispose();
      }
    
      void _modalBottomSheetMenu() {
        showModalBottomSheet(
            context: context,
            builder: (context) => StatefulBuilder(builder: (context, state) {
                  return Padding(
                    padding: EdgeInsets.only(
                        bottom: MediaQuery.of(context).viewInsets.bottom),
                    child: new Container(
                        height: height,
                        color: Colors
                            .transparent, //could change this to Color(0xFF737373),
                        //so you don't have to change MaterialApp canvasColor
                        child: new Container(
                          decoration: new BoxDecoration(
                              color: Colors.white,
                              borderRadius: new BorderRadius.only(
                                  topLeft: const Radius.circular(10.0),
                                  topRight: const Radius.circular(10.0))),
                          child: SingleChildScrollView(
                              child: Column(
                            crossAxisAlignment: CrossAxisAlignment.center,
                            mainAxisSize: MainAxisSize.max,
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: <Widget>[
                              Padding(
                                  padding: EdgeInsets.all(8.0),
                                  child: Text(
                                    "Питание",
                                    textAlign: TextAlign.center,
                                    style: TextStyle(
                                        color: Colors.black, fontSize: 26.0),
                                  )),
                              Padding(
                                  padding: EdgeInsets.all(8.0),
                                  child: TextField(
                                    maxLines: 1,
                                    textDirection: TextDirection.ltr,
                                    controller: textFieldControllerAge,
                                    style: TextStyle(
                                        color: Colors.lightGreen[400],
                                        fontSize: 18.5),
                                    decoration: InputDecoration(
                                      contentPadding: EdgeInsets.only(bottom: 4.0),
                                      labelText: "Возраст",
                                      errorText: _validateAge
                                          ? 'Поле должно быть заполненным'
                                          : null,
                                      alignLabelWithHint: false,
                                    ),
                                    keyboardType: TextInputType.phone,
                                    textInputAction: TextInputAction.done,
                                  )),
                              Padding(
                                  padding: EdgeInsets.all(8.0),
                                  child: TextField(
                                    maxLines: 1,
                                    textDirection: TextDirection.ltr,
                                    controller: textFieldControllerGrowth,
                                    style: TextStyle(
                                        color: Colors.lightGreen[400],
                                        fontSize: 18.5),
                                    decoration: InputDecoration(
                                      contentPadding: EdgeInsets.only(bottom: 4.0),
                                      labelText: "Рост",
                                      errorText: _validateGrowth
                                          ? 'Поле должно быть заполненным'
                                          : null,
                                      alignLabelWithHint: false,
                                    ),
                                    keyboardType: TextInputType.phone,
                                    textInputAction: TextInputAction.done,
                                  )),
                              Padding(
                                  padding: EdgeInsets.all(8.0),
                                  child: TextField(
                                    maxLines: 1,
                                    textDirection: TextDirection.ltr,
                                    controller: textFieldControllerWeight,
                                    style: TextStyle(
                                        color: Colors.lightGreen[400],
                                        fontSize: 18.5),
                                    decoration: InputDecoration(
                                      contentPadding: EdgeInsets.only(bottom: 4.0),
                                      labelText: "Вес",
                                      errorText: _validateWeight
                                          ? 'Поле должно быть заполненным'
                                          : null,
                                      alignLabelWithHint: false,
                                    ),
                                    keyboardType: TextInputType.phone,
                                    textInputAction: TextInputAction.done,
                                  )),
                              Padding(
                                  padding: EdgeInsets.all(8.0),
                                  child: TextField(
                                    maxLines: 1,
                                    textDirection: TextDirection.ltr,
                                    controller: textFieldControllerWeightGoal,
                                    style: TextStyle(
                                        color: Colors.lightGreen[400],
                                        fontSize: 18.5),
                                    decoration: InputDecoration(
                                      contentPadding: EdgeInsets.only(bottom: 4.0),
                                      labelText: "Целевой вес",
                                      errorText: _validateWeightGoal
                                          ? 'Поле должно быть заполненным'
                                          : null,
                                      alignLabelWithHint: false,
                                    ),
                                    keyboardType: TextInputType.phone,
                                    textInputAction: TextInputAction.done,
                                  )),
                              RadioListTile(
                                value: 0,
                                groupValue: selectedRadioTile,
                                title: Text("Мужчина"),
                                onChanged: (val) {
                                  state(() {
                                    selectedRadioTile = val;
                                  });
                                },
                                activeColor: Colors.lightGreen[400],
                                selected: true,
                              ),
                              RadioListTile(
                                value: 1,
                                groupValue: selectedRadioTile,
                                title: Text("Женщина"),
                                onChanged: (val) {
                                  state(() {
                                    selectedRadioTile = val;
                                  });
                                },
                                activeColor: Colors.lightGreen[400],
                                selected: false,
                              ),
                              Container(
                                  alignment: Alignment.bottomRight,
                                  margin: EdgeInsets.only(top: 30),
                                  child: Padding(
                                      padding: EdgeInsets.all(16.0),
                                      child: FloatingActionButton(
                                          heroTag: "tag4",
                                          backgroundColor: Color(0xffFF7070),
                                          child: Icon(Icons.check,
                                              color: Colors.white),
                                          onPressed: () {
                                            setState(() {
                                              textFieldControllerAge.text.isEmpty
                                                  ? _validateAge = true
                                                  : _validateAge = false;
    
                                              textFieldControllerGrowth.text.isEmpty
                                                  ? _validateGrowth = true
                                                  : _validateGrowth = false;
    
                                              textFieldControllerWeight.text.isEmpty
                                                  ? _validateWeight = true
                                                  : _validateWeight = false;
    
                                              textFieldControllerWeightGoal
                                                      .text.isEmpty
                                                  ? _validateWeightGoal = true
                                                  : _validateWeightGoal = false;
                                            });
                                            if (textFieldControllerAge
                                                    .text.isNotEmpty &&
                                                textFieldControllerGrowth
                                                    .text.isNotEmpty &&
                                                textFieldControllerWeight
                                                    .text.isNotEmpty &&
                                                textFieldControllerWeightGoal
                                                    .text.isNotEmpty)
                                              _sendDataToSecondScreen(context);
                                          }))),
                            ],
                          )),
                        )),
                  );
                }));
      }
    
      void _sendDataToSecondScreen(BuildContext context) {
        String textAge = textFieldControllerAge.text;
        String textGrowth = textFieldControllerGrowth.text;
        String textWeight = textFieldControllerWeight.text;
        String textWeightGoal = textFieldControllerWeightGoal.text;
        Navigator.push(
            context,
            MaterialPageRoute(builder: (context) => Container()
                // MainNutrition(
                //     textAge: textAge,
                //     textGrowth: textGrowth,
                //     textWeight: textWeight,
                //     textWeightGoal: textWeightGoal),
                // Этого класса нет
                ));
      }
    
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          resizeToAvoidBottomInset: false,
          backgroundColor: Color(0xff2b2b2b),
          appBar: AppBar(
            backgroundColor: Colors.lightGreen[400],
            title: Text(
              'Питание',
              style: new TextStyle(color: Colors.white),
            ),
            leading: IconButton(
              icon: Icon(Icons.arrow_back),
              color: Colors.white,
              onPressed: () => Navigator.of(context).pop(),
            ),
          ),
          body: Container(
            alignment: Alignment.center,
            margin: const EdgeInsets.only(bottom: 45.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisSize: MainAxisSize.max,
              mainAxisAlignment: MainAxisAlignment.end,
              children: <Widget>[
                Padding(
                    padding: const EdgeInsets.only(bottom: 200.0),
                    child: Text(
                      "Нажми на кнопку, чтобы добавить правильный рацион питания.",
                      textAlign: TextAlign.center,
                      style: TextStyle(color: Colors.white, fontSize: 20.0),
                    )),
                FloatingActionButton(
                    heroTag: "tag3",
                    backgroundColor: Color(0xffFF7070),
                    child: Icon(Icons.add, color: Colors.white),
                    onPressed: () {
                      _modalBottomSheetMenu();
                    }),
              ],
            ),
          ),
        );
      }
    }
    

    资源

    • 1
  2. MiT
    2020-08-09T21:38:00Z2020-08-09T21:38:00Z

    完整的工作示例。由于您没有提供部分代码(而且我不是 Vanga),我可以假设问题出在其他地方。

    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: MyHomePage(),
        );
      }
    }
    
    int selectedRadioTile = 0;
    
    @override
    void initState() {
      initState();
      selectedRadioTile = 0;
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key}) : super(key: key);
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text("Title"),
          ),
          body: Center(
            child: new Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                new RadioListTile(
                  value: 1,
                  groupValue: selectedRadioTile,
                  title: Text("Radio 1"),
                  onChanged: (val) {
                    print("Radio Tile pressed $val");
                    setState(() => selectedRadioTile = val);
                  },
                  activeColor: Colors.lightGreen[400],
                  selected: true,
                ),
                new RadioListTile(
                  value: 2,
                  groupValue: selectedRadioTile,
                  title: Text("Radio 2"),
                  onChanged: (val) {
                    print("Radio Tile pressed $val");
                    setState(() => selectedRadioTile = val);
                  },
                  activeColor: Colors.lightGreen[400],
                  selected: false,
                ),
              ],
            ),
          ),
        );
      }
    }
    

    UPD

    问题是它showModalBottomSheet没有更新它的状态,为了解决这个问题,我建议看这里。实际上,您radio button正在更新它们的值,但由于没有小部件更新,showModalBottomSheet因此不会显示。这可以在视觉上和在 的帮助下看到print("Radio Tile pressed $val");。我还建议将重复的代码移到单独的方法中,这将减少您将来的时间。

    • 0

相关问题

  • 如何将数据从 alertdialog 传递到同一个 Flutter 页面

  • 如何将返回值传递给 Text 小部件?

  • Flutter 中的 BuildContext 是什么?

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    根据浏览器窗口的大小调整背景图案的大小

    • 2 个回答
  • Marko Smith

    理解for循环的执行逻辑

    • 1 个回答
  • Marko Smith

    复制动态数组时出错(C++)

    • 1 个回答
  • Marko Smith

    Or and If,elif,else 构造[重复]

    • 1 个回答
  • Marko Smith

    如何构建支持 x64 的 APK

    • 1 个回答
  • Marko Smith

    如何使按钮的输入宽度?

    • 2 个回答
  • Marko Smith

    如何显示对象变量的名称?

    • 3 个回答
  • Marko Smith

    如何循环一个函数?

    • 1 个回答
  • Marko Smith

    LOWORD 宏有什么作用?

    • 2 个回答
  • Marko Smith

    从字符串的开头删除直到并包括一个字符

    • 2 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5