【问题标题】:toggle button value display in flutter在颤动中切换按钮值显示
【发布时间】:2021-01-07 11:26:22
【问题描述】:

大家好,

我想在单击它们时显示和存储切换按钮中使用的值。 这里我有点不知所措了。

下面是我制作切换按钮的代码 -:

import 'package:flutter/material.dart';
import 'package:hitch_fun1/Components/constants.dart';
import 'package:hitch_fun1/Components/Reusable_card.dart';
import 'package:flutter/cupertino.dart';

class sample extends StatefulWidget {
  @override
  _sampleState createState() => _sampleState();
}

class _sampleState extends State<sample> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('BottomModalSheet'),
      ),
      body: InkWell(
          child: TextField(
            decoration: InputDecoration(
              enabled: false,
              suffixIcon: Padding(
                padding: EdgeInsets.only(top: 15),
                child: Icon(Icons.add),
              ),
              prefixIcon: Padding(
                padding: EdgeInsets.only(top: 15),
                child: Icon(Icons.favorite_border_rounded),
              ),
            ),
          ),
          onTap: () {
            _showBottomSheet(context);
          }),
    );
  }

  List<bool> isSelected;

  @override
  void initState() {
    isSelected = [false, false, false];
    super.initState();
    focusToggle = [focusNodeButton1, focusNodeButton2, focusNodeButton3];
  }

  FocusNode focusNodeButton1 = FocusNode();
  FocusNode focusNodeButton2 = FocusNode();
  FocusNode focusNodeButton3 = FocusNode();
  List<FocusNode> focusToggle;

  @override
  void dispose() {
    // Clean up the focus node when the Form is disposed.
    focusNodeButton1.dispose();
    focusNodeButton2.dispose();
    focusNodeButton3.dispose();
    super.dispose();
  }

  _showBottomSheet(context) {
    showModalBottomSheet(
        context: context,
        builder: (BuildContext c) {
          return StatefulBuilder(
            builder: (BuildContext context,
                void Function(void Function()) setState) {
              return Container(
                  height: 250,
                  child: Row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Container(
                          child: ToggleButtons(
                            // borderColor: Colors.black,
                            fillColor: Colors.white,
                            borderWidth: 4,
                            selectedBorderColor: Colors.pink,
                            selectedColor: Colors.pinkAccent,
                            splashColor: Colors.deepOrange[400],
                            focusNodes: focusToggle,
                            borderRadius: BorderRadius.only(
                                topLeft: Radius.circular(30),
                                bottomRight: Radius.circular(30)),
                            children: <Widget>[
                              Padding(
                                padding: const EdgeInsets.all(15.0),
                                child: Text(
                                  'you',
                                  style: TextStyle(fontSize: 16),
                                ),
                              ),
                              Padding(
                                padding: const EdgeInsets.all(15.0),
                                child: Text(
                                  'me',
                                  style: TextStyle(fontSize: 16),
                                ),
                              ),
                              Padding(
                                padding: const EdgeInsets.all(15.0),
                                child: Text(
                                  'them',
                                  style: TextStyle(fontSize: 16),
                                ),
                              ),
                            ],
                            onPressed: (int index) {
                              setState(() {
                                for (int i = 0; i < isSelected.length; i++) {
                                  isSelected[i] = i == index;
                                }
                                print(isSelected[index].toString());
                              });
                            },
                            isSelected: isSelected,
                          ),
                        ),
                      ]));
            },
          );
        });
  }
}

现在我想显示在切换按钮中选择的值,并将其显示在上面禁用的 TextField 中。 谁能告诉我怎么做?

【问题讨论】:

    标签: flutter flutter-layout android-togglebutton


    【解决方案1】:

    TextEditingController添加到TextField,然后在切换按钮上的onPressed中,将切换按钮值分配给控制器,如 textFieldController.text = isSelected[index].toString()

    更新: 您可以设置列表中的 3 个值, var values = ['me','you','them']; 然后在onPresed 做这个 textEditingController.text = values[index]; 我认为这可以解决问题

    【讨论】:

    • 我试过了,但它仍然显示一个索引值
    猜你喜欢
    • 1970-01-01
    • 2021-01-31
    • 2021-08-06
    • 1970-01-01
    • 2019-01-15
    • 2018-07-23
    • 2021-01-26
    • 1970-01-01
    相关资源
    最近更新 更多