Skip to content

Instantly share code, notes, and snippets.

@dened
Last active March 3, 2021 12:54
Show Gist options
  • Save dened/bfd91765ce713507395f33b3f535aa1e to your computer and use it in GitHub Desktop.
Save dened/bfd91765ce713507395f33b3f535aa1e to your computer and use it in GitHub Desktop.
ElevatedButton without elevation at all.
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
/// Дефолтный класс [_ElevatedButtonDefaultElevation] добавляет к elevation различные значения в зависимости от состояния кнопки
@immutable
class ZeroElevationProperty extends MaterialStateProperty<double>
with Diagnosticable {
ZeroElevationProperty();
@override
double resolve(Set<MaterialState> states) => 0;
}
@immutable
class ElevatedButtonBackground extends MaterialStateProperty<Color>
with Diagnosticable {
ElevatedButtonBackground(this.primary, this.hovered);
final Color primary;
final Color hovered;
@override
Color resolve(Set<MaterialState> states) {
if (states.contains(MaterialState.hovered)) return hovered;
return primary;
}
}
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
elevatedButtonTheme: ElevatedButtonThemeData(
style: ElevatedButton.styleFrom(
elevation: 0,
minimumSize: const Size(120, 48),
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(6)),
),
),
),
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
MyHomePage({Key? key, required this.title}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(onPressed: () {}, child: const Text('Default')),
const SizedBox(height: 16),
ElevatedButton(
style: ElevatedButton.styleFrom().copyWith(
elevation: ZeroElevationProperty(),
backgroundColor: ElevatedButtonBackground(
Theme.of(context).accentColor,
Colors.red,
),
),
onPressed: () {},
child: const Text('Zero elevation')),
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment