|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | + |
| 3 | +void main(){ |
| 4 | + runApp(MyApp()); |
| 5 | +} |
| 6 | + |
| 7 | +/// App Widget |
| 8 | +class MyApp extends StatefulWidget { |
| 9 | + |
| 10 | + /// Initialize app |
| 11 | + MyApp(); |
| 12 | + |
| 13 | + @override |
| 14 | + _MyAppState createState() => _MyAppState(); |
| 15 | +} |
| 16 | + |
| 17 | +class _MyAppState extends State<MyApp> { |
| 18 | + /// Widget |
| 19 | + @override |
| 20 | + Widget build(BuildContext context) { |
| 21 | + return MaterialApp( |
| 22 | + debugShowCheckedModeBanner: false, |
| 23 | + |
| 24 | + /// Home |
| 25 | + home: ClipperExamples(), |
| 26 | + ); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +class ClipperExamples extends StatefulWidget { |
| 31 | + @override |
| 32 | + _ClipperExamplesState createState() => _ClipperExamplesState(); |
| 33 | +} |
| 34 | + |
| 35 | +class _ClipperExamplesState extends State<ClipperExamples> { |
| 36 | + @override |
| 37 | + Widget build(BuildContext context) { |
| 38 | + return Scaffold( |
| 39 | + appBar: AppBar( |
| 40 | + backgroundColor: Color(0XFF307777), |
| 41 | + title: Text("Clipper Example"), |
| 42 | + ), |
| 43 | + body: SingleChildScrollView( |
| 44 | + child: Container( |
| 45 | + width: double.infinity, |
| 46 | + margin: EdgeInsets.all(20.0), |
| 47 | + child: Column( |
| 48 | + crossAxisAlignment: CrossAxisAlignment.center, |
| 49 | + children: [ |
| 50 | + /// ClipRect |
| 51 | + Container( |
| 52 | + margin: EdgeInsets.only(top: 40.0,), |
| 53 | + child: Text( |
| 54 | + "ClipRect", |
| 55 | + style: Theme.of(context).textTheme.headline4, |
| 56 | + ), |
| 57 | + ), |
| 58 | + ClipRect( |
| 59 | + child: Container( |
| 60 | + child: Align( |
| 61 | + alignment: Alignment.center, |
| 62 | + widthFactor: 0.5, |
| 63 | + heightFactor: 0.8, |
| 64 | + child: Image.network( |
| 65 | + 'https://images.unsplash.com/photo-1595760780346-f972eb49709f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60'), |
| 66 | + ), |
| 67 | + ), |
| 68 | + ), |
| 69 | + |
| 70 | + /// ClipRRect |
| 71 | + Container( |
| 72 | + margin: EdgeInsets.only(top: 40.0,), |
| 73 | + child: Text( |
| 74 | + "ClipRRect", |
| 75 | + style: Theme.of(context).textTheme.headline4, |
| 76 | + ), |
| 77 | + ), |
| 78 | + ClipRRect( |
| 79 | + borderRadius: BorderRadius.circular(300.0), |
| 80 | + child: Image.network( |
| 81 | + "https://images.unsplash.com/photo-1595152772835-219674b2a8a6?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60"), |
| 82 | + ), |
| 83 | + |
| 84 | + /// ClipOval |
| 85 | + Container( |
| 86 | + margin: EdgeInsets.only(top: 40.0,), |
| 87 | + child: Text( |
| 88 | + "ClipOval", |
| 89 | + style: Theme.of(context).textTheme.headline4, |
| 90 | + ), |
| 91 | + ), |
| 92 | + ClipOval( |
| 93 | + child: Container( |
| 94 | + child: Image.network( |
| 95 | + 'https://images.unsplash.com/photo-1544005313-94ddf0286df2?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60'), |
| 96 | + ), |
| 97 | + ), |
| 98 | + |
| 99 | + /// ClipPath |
| 100 | + Container( |
| 101 | + margin: EdgeInsets.only(top: 40.0,), |
| 102 | + child: Text( |
| 103 | + "ClipPath", |
| 104 | + style: Theme.of(context).textTheme.headline4, |
| 105 | + ), |
| 106 | + ), |
| 107 | + ClipPath( |
| 108 | + clipper: TriangleClipper(), |
| 109 | + child: Image.network( |
| 110 | + "https://images.unsplash.com/photo-1519699047748-de8e457a634e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60"), |
| 111 | + ) |
| 112 | + ], |
| 113 | + ), |
| 114 | + ), |
| 115 | + ), |
| 116 | + ); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +class TriangleClipper extends CustomClipper<Path> { |
| 121 | + @override |
| 122 | + Path getClip(Size size) { |
| 123 | + final path = Path(); |
| 124 | + path.moveTo(size.width / 2, 0.0); |
| 125 | + path.lineTo(size.width, size.height); |
| 126 | + path.lineTo(0.0, size.height); |
| 127 | + path.close(); |
| 128 | + return path; |
| 129 | + } |
| 130 | + |
| 131 | + @override |
| 132 | + bool shouldReclip(TriangleClipper oldClipper) => false; |
| 133 | +} |
| 134 | + |
| 135 | + |
0 commit comments