简体   繁体   中英

Flutter reuse custom widget

Let's say I created a custom widget that emulates button functions.

  @override
  Widget build(BuildContext context) {
    return RawMaterialButton(
      onPressed: () {},
      elevation: 2.0,
      fillColor: Colors.lightGreenAccent,
      child: SizedBox (
        width: 90 ,
        height: 90,
        child: Column (
          children: [
            Icon(Icons.face,
            size: 50,
            color: Colors.red,),
            Text('Enable ->'),
            Container(
              height: 20,
              width: 80,
              decoration: new BoxDecoration(
                color: Colors.red,
                borderRadius: new BorderRadius.all(Radius.elliptical(45, 10)),
              ),
            ),
          ],
        ),
      ),
      padding: EdgeInsets.all(15.0),
      shape: CircleBorder(),
    );
  }

How can I create a group of such widgets, but so that each has a different icon or text by reusing my widget?

You can customize your widget like this:

import 'package:flutter/material.dart';

class CustomWidget extends StatelessWidget {
CustomWidget({this.icon, this.text});
final Widget icon;
final Widget text;

@override
Widget build(BuildContext context) {
 return RawMaterialButton(
  onPressed: () {},
  elevation: 2.0,
  fillColor: Colors.lightGreenAccent,
  child: SizedBox (
    width: 90 ,
    height: 90,
    child: Column (
      children: [
        icon,
        text,
        Container(
          height: 20,
          width: 80,
          decoration: new BoxDecoration(
            color: Colors.red,
            borderRadius: new BorderRadius.all(Radius.elliptical(45, 10)),
          ),
        ),
      ],
    ),
  ),
  padding: EdgeInsets.all(15.0),
  shape: CircleBorder(),
  );

 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM