如何在Flutter中实现CheckBox

时间:2018-10-15 09:51:52

标签: flutter flutter-layout

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

代码:

 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');
          }),
    ],
  ),
);

3 个答案:

答案 0 :(得分:2)

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

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
    );
  }
}

答案 1 :(得分:2)

如果您需要带有标签的复选框,则可以使用CheckboxListTile

CheckboxListTile(
    title: Text("title text"),
    value: checkedValue,
    onChanged: (newValue) { ... },
    controlAffinity: ListTileControlAffinity.leading,  //  <-- leading Checkbox
  )

答案 2 :(得分:0)

U尝试使其完美运行的代码

  

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(),
    );   } } 
  

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(),
            ),
          )),
        ],
      ),
    );
  }
}

它给您提供了类似的东西

Output