Is it possible to justify align widgets within the Wrap widget? This is what I currently have:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Wrap(
spacing: 20,
runSpacing: 10,
children: [
ToggleButton(
title: 'Button 1',
selected: false,
),
ToggleButton(
title: 'My second button',
selected: false,
),
ToggleButton(
title: 'Third button',
selected: false,
),
ToggleButton(
title: 'This is button number four',
selected: false,
),
ToggleButton(
title: 'Final button long',
selected: false,
)
],
),
),
);
}
}
class ToggleButton extends StatefulWidget {
const ToggleButton(
{this.selected = false, this.title = '', this.onSelectedChanged});
final bool selected;
final String title;
final Function(String title, bool selected) onSelectedChanged;
@override
_ToggleButtonState createState() => _ToggleButtonState();
}
class _ToggleButtonState extends State<ToggleButton> {
bool selected;
@override
void initState() {
selected = widget.selected;
super.initState();
}
@override
Widget build(BuildContext context) {
return Container(
height: 36,
child: RaisedButton(
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(widget.title),
SizedBox(width: 15),
IgnorePointer(
child: Checkbox(
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
value: selected,
onChanged: (bool value) {},
),
)
],
),
color: selected
? Colors.blue
: Colors.white,
textColor: selected
? Colors.white
: Colors.black.withAlpha(80),
onPressed: () {
setState(() {
selected = !selected;
if (widget.onSelectedChanged != null) {
widget.onSelectedChanged(widget.title, selected);
}
});
},
),
);
}
}
Which gives me this:
But what I'm trying to achieve is this:
Basically to have each row expand to fill the available space. Is that possible? Maybe I need to build my ToggleButton differently?
I'm aware of the alignment: WrapAlignment.spaceBetween but that doesn't work, specially when your container is smaller and you end up with this:
or this:
which are both pretty ugly...
Thank you!



