1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-08 13:58:47 +00:00
Files
samples/veggieseasons/lib/screens/search.dart
Eric Windmill 2999d738b8 Dart 3.9 / Flutter 3.35 [first LLM release] (#2714)
I got carried away with Gemini and basically rewrote CI and the release
process for the new LLM reality. This work was largely completed by
Gemini.

- Bump all SDK versions to the current beta (3.9.0-0)
- Run `flutter channel beta`
- Wrote `ci_script.dart` to replace the bash scripts
- Converted repository to pub workspace #2499 
- Added llm.md and release.md
- Added redirect for deprecated Samples Index

## 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 have added sample code updates to the [changelog].
- [x] I updated/added relevant documentation (doc comments with `///`).
2025-08-14 12:26:24 -07:00

133 lines
3.7 KiB
Dart

// Copyright 2018 The Flutter team. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import '../data/app_state.dart';
import '../data/veggie.dart';
import '../widgets/veggie_headline.dart';
class SearchScreen extends StatefulWidget {
const SearchScreen({this.restorationId, super.key});
final String? restorationId;
@override
State<SearchScreen> createState() => _SearchScreenState();
}
class _SearchScreenState extends State<SearchScreen>
with RestorationMixin {
final controller = RestorableTextEditingController();
final focusNode = FocusNode();
String? terms;
@override
String? get restorationId => widget.restorationId;
@override
void restoreState(RestorationBucket? oldBucket, bool initialRestore) {
registerForRestoration(controller, 'text');
controller.addListener(_onTextChanged);
terms = controller.value.text;
}
@override
void dispose() {
focusNode.dispose();
controller.dispose();
super.dispose();
}
void _onTextChanged() {
setState(() => terms = controller.value.text);
}
Widget _createSearchBox({bool focus = true}) {
return Padding(
padding: const EdgeInsets.all(8),
child: CupertinoSearchTextField(
controller: controller.value,
focusNode: focus ? focusNode : null,
),
);
}
Widget _buildSearchResults(List<Veggie> veggies) {
if (veggies.isEmpty) {
return Center(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 24),
child: Text(
'No veggies matching your search terms were found.',
style: CupertinoTheme.of(context).textTheme.textStyle,
),
),
);
}
return ListView.builder(
restorationId: 'list',
itemCount: veggies.length + 1,
itemBuilder: (context, i) {
if (i == 0) {
return Visibility(
// This invisible and otherwise unnecessary search box is used to
// pad the list entries downward, so none will be underneath the
// real search box when the list is at its top scroll position.
visible: false,
maintainSize: true,
maintainAnimation: true,
maintainState: true,
// This invisible and otherwise unnecessary search box is used to
// pad the list entries downward, so none will be underneath the
// real search box when the list is at its top scroll position.
child: _createSearchBox(focus: false),
);
} else {
return Padding(
padding: const EdgeInsets.only(
left: 16,
right: 16,
bottom: 24,
),
child: VeggieHeadline(veggies[i - 1]),
);
}
},
);
}
@override
Widget build(BuildContext context) {
final model = Provider.of<AppState>(context);
return UnmanagedRestorationScope(
bucket: bucket,
child: CupertinoTabView(
restorationScopeId: 'tabview',
builder: (context) {
return AnnotatedRegion<SystemUiOverlayStyle>(
value: SystemUiOverlayStyle(
statusBarBrightness: MediaQuery.platformBrightnessOf(
context,
),
),
child: SafeArea(
bottom: false,
child: Stack(
children: [
_buildSearchResults(model.searchVeggies(terms)),
_createSearchBox(),
],
),
),
);
},
),
);
}
}