Skip to content

Instantly share code, notes, and snippets.

@HansMuller
Created October 5, 2022 02:04
Show Gist options
  • Select an option

  • Save HansMuller/fec40ad06b7124c0db2eb6b7b9a91bb2 to your computer and use it in GitHub Desktop.

Select an option

Save HansMuller/fec40ad06b7124c0db2eb6b7b9a91bb2 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
class Star {
const Star(this.title);
final String title;
}
class StarView extends StatefulWidget {
const StarView({ super.key, required this.star, required this.onTitleChanged });
final Star star;
final ValueChanged<String> onTitleChanged;
@override
State<StarView> createState() => _StarViewState();
}
class _StarViewState extends State<StarView> {
late final TextEditingController textController;
@override
Widget build(BuildContext context) {
final ColorScheme colorScheme = Theme.of(context).colorScheme;
return Container(
padding: const EdgeInsets.all(64),
alignment: Alignment.center,
decoration: ShapeDecoration(
color: colorScheme.primaryContainer,
shape: StarBorder(
side: BorderSide(
color: colorScheme.outline,
width: 9,
),
points: 7,
innerRadiusRatio: 0.75,
pointRounding: 0.5,
valleyRounding: 0.25,
),
),
child: TextField(
controller: textController,
onChanged: widget.onTitleChanged,
decoration: const InputDecoration(
border: OutlineInputBorder(),
),
),
);
}
@override
void initState() {
super.initState();
textController = TextEditingController(text: widget.star.title);
}
@override
void dispose() {
textController.dispose();
super.dispose();
}
}
class Home extends StatefulWidget {
const Home({ super.key });
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
int selectedIndex = 0;
List<Star> stars = [
const Star('One'),
const Star('Two'),
const Star('Free'),
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
padding: const EdgeInsets.all(64),
alignment: Alignment.center,
child: AspectRatio(
aspectRatio: 1,
child: StarView(
star: stars[selectedIndex],
onTitleChanged: (String newTitle) {
setState(() {
stars[selectedIndex] = Star(newTitle);
});
},
),
),
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: selectedIndex,
onTap: (int index) {
setState(() {
selectedIndex = index;
});
},
items: stars.map<BottomNavigationBarItem>((Star star) {
return BottomNavigationBarItem(
icon: const Icon(Icons.article),
label: star.title,
);
}).toList(),
),
);
}
}
void main() {
runApp(
MaterialApp(
theme: ThemeData(
useMaterial3: true,
colorSchemeSeed: const Color(0xff6750a4),
),
home: const Home(),
),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment