mirror of
https://github.com/flutter/samples.git
synced 2025-11-08 13:58:47 +00:00
Flutter 3.29 beta (#2571)
This commit is contained in:
@@ -55,11 +55,7 @@ class _GenerativeAISampleState extends State<GenerativeAISample> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AnimatedBuilder(
|
||||
animation: Listenable.merge([
|
||||
themeColor,
|
||||
themeMode,
|
||||
textScaleFactor,
|
||||
]),
|
||||
animation: Listenable.merge([themeColor, themeMode, textScaleFactor]),
|
||||
builder: (context, child) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
@@ -69,22 +65,23 @@ class _GenerativeAISampleState extends State<GenerativeAISample> {
|
||||
themeMode: themeMode.value,
|
||||
builder: (context, child) {
|
||||
return MediaQuery(
|
||||
data: MediaQuery.of(context).copyWith(
|
||||
textScaler: TextScaler.linear(textScaleFactor.value)),
|
||||
data: MediaQuery.of(
|
||||
context,
|
||||
).copyWith(textScaler: TextScaler.linear(textScaleFactor.value)),
|
||||
child: child!,
|
||||
);
|
||||
},
|
||||
home: switch (apiKey) {
|
||||
final providedKey? => Example(
|
||||
title: widget.title,
|
||||
apiKey: providedKey,
|
||||
),
|
||||
title: widget.title,
|
||||
apiKey: providedKey,
|
||||
),
|
||||
_ => ApiKeyWidget(
|
||||
title: widget.title,
|
||||
onSubmitted: (key) {
|
||||
setState(() => apiKey = key);
|
||||
},
|
||||
),
|
||||
title: widget.title,
|
||||
onSubmitted: (key) {
|
||||
setState(() => apiKey = key);
|
||||
},
|
||||
),
|
||||
},
|
||||
);
|
||||
},
|
||||
@@ -93,11 +90,7 @@ class _GenerativeAISampleState extends State<GenerativeAISample> {
|
||||
}
|
||||
|
||||
class Example extends StatefulWidget {
|
||||
const Example({
|
||||
super.key,
|
||||
required this.apiKey,
|
||||
required this.title,
|
||||
});
|
||||
const Example({super.key, required this.apiKey, required this.title});
|
||||
|
||||
final String apiKey, title;
|
||||
|
||||
@@ -189,9 +182,7 @@ class _ExampleState extends State<Example> {
|
||||
Future<GenerateContentResponse> callWithActions(
|
||||
Iterable<Content> prompt,
|
||||
) async {
|
||||
final response = await model.generateContent(
|
||||
_history.followedBy(prompt),
|
||||
);
|
||||
final response = await model.generateContent(_history.followedBy(prompt));
|
||||
if (response.candidates.isNotEmpty) {
|
||||
_history.addAll(prompt);
|
||||
_history.add(response.candidates.first.content);
|
||||
@@ -203,16 +194,20 @@ class _ExampleState extends State<Example> {
|
||||
case 'change_theme_color':
|
||||
final hex = args['hex'] as String;
|
||||
if (hex.length != 6) {
|
||||
actions.add(FunctionResponse(fn.name, {
|
||||
'type': 'Error',
|
||||
'message': 'hex must be exactly 6 characters',
|
||||
}));
|
||||
actions.add(
|
||||
FunctionResponse(fn.name, {
|
||||
'type': 'Error',
|
||||
'message': 'hex must be exactly 6 characters',
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
themeColor.value = Color(int.parse('0xFF$hex'));
|
||||
actions.add(FunctionResponse(fn.name, {
|
||||
'type': 'Success',
|
||||
'message': 'theme color updated',
|
||||
}));
|
||||
actions.add(
|
||||
FunctionResponse(fn.name, {
|
||||
'type': 'Success',
|
||||
'message': 'theme color updated',
|
||||
}),
|
||||
);
|
||||
}
|
||||
break;
|
||||
case 'change_theme_mode':
|
||||
@@ -223,18 +218,22 @@ class _ExampleState extends State<Example> {
|
||||
'dark' => ThemeMode.dark,
|
||||
(_) => ThemeMode.system,
|
||||
};
|
||||
actions.add(FunctionResponse(fn.name, {
|
||||
'type': 'Success',
|
||||
'message': 'theme mode updated',
|
||||
}));
|
||||
actions.add(
|
||||
FunctionResponse(fn.name, {
|
||||
'type': 'Success',
|
||||
'message': 'theme mode updated',
|
||||
}),
|
||||
);
|
||||
break;
|
||||
case 'change_text_scale_factor':
|
||||
final value = args['scale'] as num;
|
||||
textScaleFactor.value = value.toDouble();
|
||||
actions.add(FunctionResponse(fn.name, {
|
||||
'type': 'Success',
|
||||
'message': 'font scale updated',
|
||||
}));
|
||||
actions.add(
|
||||
FunctionResponse(fn.name, {
|
||||
'type': 'Success',
|
||||
'message': 'font scale updated',
|
||||
}),
|
||||
);
|
||||
break;
|
||||
default:
|
||||
}
|
||||
@@ -266,30 +265,31 @@ class _ExampleState extends State<Example> {
|
||||
builder: (context, child) {
|
||||
final reversed = messages.value.reversed;
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(widget.title),
|
||||
),
|
||||
body: messages.value.isEmpty
|
||||
? const Center(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(32.0),
|
||||
child: Text('Start changing the theme! Try typing '
|
||||
appBar: AppBar(title: Text(widget.title)),
|
||||
body:
|
||||
messages.value.isEmpty
|
||||
? const Center(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(32.0),
|
||||
child: Text(
|
||||
'Start changing the theme! Try typing '
|
||||
'in requests like "Make the colors darker" or "Make the '
|
||||
'font larger" and see what happens.'),
|
||||
'font larger" and see what happens.',
|
||||
),
|
||||
),
|
||||
)
|
||||
: ListView.builder(
|
||||
padding: const EdgeInsets.all(8),
|
||||
reverse: true,
|
||||
itemCount: reversed.length,
|
||||
itemBuilder: (context, index) {
|
||||
final (sender, message) = reversed.elementAt(index);
|
||||
return MessageWidget(
|
||||
isFromUser: sender == Sender.user,
|
||||
text: message,
|
||||
);
|
||||
},
|
||||
),
|
||||
)
|
||||
: ListView.builder(
|
||||
padding: const EdgeInsets.all(8),
|
||||
reverse: true,
|
||||
itemCount: reversed.length,
|
||||
itemBuilder: (context, index) {
|
||||
final (sender, message) = reversed.elementAt(index);
|
||||
return MessageWidget(
|
||||
isFromUser: sender == Sender.user,
|
||||
text: message,
|
||||
);
|
||||
},
|
||||
),
|
||||
bottomNavigationBar: BottomAppBar(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: Row(
|
||||
@@ -328,7 +328,4 @@ class _ExampleState extends State<Example> {
|
||||
}
|
||||
}
|
||||
|
||||
enum Sender {
|
||||
user,
|
||||
system,
|
||||
}
|
||||
enum Sender { user, system }
|
||||
|
||||
@@ -18,11 +18,7 @@ import 'package:url_launcher/link.dart';
|
||||
import 'text_field_decoration.dart';
|
||||
|
||||
class ApiKeyWidget extends StatelessWidget {
|
||||
ApiKeyWidget({
|
||||
super.key,
|
||||
required this.onSubmitted,
|
||||
required this.title,
|
||||
});
|
||||
ApiKeyWidget({super.key, required this.onSubmitted, required this.title});
|
||||
|
||||
final String title;
|
||||
final ValueChanged onSubmitted;
|
||||
@@ -31,9 +27,7 @@ class ApiKeyWidget extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(title),
|
||||
),
|
||||
appBar: AppBar(title: Text(title)),
|
||||
body: Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
@@ -50,10 +44,11 @@ class ApiKeyWidget extends StatelessWidget {
|
||||
Link(
|
||||
uri: Uri.https('aistudio.google.com', '/app/apikey'),
|
||||
target: LinkTarget.blank,
|
||||
builder: (context, followLink) => TextButton(
|
||||
onPressed: followLink,
|
||||
child: const Text('Get an API Key'),
|
||||
),
|
||||
builder:
|
||||
(context, followLink) => TextButton(
|
||||
onPressed: followLink,
|
||||
child: const Text('Get an API Key'),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
@@ -37,20 +37,20 @@ class MessageWidget extends StatelessWidget {
|
||||
child: Container(
|
||||
constraints: const BoxConstraints(maxWidth: 520),
|
||||
decoration: BoxDecoration(
|
||||
color: isFromUser
|
||||
? Theme.of(context).colorScheme.primaryContainer
|
||||
: Theme.of(context).colorScheme.surfaceContainerHighest,
|
||||
color:
|
||||
isFromUser
|
||||
? Theme.of(context).colorScheme.primaryContainer
|
||||
: Theme.of(context).colorScheme.surfaceContainerHighest,
|
||||
borderRadius: BorderRadius.circular(18),
|
||||
),
|
||||
padding: const EdgeInsets.symmetric(
|
||||
vertical: 15,
|
||||
horizontal: 20,
|
||||
),
|
||||
padding: const EdgeInsets.symmetric(vertical: 15, horizontal: 20),
|
||||
margin: const EdgeInsets.only(bottom: 8),
|
||||
child: Column(children: [
|
||||
if (text case final text?) MarkdownBody(data: text),
|
||||
if (image case final image?) image,
|
||||
]),
|
||||
child: Column(
|
||||
children: [
|
||||
if (text case final text?) MarkdownBody(data: text),
|
||||
if (image case final image?) image,
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
|
||||
@@ -19,20 +19,12 @@ InputDecoration textFieldDecoration(BuildContext context, String hintText) {
|
||||
contentPadding: const EdgeInsets.all(15),
|
||||
hintText: hintText,
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: const BorderRadius.all(
|
||||
Radius.circular(14),
|
||||
),
|
||||
borderSide: BorderSide(
|
||||
color: Theme.of(context).colorScheme.secondary,
|
||||
),
|
||||
borderRadius: const BorderRadius.all(Radius.circular(14)),
|
||||
borderSide: BorderSide(color: Theme.of(context).colorScheme.secondary),
|
||||
),
|
||||
focusedBorder: OutlineInputBorder(
|
||||
borderRadius: const BorderRadius.all(
|
||||
Radius.circular(14),
|
||||
),
|
||||
borderSide: BorderSide(
|
||||
color: Theme.of(context).colorScheme.secondary,
|
||||
),
|
||||
borderRadius: const BorderRadius.all(Radius.circular(14)),
|
||||
borderSide: BorderSide(color: Theme.of(context).colorScheme.secondary),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ description: "Sample app for the google_generative_ai package"
|
||||
publish_to: 'none'
|
||||
version: 1.0.0+1
|
||||
environment:
|
||||
sdk: ^3.5.0
|
||||
sdk: ^3.7.0-0
|
||||
|
||||
dependencies:
|
||||
flutter:
|
||||
|
||||
Reference in New Issue
Block a user