Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

android studio - Flutter: Multiple material widgets

[![enter image description here][1]][1]A little bit of a beginner question, but I have been messing with it for a long time and looking around without finding an answer.

I am using Flutter framework and I am trying to run multiple Stateless widgets, a bottom navigation bar and a top appbar to hold menu and other navigation buttons. I am struggling to get both running at once and I can not find a way to run them both. At the moment, when I call home:MyBottomNavigationBar only this appears and not the other(Makes sense) But what command or solution would help me run multiple widgets? I have tried tons of different ways to use home, but nothing seems to work. My code is very simple. Just creating a Navigation bar and appbar with stf inside main.dart.

If needed I can attach the code, but hopefully my question is clear enough.

Done In Xamarin, I can have both BottomNavBar and Couple of Nav buttons at the top easily. [1]: https://i.stack.imgur.com/Q2qXg.png

BottomNavBar.dart in Widgets folder

import 'dart:io';

class MyBottomNavigationBar extends StatefulWidget
{
  @override
  MyBottomNavigationBarState createState() => MyBottomNavigationBarState();
}

class MyBottomNavigationBarState extends State<MyBottomNavigationBar>
{
  int _currentIndex = 0;
  final List <Widget> _children =
  [
    HomePage(),
    ExplorePage(),
    ProgressPage(),
    ChallengesPage(),
    ProfilePage(),
  ];

  void onTappedBar(int index)
  {
    setState((){
      _currentIndex=index;
    });
  }

  @override
  Widget build(BuildContext context)
  {
    return new Scaffold
      (
      body: _children[_currentIndex],
      bottomNavigationBar: BottomNavigationBar
        (

        onTap: onTappedBar,
        currentIndex: _currentIndex,

        items:
        [

          BottomNavigationBarItem
            (
            backgroundColor: Colors.pinkAccent,
            icon: new Icon(Icons.home,
                color: Colors.black),
            label: 'Feed',
            //activeBackgroundColor: Colors.red, // this is the change
          ),
question from:https://stackoverflow.com/questions/65844103/flutter-multiple-material-widgets

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

please put the code here, it would be helpful. I personally didn't understand what you're trying to achieve. every stateless/stateful has one build method which returns a widget (that has its own build method). However, as some widgets can have more than one child (row,column,stack), you can use your pre-built layout composition and then mount it to the widget tree. Or, simply use your custom layout by extending CustomMultiChildLayout, you can also use "Overlay"-ing, to paint directly on the canvas based on an event or a state. ||||||||||||

new answer: enter image description here

I tried to reproduced the image, basically you need to use scaffold's appBar and bottomNavigationBar properties. appbar is extending PreferredSizedWidget so you can easily create your own.

main.dart

 void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    MyBottomNavigationBar();
    return MaterialApp(
      title: _title,
      home: HomePage(),
    );
  }
}

home_page.dart

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Flutter version"),
        actions: [
          IconButton(icon: Icon(Icons.menu), onPressed: () {}),
          IconButton(icon: Icon(Icons.account_circle), onPressed: () {}),
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
        backgroundColor: Colors.blue,
        items: [
          BottomNavigationBarItem(
            backgroundColor: Colors.pinkAccent,
            icon: new Icon(Icons.home, color: Colors.black),
            label: 'Feed',
            //activeBackgroundColor: Colors.red, // this is the change
          ),
          BottomNavigationBarItem(
            backgroundColor: Colors.pinkAccent,
            icon: new Icon(Icons.home, color: Colors.black),
            label: 'Feed',
            //activeBackgroundColor: Colors.red, // this is the change
          ),
        ],
      ),
      body: Column(
        children: [
          Container(
            width: MediaQuery.of(context).size.width,
            height: MediaQuery.of(context).size.height/7,
            color: Colors.blue,
            child: Center(child: Text("Challenges Page",style: TextStyle(fontSize: 41,color: Colors.white),),),
          ),
        ],
      ),
    );
  }
}

assuming you want to create your layout from scratch and don't want to use the scaffold's properties. the code blow is the same layout with stack, I didn't make it beautiful though, it just shows the layout without refactoring any widget.

enter image description here

code for home_page.dart (main.dart is the same as above)

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Container(
          width: MediaQuery.of(context).size.width,
          height: MediaQuery.of(context).size.height,
          child: Stack(
            children: [
              Container(
                width: MediaQuery.of(context).size.width,
                height: MediaQuery.of(context).size.height/12,
                decoration: BoxDecoration(
                  color: Colors.blue,
                  boxShadow: [
                    BoxShadow(color: Colors.black54,blurRadius: 2,spreadRadius: .1,offset: Offset(0, 2)),
                  ],
                ),

                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Row(

                      children: [
                        Padding(
                          padding: const EdgeInsets.all(18.0),
                          child: Text("just stack",style: TextStyle(fontSize: 21,color: Colors.white),),
                        ),
                      ],
                    ),
                    Row(
                      children: [
                        IconButton(icon: Icon(Icons.account_circle,size: 32,), onPressed: (){}),IconButton(icon: Icon(Icons.settings,size: 32,), onPressed: (){}),
                      ],
                    ),


                  ],
                ),
              ),
              Positioned(
                top: MediaQuery.of(context).size.height/12+1,
                child: Container(
                  width: MediaQuery.of(context).size.width,
                  height: MediaQuery.of(context).size.height/10,
                  color: Colors.blue,
                  child: Center(child: Text("Challenges Page",style: TextStyle(fontSize: 31,color: Colors.white),)),
                ),
              ),
              Positioned(
                bottom:0,
                child: Container(
                  width: MediaQuery.of(context).size.width,
                  height: MediaQuery.of(context).size.height/12,
                  color: Colors.blue,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Column(
                        children: [
                          IconButton(icon: Icon(Icons.home,size: 32,), onPressed: (){}),
                          Text("home"),
                        ],
                      ),
                      IconButton(icon: Icon(Icons.account_circle,size: 32,), onPressed: (){}),
                      IconButton(icon: Icon(Icons.settings,size: 32,), onPressed: (){}),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...