【问题标题】:How to implement CheckBox in Flutter?如何在 Flutter 中实现 CheckBox?
【发布时间】:2019-03-19 17:20:07
【问题描述】:

这里我提到了我的复选框代码。我是 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');
          }),
    ],
  ),
);

【问题讨论】:

  • 那么你的问题是什么?
  • 我的问题基本上是我想在颤振中使用复选框实现我的凭据用户名和密码的“记住我”功能。

标签: flutter flutter-layout


【解决方案1】:

如果您需要带有标签的Checkbox,那么您可以使用CheckboxListTile

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

【讨论】:

  • 请求是针对复选框而不是复选框列表图块.....
【解决方案2】:

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

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

【讨论】:

  • 嘿马塞尔感谢您的回答。这对我来说可以。其实我问的和你的回答一样。
  • 完美,我很高兴能帮助你:) 你介意接受我的回答吗?
  • 不,我可以马塞尔。
  • 为什么你不能接受这个答案,你说的是“it works fine for me”?
【解决方案3】:

你试试那个代码,它可以完美运行

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

它给你输出类似的东西

【讨论】:

    【解决方案4】:

    flutter 中的复选框是一个材料设计小部件。它总是在 Stateful Widget 中使用,因为它不维护自己的状态。我们可以使用它的 onChanged 属性来交互或修改 Flutter 应用中的其他小部件。与大多数其他 Flutter 小部件一样,它还带有许多属性,如 activeColor、checkColor、mouseCursor 等,让开发人员可以完全控制小部件的外观。

    例子:

    import 'package:flutter/material.dart';
    
    //importing material design liverary
    void main() {
    runApp(MaterialApp(
        //runApp method
        home: HomePage(),
    ));//MaterialApp
    }
    
    class HomePage extends StatefulWidget {
    @override
    _HomePageState createState() => _HomePageState();
    }
    
    class _HomePageState extends State<HomePage> {
    bool value = false;
    
    @override
    //App widget tree
    Widget build(BuildContext context) {
        return MaterialApp(
        home: Scaffold(
            appBar: AppBar(
            title: Text('GeeksforGeeks'),
            backgroundColor: Colors.greenAccent[400],
            leading: IconButton(
                icon: Icon(Icons.menu),
                tooltip: 'Menu',
                onPressed: () {},
            ), //IcoButton
            ), //AppBar
            body: Center(
            /** Card Widget **/
            child: Card(
                child: Padding(
                padding: const EdgeInsets.all(15.0),
                child: SizedBox(
                    width: 430,
                    height: 700,
                    child: Column(
                    children: [
                        Text(
                        'Algorithms',
                        style: TextStyle(
                            color: Colors.greenAccent[400],
                            fontSize: 30), //TextStyle
                        ), //Text
                        SizedBox(height: 10),
                        Row(
                        children: <Widget>[
                            SizedBox(
                            width: 10,
                            ), //SizedBox
                            Text(
                            'Liberary Implementation Of Searching Algorithm: ',
                            style: TextStyle(fontSize: 17.0),
                            ), //Text
                            SizedBox(width: 10), //SizedBox
                            /** Checkbox Widget **/
                            Checkbox(
                            value: this.value,
                            onChanged: (bool value) {
                                setState(() {
                                this.value = value;
                                });
                            },
                            ), //Checkbox
                        ], //<Widget>[]
                        ), //Row
                    ],
                    ), //Column
                ), //SizedBox
                ), //Padding
            ), //Card
            ), //Center//Center
        ), //Scaffold
        ); //MaterialApp
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-23
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 2020-07-19
      • 1970-01-01
      • 1970-01-01
      • 2020-06-14
      相关资源
      最近更新 更多