【问题标题】:How can i apply this design in flutter我如何在颤振中应用这种设计
【发布时间】:2021-05-24 21:50:21
【问题描述】:

我希望所有视图都做得很好,我想用颤振应用图片中的设计,我创建了一个自定义 appBar 但我不能这样做,我尝试使用定位小部件并且我给容器对齐但它没有工作,有人可以帮助我吗?

class Test extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        height: double.infinity,
        width: double.infinity,
        child: Column(
          children: [
            Container(
              height: 147,
              decoration: BoxDecoration(color: Theme.of(context).primaryColor),
            ),
            Container(
              alignment: Alignment(.40, -0.80),
              height: 50,
              width: 343,
              child: TextField(
                  textAlign: TextAlign.right,
                  decoration: InputDecoration(
                    filled: true,
                    fillColor: Colors.white24,
                    hintText: 'Data',
                    prefixIcon: Icon(Icons.search),
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(20),
                      borderSide: BorderSide(width: 0, color: Colors.white12),
                    ),
                    focusedBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(100),
                        borderSide:
                            BorderSide(width: 0, color: Colors.white12)),
                  )),
            )
          ],
        ),
      ),
    );
  }
}

【问题讨论】:

    标签: flutter dart flutter-layout textfield flutter-widget


    【解决方案1】:

    尝试使用Stack()Positioned() - 在堆栈中对齐容器

    https://api.flutter.dev/flutter/widgets/Stack-class.html

    【讨论】:

      【解决方案2】:

      使用 Stack() 和 Positioned()

      return Scaffold(
        body: Container(
          height: double.infinity,
          width: double.infinity,
          child: Stack(
            children: [
              Container(
                height: 147,
                decoration: BoxDecoration(color: Theme.of(context).primaryColor),
                child: Padding(
                  padding: EdgeInsets.only(bottom: 10.0, left: 10.0, right: 10.0),
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Row(
                        children: [
                          IconButton(
                            icon: Icon(Icons.menu, color: Colors.white),
                            onPressed: () => print('Test'),
                          ),
                          SizedBox(width: 10.0),
                          Text('Data', style: TextStyle(color: Colors.white, fontSize: 20.0)),
                        ],
                      ),
                      Icon(Icons.bubble_chart_rounded, color: Colors.white)
                    ],
                  ),
                ),
              ),
              Positioned(
                top: 120,
                child: Container(
                  width: MediaQuery.of(context).size.width,
                  child: Padding(
                    padding: const EdgeInsets.only(right: 25, left: 25),
                    child: Column(
                      children: [
                        TextField(
                            textAlign: TextAlign.right,
                            decoration: InputDecoration(
                              filled: true,
                              fillColor: Colors.white24,
                              hintText: 'Data',
                              prefixIcon: Icon(Icons.search),
                              border: OutlineInputBorder(
                                borderRadius: BorderRadius.circular(20),
                                borderSide: BorderSide(width: 0, color: Colors.white12),
                              ),
                              focusedBorder: OutlineInputBorder(borderRadius: BorderRadius.circular(100), borderSide: BorderSide(width: 0, color: Colors.white12)),
                            )),
                        Row(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          children: [
                            Text('Data'),
                            Text('Data'),
                          ],
                        )
                      ],
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      );
      

      【讨论】:

      • yeeeees 我的朋友效果很好,非常感谢您的帮助❤
      【解决方案3】:

      这是一个例子,我的朋友。

      只更改您的图标

      import 'package:flutter/material.dart';
      import 'package:flutter/services.dart';
      
       
      void main() {
        WidgetsFlutterBinding.ensureInitialized();
        SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
          statusBarColor: Colors.transparent, // transparent status bar
        ));
        runApp(MyApp());
      } 
       
      class MyApp extends StatelessWidget {
      
        final _primaryColor = Color(0xff1877F2);
      
        @override
        Widget build(BuildContext context) {
          return MaterialApp(
            title: 'Material App',
            debugShowCheckedModeBanner: false,
            home: Scaffold(
              appBar: AppBar(
                title: Text('Data'),
                centerTitle: true,
                leading: IconButton(
                  icon: Icon(Icons.menu), 
                  onPressed: (){}
                ),
                elevation: 0,
                backgroundColor: _primaryColor,
                actions: [
                  IconButton(
                    icon: Icon(Icons.shopping_bag_rounded), 
                    onPressed: (){}
                  ),
                ],
              ),
              body: Stack(
                children: [
                  Column(
                    children: [
                      Container(
                        height: 50,
                        color: _primaryColor,
                      ),
                      Expanded(child: Container())
                    ],
                  ),
      
                  Positioned(
                    top: 20,
                    left: 16,
                    right: 16,
                    child: Container(
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(10),
                        boxShadow: [
                          BoxShadow(
                            blurRadius: 5,
                            color: Colors.grey[400],
                            offset: Offset(0, 1),
                            spreadRadius: -1
                          )
                        ]
                      ),
                      child: TextField(
                          decoration: InputDecoration(
                            filled: true,
                            contentPadding: EdgeInsets.only(left: 32, top: 16, bottom: 16),
                            fillColor: Colors.white,
                            hintText: 'Search to find more products...',
                            suffixIcon: Icon(Icons.search),
                            border: OutlineInputBorder(
                              borderRadius: BorderRadius.circular(10),
                              borderSide: BorderSide.none,
                            ),
                          )),
                    ),
                  ),
      
                  Positioned(
                    top: 80,
                    left: 0,
                    right: 0,
                    bottom: 0,
                    child: ListView.builder(
                      itemBuilder: (_, i) {
                        return ListTile(
                          title: Text('Row $i'),
                        );
                      }
                    )
                  )
      
                ],
              ),
            )
          );
        }
      }
      

      【讨论】:

      • 是的!太棒了,非常感谢你❤
      猜你喜欢
      • 2022-07-04
      • 2020-01-01
      • 1970-01-01
      • 2021-07-02
      • 1970-01-01
      • 2021-06-27
      • 2022-08-20
      • 2019-02-23
      • 1970-01-01
      相关资源
      最近更新 更多