Skip to content

Commit dfd4244

Browse files
authored
Update CupertinoTextField example (#93738)
1 parent 60f039c commit dfd4244

3 files changed

Lines changed: 77 additions & 28 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
// Flutter code sample for CupertinoTextField
6+
7+
import 'package:flutter/cupertino.dart';
8+
9+
void main() => runApp(const MyApp());
10+
11+
class MyApp extends StatelessWidget {
12+
const MyApp({Key? key}) : super(key: key);
13+
14+
static const String _title = 'Flutter Code Sample';
15+
16+
@override
17+
Widget build(BuildContext context) {
18+
return const CupertinoApp(
19+
title: _title,
20+
home: MyStatefulWidget(),
21+
);
22+
}
23+
}
24+
25+
class MyStatefulWidget extends StatefulWidget {
26+
const MyStatefulWidget({Key? key}) : super(key: key);
27+
28+
@override
29+
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
30+
}
31+
32+
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
33+
late TextEditingController _textController;
34+
35+
@override
36+
void initState() {
37+
super.initState();
38+
_textController = TextEditingController(text: 'initial text');
39+
}
40+
41+
@override
42+
void dispose() {
43+
_textController.dispose();
44+
super.dispose();
45+
}
46+
47+
@override
48+
Widget build(BuildContext context) {
49+
return CupertinoPageScaffold(
50+
child: Center(
51+
child: CupertinoTextField(
52+
controller: _textController,
53+
)
54+
),
55+
);
56+
}
57+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter_api_samples/cupertino/text_field/cupertino_text_field.0.dart' as example;
6+
import 'package:flutter_test/flutter_test.dart';
7+
8+
void main() {
9+
testWidgets('CupertinoTextField has initial text', (WidgetTester tester) async {
10+
await tester.pumpWidget(
11+
const example.MyApp(),
12+
);
13+
14+
expect(find.text('initial text'), findsOneWidget);
15+
});
16+
}

packages/flutter/lib/src/cupertino/text_field.dart

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -139,35 +139,11 @@ class _CupertinoTextFieldSelectionGestureDetectorBuilder extends TextSelectionGe
139139
///
140140
/// {@macro flutter.widgets.EditableText.onChanged}
141141
///
142-
/// To control the text that is displayed in the text field, use the
143-
/// [controller]. For example, to set the initial value of the text field, use
144-
/// a [controller] that already contains some text such as:
142+
/// {@tool dartpad}
143+
/// This example shows how to set the initial value of the `CupertinoTextField` using
144+
/// a [controller] that already contains some text.
145145
///
146-
/// {@tool snippet}
147-
///
148-
/// ```dart
149-
/// class MyPrefilledText extends StatefulWidget {
150-
/// const MyPrefilledText({Key? key}) : super(key: key);
151-
///
152-
/// @override
153-
/// State<MyPrefilledText> createState() => _MyPrefilledTextState();
154-
/// }
155-
///
156-
/// class _MyPrefilledTextState extends State<MyPrefilledText> {
157-
/// late TextEditingController _textController;
158-
///
159-
/// @override
160-
/// void initState() {
161-
/// super.initState();
162-
/// _textController = TextEditingController(text: 'initial text');
163-
/// }
164-
///
165-
/// @override
166-
/// Widget build(BuildContext context) {
167-
/// return CupertinoTextField(controller: _textController);
168-
/// }
169-
/// }
170-
/// ```
146+
/// ** See code in examples/api/lib/cupertino/text_field/cupertino_text_field.0.dart **
171147
/// {@end-tool}
172148
///
173149
/// The [controller] can also control the selection and composing region (and to

0 commit comments

Comments
 (0)