Палитра ColorScheme.fromSeed в Flutter
Роли цветов описывает
ColorScheme:
основной, фон,
поверхность, ошибка. В Material 3 палитру
удобно получить из одного «семени» функцией
ColorScheme.fromSeed.
Достаточно передать seedColor -
Flutter подберёт оттенки для кнопок,
AppBar и акцентов. Поля
primary, surface и error
уже согласованы по контрасту. Схему вкладывают
в ThemeData
через colorScheme: ...:
import 'package:flutter/material.dart';
void main() {
runApp(const SchemeApp());
}
class SchemeApp extends StatelessWidget {
const SchemeApp({super.key});
@override
Widget build(BuildContext context) {
final scheme = ColorScheme.fromSeed(seedColor: Colors.indigo);
return MaterialApp(
theme: ThemeData(useMaterial3: true, colorScheme: scheme),
home: const SchemeDemoPage(),
);
}
}
class SchemeDemoPage extends StatelessWidget {
const SchemeDemoPage({super.key});
@override
Widget build(BuildContext context) {
final scheme = Theme.of(context).colorScheme;
return Scaffold(
appBar: AppBar(title: const Text('ColorScheme')),
body: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Primary', style: TextStyle(color: scheme.primary)),
Text('On surface', style: TextStyle(color: scheme.onSurface)),
const SizedBox(height: 16),
FilledButton(
onPressed: () {},
child: const Text('FilledButton'),
),
],
),
),
);
}
}
Соберите тему так:
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.green,
),
),
На экране покажите три полоски высотой 40.
Цвет каждой берите из
Theme.of(context).colorScheme:
final scheme = Theme.of(context).colorScheme;
Column(
children: [
Container(height: 40, color: scheme.primary),
Container(height: 40, color: scheme.secondary),
Container(height: 40, color: scheme.tertiary),
],
)