Skip to content

Instantly share code, notes, and snippets.

@micimize
Created December 13, 2019 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micimize/bd1bfd3352ba3f9679789121bfce81ef to your computer and use it in GitHub Desktop.
Save micimize/bd1bfd3352ba3f9679789121bfce81ef to your computer and use it in GitHub Desktop.
Don't do this
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. 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';
enum _Side { top, right, bottom, left }
extension SpreadBorder on Border {
Iterable<MapEntry<_Side, BorderSide>> get sides {
return () sync* {
if (top != BorderSide.none) {
yield MapEntry(_Side.top, top);
}
if (right != BorderSide.none) {
yield MapEntry(_Side.right, right);
}
if (bottom != BorderSide.none) {
yield MapEntry(_Side.bottom, bottom);
}
if (left != BorderSide.none) {
yield MapEntry(_Side.left, left);
}
}();
}
static Border fromSides(Iterable<MapEntry<_Side, BorderSide>> parts) {
BorderSide top;
BorderSide right;
BorderSide bottom;
BorderSide left;
for (final borderPart in parts) {
switch (borderPart.key) {
case _Side.top:
top = borderPart.value;
break;
case _Side.right:
right = borderPart.value;
break;
case _Side.bottom:
bottom = borderPart.value;
break;
case _Side.left:
left = borderPart.value;
break;
}
}
return Border(
top: top,
right: right,
bottom: bottom,
left: left,
);
}
}
final Border spreadBorder = SpreadBorder.fromSides([
...Border(
left: BorderSide(color: Colors.pink),
right: BorderSide(color: Colors.pinkAccent),
).sides,
...Border(
top: BorderSide(color: Colors.blue),
bottom: BorderSide(color: Colors.blueAccent),
).sides,
]).scale(5);
// boilerplate
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,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
decoration: BoxDecoration(border: spreadBorder),
child: Text(
'You have pushed the button this many times:',
),),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment