如何在 Flutter 中实现 CheckBox?

Dha*_*hra 39 flutter flutter-layout

在这里我提到了我的复选框代码。我是 flutter 的新手,所以我必须实现它以实现记住我的功能。

代码:

 Container(
 padding: EdgeInsets.all(10.0),
  child: Column(
    children: <Widget>[
     new Checkbox(value: checkBoxValue,
          activeColor: Colors.green,
          onChanged:(bool newValue){
        setState(() {
          checkBoxValue = newValue;
        });
       Text('Remember me');
          }),
    ],
  ),
);
Run Code Online (Sandbox Code Playgroud)

Nik*_*ikh 58

如果你需要一个Checkbox带标签的,那么你可以使用一个CheckboxListTile

CheckboxListTile(
  title: Text("title text"),
  value: checkedValue,
  onChanged: (newValue) {
    setState(() {
      checkedValue = newValue;
    });
  },
  controlAffinity: ListTileControlAffinity.leading,  //  <-- leading Checkbox
)
Run Code Online (Sandbox Code Playgroud)

  • 该请求是针对复选框而不是复选框列表图块...... (2认同)

Mar*_*cel 26

我不确定我是否正确理解了您的问题,但是如果是如何将功能绑定到Checkbox,那么StateaStatefulWidget应该作为您的最小工作示例:

class _MyWidgetState extends State<MyWidget> {
  bool rememberMe = false;

  void _onRememberMeChanged(bool newValue) => setState(() {
    rememberMe = newValue;

    if (rememberMe) {
      // TODO: Here goes your functionality that remembers the user.
    } else {
      // TODO: Forget the user
    }
  });

  @override
  Widget build(BuildContext context) {
    return Checkbox(
      value: rememberMe,
      onChanged: _onRememberMeChanged
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 完美,我很高兴能帮到你 :) 你介意接受我的回答吗? (2认同)

Soh*_*lam 6

你试试那个代码它工作得很好

Main.dart

import 'package:flutter/material.dart';

import 'checkbox_in_listview_task-7.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',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      //home: MyHomePage(title: 'Flutter Demo Home Page'),
      home: CheckBoxInListview(),
    );   } } 
Run Code Online (Sandbox Code Playgroud)

checkbox_in_listview_task-7.dart

import 'package:flutter/material.dart';

class GetCheckValue extends StatefulWidget {
  @override
  GetCheckValueState createState() {
    return new GetCheckValueState();
  }
}

class GetCheckValueState extends State<GetCheckValue> {
  bool _isChecked = true;
  String _currText = '';

  List<String> text = ["InduceSmile.com", "Flutter.io", "google.com"];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Get check Value Example"),
      ),
      body: Column(
        children: <Widget>[
          Expanded(
            child: Center(
              child: Text(_currText,
                  style: TextStyle(
                    fontSize: 20.0,
                    fontWeight: FontWeight.bold,
                  )),
            ),
          ),
          Expanded(
              child: Container(
            height: 350.0,
            child: Column(
              children: text
                  .map((t) => CheckboxListTile(
                        title: Text(t),
                        value: _isChecked,
                        onChanged: (val) {
                          setState(() {
                            _isChecked = val;
                            if (val == true) {
                              _currText = t;
                            }
                          });
                        },
                      ))
                  .toList(),
            ),
          )),
        ],
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

它给你输出类似的东西

输出