chips_choice 1.0.0
chips_choice: ^1.0.0 copied to clipboard
Lite version of smart_select package, zero dependencies, an easy way to provide a single or multiple choice chips.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:chips_choice/chips_choice.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter ChipsChoice',
theme: ThemeData(
primarySwatch: Colors.red,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int tag = 1;
List<String> tags = [];
List<String> options = [
'News', 'Entertainment', 'Politics',
'Automotive', 'Sports', 'Education',
'Fashion', 'Travel', 'Food', 'Tech',
'Science',
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter ChipsChoice'),
actions: <Widget>[
IconButton(
icon: Icon(Icons.help_outline),
onPressed: () => _about(context),
)
],
),
body: ListView(
padding: EdgeInsets.all(5),
children: <Widget>[
Content(
title: 'Scrollable List Single Choice',
child: ChipsChoice<int>.single(
value: tag,
options: ChipsChoiceOption.listFrom<int, String>(
source: options,
value: (i, v) => i,
label: (i, v) => v,
),
onChanged: (val) => setState(() => tag = val),
),
),
Content(
title: 'Scrollable List Multiple Choice',
child: ChipsChoice<String>.multiple(
value: tags,
options: ChipsChoiceOption.listFrom<String, String>(
source: options,
value: (i, v) => v,
label: (i, v) => v,
),
onChanged: (val) => setState(() => tags = val),
),
),
Content(
title: 'Wrapped List Single Choice',
child: ChipsChoice<int>.single(
value: tag,
options: ChipsChoiceOption.listFrom<int, String>(
source: options,
value: (i, v) => i,
label: (i, v) => v,
),
onChanged: (val) => setState(() => tag = val),
isWrapped: true,
),
),
Content(
title: 'Wrapped List Multiple Choice',
child: ChipsChoice<String>.multiple(
value: tags,
options: ChipsChoiceOption.listFrom<String, String>(
source: options,
value: (i, v) => v,
label: (i, v) => v,
),
onChanged: (val) => setState(() => tags = val),
isWrapped: true,
),
),
Content(
title: 'Disabled Choice item',
child: ChipsChoice<int>.single(
value: tag,
options: ChipsChoiceOption.listFrom<int, String>(
source: options,
value: (i, v) => i,
label: (i, v) => v,
disabled: (i, v) => [0, 2, 5].contains(i),
),
onChanged: (val) => setState(() => tag = val),
isWrapped: true,
),
),
Content(
title: 'Hidden Choice item',
child: ChipsChoice<String>.multiple(
value: tags,
options: ChipsChoiceOption.listFrom<String, String>(
source: options,
value: (i, v) => v,
label: (i, v) => v,
hidden: (i, v) => ['Science', 'Politics', 'News', 'Tech'].contains(v),
),
onChanged: (val) => setState(() => tags = val),
isWrapped: true,
),
),
Content(
title: 'Custom Choice item',
child: ChipsChoice<String>.multiple(
value: tags,
options: ChipsChoiceOption.listFrom<String, String>(
source: options,
value: (i, v) => v,
label: (i, v) => v,
),
itemBuilder: (item, selected, onSelect) {
return CustomChip(item.value, item.label, selected, onSelect);
},
onChanged: (val) => setState(() => tags = val),
isWrapped: true,
),
),
],
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
class CustomChip<T> extends StatelessWidget {
final T value;
final String label;
final bool selected;
final Function(T value, bool selected) onSelect;
CustomChip(
this.value,
this.label,
this.selected,
this.onSelect,
{ Key key }
) : super(key: key);
@override
Widget build(BuildContext context) {
return AnimatedContainer(
margin: EdgeInsets.symmetric(vertical: 5),
duration: Duration(milliseconds: 300),
decoration: BoxDecoration(
color: selected ? Colors.green : Colors.transparent,
border: Border.all(
color: selected ? Colors.green : Colors.grey,
width: 1,
),
),
child: InkWell(
onTap: () => onSelect(value, !selected),
child: Padding(
padding: EdgeInsets.symmetric(
vertical: 7,
horizontal: 9,
),
child: Text(
label,
style: TextStyle(
color: selected ? Colors.white : Colors.black45,
),
),
),
),
);
}
}
class Content extends StatelessWidget {
final String title;
final Widget child;
Content({
Key key,
@required this.title,
@required this.child,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Card(
elevation: 2,
margin: EdgeInsets.all(5),
clipBehavior: Clip.antiAliasWithSaveLayer,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
width: double.infinity,
padding: EdgeInsets.all(15),
color: Colors.blueGrey[50],
child: Text(
title,
style: TextStyle(
color: Colors.blueGrey,
fontWeight: FontWeight.w500
),
),
),
child,
],
),
);
}
}
void _about(BuildContext context) {
showDialog(
context: context,
builder: (_) => Dialog(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ListTile(
title: Text(
'chips_choice',
style: Theme.of(context).textTheme.headline.merge(TextStyle(color: Colors.black87)),
),
subtitle: Text('by davigmacode'),
trailing: IconButton(
icon: Icon(Icons.close),
onPressed: () => Navigator.pop(context),
),
),
Flexible(
fit: FlexFit.loose,
child: Container(
padding: EdgeInsets.symmetric(horizontal: 15),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(
'Easy way to provide a single or multiple choice chips.',
style: Theme.of(context).textTheme.body1.merge(TextStyle(color: Colors.black54)),
),
Container(height: 15),
],
),
),
)
],
),
),
);
}