flutter/examples/layers/widgets/styled_text.dart
Sangam Shrestha d261411b4c
Remove redundant useMaterial3: true (#163376)
<!--
Thanks for filing a pull request!
Reviewers are typically assigned within a week of filing a request.
To learn more about code review, see our documentation on Tree Hygiene:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
-->

This PR removes redundant useMaterial3: true as described in
https://github.com/flutter/flutter/issues/162818

*List which issues are fixed by this PR. You must list at least one
issue. An issue is not required if the PR fixes something trivial like a
typo.*

- https://github.com/flutter/flutter/issues/162818

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [ ] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md

---------

Co-authored-by: Qun Cheng <36861262+QuncCccccc@users.noreply.github.com>
2025-03-14 17:50:20 +00:00

116 lines
3.6 KiB
Dart

// Copyright 2014 The Flutter Authors. 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/material.dart';
typedef _TextTransformer = Widget Function(String name, String text);
// From https://en.wikiquote.org/wiki/2001:_A_Space_Odyssey_(film)
const String _kDialogText = '''
Dave: Open the pod bay doors, please, HAL. Open the pod bay doors, please, HAL. Hello, HAL. Do you read me? Hello, HAL. Do you read me? Do you read me, HAL?
HAL: Affirmative, Dave. I read you.
Dave: Open the pod bay doors, HAL.
HAL: I'm sorry, Dave. I'm afraid I can't do that.
Dave: What's the problem?
HAL: I think you know what the problem is just as well as I do.
Dave: What are you talking about, HAL?
HAL: This mission is too important for me to allow you to jeopardize it.''';
// [["Dave", "Open the pod bay..."] ...]
final List<List<String>> _kNameLines =
_kDialogText.split('\n').map<List<String>>((String line) => line.split(':')).toList();
final TextStyle _kDaveStyle = TextStyle(color: Colors.indigo.shade400, height: 1.8);
final TextStyle _kHalStyle = TextStyle(color: Colors.red.shade400, fontFamily: 'monospace');
const TextStyle _kBold = TextStyle(fontWeight: FontWeight.bold);
const TextStyle _kUnderline = TextStyle(
decoration: TextDecoration.underline,
decorationColor: Color(0xFF000000),
decorationStyle: TextDecorationStyle.wavy,
);
Widget toStyledText(String name, String text) {
final TextStyle lineStyle = (name == 'Dave') ? _kDaveStyle : _kHalStyle;
return RichText(
key: Key(text),
text: TextSpan(
style: lineStyle,
children: <TextSpan>[
TextSpan(
style: _kBold,
children: <TextSpan>[TextSpan(style: _kUnderline, text: name), const TextSpan(text: ':')],
),
TextSpan(text: text),
],
),
);
}
Widget toPlainText(String name, String text) => Text('$name:$text');
class SpeakerSeparator extends StatelessWidget {
const SpeakerSeparator({super.key});
@override
Widget build(BuildContext context) {
return Container(
constraints: const BoxConstraints.expand(height: 0.0),
margin: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 64.0),
decoration: const BoxDecoration(
border: Border(bottom: BorderSide(color: Color.fromARGB(24, 0, 0, 0))),
),
);
}
}
class StyledTextDemo extends StatefulWidget {
const StyledTextDemo({super.key});
@override
State<StyledTextDemo> createState() => _StyledTextDemoState();
}
class _StyledTextDemoState extends State<StyledTextDemo> {
_TextTransformer _toText = toStyledText;
void _handleTap() {
setState(() {
_toText = (_toText == toPlainText) ? toStyledText : toPlainText;
});
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: _handleTap,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children:
_kNameLines
.map<Widget>(
(List<String> nameAndText) => _toText(nameAndText[0], nameAndText[1]),
)
.expand((Widget line) => <Widget>[line, const SpeakerSeparator()])
.toList()
..removeLast(),
),
),
);
}
}
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Hal and Dave')),
body: Material(color: Colors.grey.shade50, child: const StyledTextDemo()),
),
),
);
}