Skip to content

Commit 91272eb

Browse files
committed
more layout refactor
1 parent 4d65d1a commit 91272eb

File tree

3 files changed

+39
-25
lines changed

3 files changed

+39
-25
lines changed

firebase_ai_logic_showcase/lib/demos/chat/chat_demo.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class _ChatDemoState extends ConsumerState<ChatDemo> {
4444
Uint8List? _attachment;
4545
final ScrollController _scrollController = ScrollController();
4646
bool _loading = false;
47+
static bool _functionCallDialogShown = false;
4748

4849
@override
4950
void initState() {
@@ -54,9 +55,12 @@ class _ChatDemoState extends ConsumerState<ChatDemo> {
5455
_userTextInputController.text =
5556
'Hey Gemini! Can you set the app color to purple?';
5657
WidgetsBinding.instance.addPostFrameCallback((_) async {
57-
final trigger = await _showFunctionCallDialog();
58-
if (mounted && trigger == true) {
59-
sendMessage(_userTextInputController.text);
58+
if (!_functionCallDialogShown) {
59+
_functionCallDialogShown = true;
60+
final trigger = await _showFunctionCallDialog();
61+
if (mounted && trigger == true) {
62+
sendMessage(_userTextInputController.text);
63+
}
6064
}
6165
});
6266
}

firebase_ai_logic_showcase/lib/demos/chat_nano/chat_nano_demo.dart

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ import '../../shared/ui/chat_components/model_picker.dart';
2828
import '../../shared/models/models.dart';
2929

3030
class ChatDemoNano extends ConsumerStatefulWidget {
31-
const ChatDemoNano({super.key});
31+
const ChatDemoNano({super.key, this.isSelected = false});
32+
final bool isSelected;
3233

3334
@override
3435
ConsumerState<ChatDemoNano> createState() => ChatDemoNanoState();
@@ -46,6 +47,7 @@ class ChatDemoNanoState extends ConsumerState<ChatDemoNano> {
4647
final ScrollController _scrollController = ScrollController();
4748
bool _loading = false;
4849
OverlayPortalController opController = OverlayPortalController();
50+
static bool _pickerHasBeenShown = false;
4951

5052
@override
5153
void initState() {
@@ -55,6 +57,26 @@ class ChatDemoNanoState extends ConsumerState<ChatDemoNano> {
5557
_chatService.init();
5658
_userTextInputController.text =
5759
'Hot air balloons rising over the San Francisco Bay at golden hour with a view of the Golden Gate Bridge. Make it anime style.';
60+
_checkAndShowPicker();
61+
}
62+
63+
@override
64+
void didUpdateWidget(ChatDemoNano oldWidget) {
65+
super.didUpdateWidget(oldWidget);
66+
if (widget.isSelected != oldWidget.isSelected) {
67+
_checkAndShowPicker();
68+
}
69+
}
70+
71+
void _checkAndShowPicker() {
72+
if (widget.isSelected && !_pickerHasBeenShown) {
73+
_pickerHasBeenShown = true;
74+
WidgetsBinding.instance.addPostFrameCallback((_) {
75+
if (mounted) {
76+
showModelPicker();
77+
}
78+
});
79+
}
5880
}
5981

6082
@override

firebase_ai_logic_showcase/lib/flutter_firebase_ai_demo.dart

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,22 @@ class DemoHomeScreen extends StatefulWidget {
2727

2828
class _DemoHomeScreenState extends State<DemoHomeScreen> {
2929
int _selectedIndex = 0;
30-
final GlobalKey<ChatDemoNanoState> _chatNanoKey = GlobalKey();
31-
bool _nanoPickerHasBeenShown = false;
32-
33-
late final List<Widget> _demoPages;
34-
35-
@override
36-
void initState() {
37-
super.initState();
38-
_demoPages = <Widget>[
39-
const ChatDemo(),
40-
const LiveAPIDemo(),
41-
const MultimodalDemo(),
42-
ChatDemoNano(key: _chatNanoKey),
43-
];
44-
}
4530

4631
void _onItemTapped(int index) {
47-
if (index == 3 && !_nanoPickerHasBeenShown) {
48-
_chatNanoKey.currentState?.showModelPicker();
49-
_nanoPickerHasBeenShown = true;
50-
}
5132
setState(() {
5233
_selectedIndex = index;
5334
});
5435
}
5536

5637
@override
5738
Widget build(BuildContext context) {
39+
final List<Widget> demoPages = <Widget>[
40+
const ChatDemo(),
41+
const LiveAPIDemo(),
42+
const MultimodalDemo(),
43+
ChatDemoNano(isSelected: _selectedIndex == 3),
44+
];
45+
5846
final List<({Widget icon, String label, Widget? selectedIcon})> destinations = [
5947
(icon: const Icon(Icons.chat), label: 'Chat', selectedIcon: null),
6048
(icon: const Icon(Icons.video_chat), label: 'Live API', selectedIcon: null),
@@ -76,7 +64,7 @@ class _DemoHomeScreenState extends State<DemoHomeScreen> {
7664
if (constraints.maxWidth < 600) {
7765
// Use BottomNavigationBar for smaller screens
7866
return Scaffold(
79-
body: IndexedStack(index: _selectedIndex, children: _demoPages),
67+
body: IndexedStack(index: _selectedIndex, children: demoPages),
8068
bottomNavigationBar: BottomNavigationBar(
8169
type: BottomNavigationBarType.fixed,
8270
items: destinations
@@ -117,7 +105,7 @@ class _DemoHomeScreenState extends State<DemoHomeScreen> {
117105
Expanded(
118106
child: IndexedStack(
119107
index: _selectedIndex,
120-
children: _demoPages,
108+
children: demoPages,
121109
),
122110
),
123111
],

0 commit comments

Comments
 (0)