0

I want make selectable buttons(I know how to do it but), i cant do like in example like this. The first button(all) doesnt need to have icon. And the last two button need to be on new line. When i try all buttons get icon, and i dont know to to make the last 2 button be on NEW LINE Can you explain to me how to do it. Thank you

List<IconData> icon = [Icons.photo, Icons.video_call, Icons.video_library, Icons.add_circle];
List<bool> isSelected = List.generate(4, (index) => false);
List<String> innerText = ['All', 'Photo', 'Video', 'Reel', 'Story']; (this in main file)
```

```
import 'package:flutter/material.dart';
import '../my_const.dart';

class MyToggleButtons extends StatelessWidget{
  final List<String> text;
  final List<IconData> iconData;
  final List<bool> isSelected;
  final Function setStateFunction;
  const MyToggleButtons({Key? key, required this.text, required this.iconData, required this.isSelected, required this.setStateFunction,})
      : super(key: key);


  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: ToggleButtons(
        isSelected: isSelected,
        splashColor: Colors.green,
        fillColor: Colors.transparent,
        onPressed: (int index){
          setStateFunction((){
            isSelected[index] = !isSelected[index];
          });
        },
        renderBorder: false,
        children:
        List<Widget>.generate(iconData.length, (index){
          return CustomButton(
            text: text[index],
            iconData: iconData[index],
            isSelected: isSelected[index],
          );
        }),
      ),
    );
  }
}

class CustomButton extends StatelessWidget {
  final String text;
  final IconData iconData;
  final bool isSelected;
  const CustomButton({Key? key, required this.text, required this.iconData, this.isSelected = false, }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
        Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          const SizedBox(
            width: 10,
          ),
          SizedBox.fromSize(
            size: const Size(102, 52),
            child: Container(
              decoration: BoxDecoration(
                color: isSelected ? kBlue : superWhite,
                borderRadius: BorderRadius.circular(10),
                border: Border.all(
                    width: 3.0,
                    color: kBlue
                ),
              ),
              child: InkWell(
                onTap: (){},
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: const [
                    Icon(iconData,
           color: isSelected ? Colors.white : Colors.blue,),
                    Text('Video', style: TextStyle(fontSize: 14, color: kBlue, fontWeight: FontWeight.w600),)
                  ],
                ),
              ),
            ),
          ),
        ]
    ),
    ]
    );
  }
}
```
```
`
3
  • Instead of Row try to Use Warp and in the Warp you can control Alignment such as : alignment: WrapAlignment.center, and spacing such as: spacing: 12,...etc Commented Nov 4, 2022 at 10:32
  • ok thanks. But how to make the first button not to have an icon? Commented Nov 4, 2022 at 10:36
  • I cant try it now , but I think you can do a something like this: isSelected[index] == isSelected[0] ? Sizedbox() : Icon(iconData, color: isSelected ? Colors.white : Colors.blue,), Text('Video', style: TextStyle(fontSize: 14, color: kBlue, fontWeight: FontWeight.w600),) Commented Nov 4, 2022 at 10:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.