Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.4 单选开关和复选框 一节中代码示例中可能有歧义 #217

Open
Liu-code3 opened this issue May 28, 2024 · 0 comments
Open

Comments

@Liu-code3
Copy link

书中代码示例歧义的地方

image

问题描述

  • 如果直接把SwitchAndCheckBoxTestRoute放在mart.dart中的MateriaApp中的home属性的话,会报错。报错信息为: No Material widget found
  • 修改后如下
import 'package:flutter/material.dart';

class SwitchAndCheckBoxTestRoute extends StatefulWidget {
  const SwitchAndCheckBoxTestRoute({super.key});

  @override
  State<SwitchAndCheckBoxTestRoute> createState() =>
      _SwitchAndCheckBoxTestRouteState();
}

class _SwitchAndCheckBoxTestRouteState
    extends State<SwitchAndCheckBoxTestRoute> {
  bool _switchSelected = true; // 维护单选开关状态
  bool _checkboxSelected = true; // 维护复选框状态
  @override
  Widget build(BuildContext context) {
    return Scaffold( // 要引入 Material 小部件,您可以直接包含一个小部件,或者使用包含 Material 本身的小部件,例如 Card ,Dialog , Drawer 或 Scaffold 。
      body: Column(
        children: [
          Switch(
              value: _switchSelected,
              onChanged: (value) {
                setState(() {
                  _switchSelected = value;
                });
              }),
          Checkbox(
              value: _checkboxSelected,
              onChanged: (value) {
                setState(() {
                  _checkboxSelected = value!;
                });
              })
        ],
      ),
    );
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant