当按下按钮时,我无法获得按钮来切换值

问题描述 投票:1回答:1

Introduction:我已经尝试了一段时间,以便在按下筹码失败时能够切换颜色。如果有人可以告诉我我在代码中做错了什么,对我来说将是一项很棒的服务。

期望:我想要切换方法来切换'isSelected'布尔值。按下芯片时的值。当我按下按钮时,绝对没有任何反应。我看到该值确实从true更改为false,但是它并没有像预期的那样更改我的颜色,并且在第一次按下后也不会再次更改该值。

代码:

import 'package:flutter/material.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',
      theme: ThemeData(
        primarySwatch: Colors.deepPurple,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    List<Actions> actions = Actions.all();
    List<Widget> newWidgets = [];

    for (var i in actions) {
      newWidgets.add(myChips(
          isSelected: i.isSelected,
          chipName: i.name,
          function: () {
            setState(() {
              i.toggle();
              print(i.isSelected);
            });
          }));
    }

    return Scaffold(
      appBar: AppBar(
        title: Text('Them'),
      ),
      body: Scaffold(
        body: CustomScrollView(
          slivers: <Widget>[
            SliverAppBar(
              title: Text('Test'),
              pinned: true,
              expandedHeight: 400.0,
              flexibleSpace: FlexibleSpaceBar(
                background: Image.asset('assets/abc.png'),
              ),
            ),
            SliverList(
              delegate: SliverChildBuilderDelegate(
                (context, index) => Container(
                  child: Column(
                    children: <Widget>[
                      Column(
                        children: <Widget>[
                          Container(
                            child: Padding(
                              padding: const EdgeInsets.all(16.0),
                              child: Text(
                                'Name of Chips',
                                style: TextStyle(
                                  color: Colors.black,
                                  fontSize: 24.0,
                                  fontWeight: FontWeight.bold,
                                ),
                              ),
                            ),
                          ),
                          Wrap(
                            direction: Axis.horizontal,
                            spacing: 10.0,
                            runSpacing: 5.0,
                            children: newWidgets,
                          ),
                          categoryDivider(context),
                        ],
                      )
                    ],
                  ),
                ),
                childCount: 1,
              ),
            ),
          ],
        ),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  Container myChips({bool isSelected, String chipName, Function function}) {
    return Container(
      child: RaisedButton(
          color: isSelected ? Color(0xffeadffd) : Color(0xffededed),
          child: Text(
            chipName,
            style: TextStyle(
              color: new Color(0xff6200ee),
            ),
          ),
          onPressed: function,
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(30.0))),
    );
  }
}

// divider
Container categoryDivider(BuildContext context) {
  return Container(
    height: 1.0,
    width: MediaQuery.of(context).size.width,
    color: Colors.grey,
    margin: const EdgeInsets.only(left: 10.0, right: 10.0),
  );
}

class Actions {
  Actions({this.name});

  String name;
  bool isSelected = false;

  toggle() {
    isSelected = !isSelected;
    print(isSelected);
  }

  static List<Actions> all() {
    return [
      Actions(name: 'A'),
      Actions(name: 'B'),
      Actions(name: 'C'),
      Actions(name: 'D'),
      Actions(name: 'E'),
      Actions(name: 'F'),
      Actions(name: 'G'),
      Actions(name: 'H'),
      Actions(name: 'I'),
      Actions(name: 'J'),
      Actions(name: 'K'),
      Actions(name: 'L'),
    ];
  }
}
flutter switch-statement
1个回答
1
投票

你在做什么错?

© www.soinside.com 2019 - 2024. All rights reserved.