mirror of
https://github.com/flutter/samples.git
synced 2025-11-10 14:58:34 +00:00
Adding the demo app from my I/O talk. Because AI. ## Pre-launch Checklist - [x] I read the [Flutter Style Guide] _recently_, and have followed its advice. - [x] I signed the [CLA]. - [x] I read the [Contributors Guide]. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] All existing and new tests are passing. --------- Co-authored-by: Brett Morgan <brett.morgan@gmail.com>
59 lines
1.3 KiB
Dart
59 lines
1.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:material_symbols_icons/symbols.dart';
|
|
|
|
typedef StarRatingCallback = void Function(int);
|
|
|
|
class StartRating extends StatefulWidget {
|
|
const StartRating({
|
|
super.key,
|
|
required this.starColor,
|
|
required this.onTap,
|
|
this.initialRating = -1,
|
|
});
|
|
|
|
final Color starColor;
|
|
final int initialRating;
|
|
|
|
/// If [onTap] is not null, the stars are interactive
|
|
final StarRatingCallback? onTap;
|
|
|
|
@override
|
|
State<StartRating> createState() => _StartRatingState();
|
|
}
|
|
|
|
class _StartRatingState extends State<StartRating> {
|
|
late int selectedIdx;
|
|
|
|
@override
|
|
void initState() {
|
|
selectedIdx = widget.initialRating - 1;
|
|
super.initState();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Row(
|
|
children: [
|
|
...List.generate(
|
|
5,
|
|
(index) => GestureDetector(
|
|
onTap: widget.onTap != null
|
|
? () {
|
|
setState(() {
|
|
selectedIdx = index;
|
|
});
|
|
widget.onTap!(index);
|
|
}
|
|
: null,
|
|
child: Icon(
|
|
Symbols.kid_star,
|
|
color: widget.starColor,
|
|
fill: selectedIdx >= index ? 1 : 0,
|
|
),
|
|
),
|
|
)
|
|
],
|
|
);
|
|
}
|
|
}
|