美文网首页
自定义 MaterialColor

自定义 MaterialColor

作者: _Jock羁 | 来源:发表于2021-12-03 10:46 被阅读0次
    import 'package:flutter/material.dart';
    import 'login.dart';
    import 'dart:ui';
    
    void main() {
      runApp(const MyApp());
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({Key? key}) : super(key: key);
    
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: createMaterialColor(Color(0xFF273470)),
    
          ),
          home:Scaffold(
            appBar: AppBar(
              title: const Text("Log In",style: TextStyle(fontSize: 20.0,color: Colors.white),),
            ),
            body: const Login(),
          ),
        );
      }
    }
    
    
    //调用的时候需要把hex改一下,比如#223344 needs change to 0xFF223344
    //即把#换成0xFF即可
    
    MaterialColor createMaterialColor(Color color) {
      List strengths = <double>[.05];
      Map<int, Color> swatch = {};
      final int r = color.red, g = color.green, b = color.blue;
    
      for (int i = 1; i < 10; i++) {
        strengths.add(0.1 * i);
      }
      strengths.forEach((strength) {
        final double ds = 0.5 - strength;
        swatch[(strength * 1000).round()] = Color.fromRGBO(
          r + ((ds < 0 ? r : (255 - r)) * ds).round(),
          g + ((ds < 0 ? g : (255 - g)) * ds).round(),
          b + ((ds < 0 ? b : (255 - b)) * ds).round(),
          1,
        );
      });
      return MaterialColor(color.value, swatch);
    }
    

    相关文章

      网友评论

          本文标题:自定义 MaterialColor

          本文链接:https://www.haomeiwen.com/subject/nnppxrtx.html