Skip to content

Commit

Permalink
disable flickering in menubutton
Browse files Browse the repository at this point in the history
  • Loading branch information
danemadsen committed May 12, 2024
1 parent bb86e39 commit fe24a51
Showing 1 changed file with 57 additions and 26 deletions.
83 changes: 57 additions & 26 deletions lib/ui/mobile/widgets/buttons/menu_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,72 @@ class MenuButton extends StatefulWidget {

class _MenuButtonState extends State<MenuButton> {
final iconButtonKey = GlobalKey();
static LargeLanguageModelType lastModelType = LargeLanguageModelType.none;
static DateTime lastCheck = DateTime.now();
static List<String> cache = [];
List<String> options = [];

bool canUseCache(Session session) {
if (cache.isEmpty && session.model.type != LargeLanguageModelType.llamacpp) return false;

if (session.model.type != lastModelType) return false;

if (DateTime.now().difference(lastCheck).inMinutes > 1) return false;

return true;
}

@override
Widget build(BuildContext context) {
return Consumer<Session>(
builder: (context, session, child) {
return FutureBuilder(
future: session.model.options,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
options = snapshot.data as List<String>;
if (canUseCache(session)) {
options = cache;
return IconButton(
key: iconButtonKey,
icon: const Icon(
Icons.account_tree_rounded,
size: 24,
),
onPressed: onPressed,
);
}
else {
lastModelType = session.model.type;
lastCheck = DateTime.now();

return IconButton(
key: iconButtonKey,
icon: const Icon(
Icons.account_tree_rounded,
size: 24,
),
onPressed: onPressed,
);
} else {
return const Padding(
padding: EdgeInsets.all(8.0), // Adjust padding to match the visual space of the IconButton
child: SizedBox(
width: 24, // Width of the CircularProgressIndicator
height: 24, // Height of the CircularProgressIndicator
child: Center(
child: CircularProgressIndicator(
strokeWidth: 3.0, // Adjust the thickness of the spinner here
return FutureBuilder(
future: session.model.options,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
options = snapshot.data as List<String>;
cache = options;

return IconButton(
key: iconButtonKey,
icon: const Icon(
Icons.account_tree_rounded,
size: 24,
),
onPressed: onPressed,
);
} else {
return const Padding(
padding: EdgeInsets.all(8.0), // Adjust padding to match the visual space of the IconButton
child: SizedBox(
width: 24, // Width of the CircularProgressIndicator
height: 24, // Height of the CircularProgressIndicator
child: Center(
child: CircularProgressIndicator(
strokeWidth: 3.0, // Adjust the thickness of the spinner here
),
),
),
),
);
);
}
}
}
);
);
}
}
);
}
Expand Down Expand Up @@ -130,6 +160,7 @@ class _MenuButtonState extends State<MenuButton> {
contentPadding: const EdgeInsets.symmetric(horizontal: 8.0),
title: const Text('App Settings'),
onTap: () {
cache.clear();
Navigator.pop(context);
Navigator.push(
context,
Expand Down

0 comments on commit fe24a51

Please sign in to comment.